From ddefad503a825e058d3c7a10eef567354de5252e Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Tue, 14 Apr 2015 15:22:01 +0000 Subject: [PATCH] Use issubclass --- orchestra/admin/utils.py | 2 +- orchestra/contrib/orchestration/backends.py | 6 ++---- orchestra/contrib/orchestration/manager.py | 4 +--- orchestra/contrib/orchestration/middlewares.py | 18 ++---------------- orchestra/contrib/orchestration/models.py | 4 ---- orchestra/contrib/resources/backends.py | 2 +- 6 files changed, 7 insertions(+), 29 deletions(-) diff --git a/orchestra/admin/utils.py b/orchestra/admin/utils.py index cf4ad24e..fede4fe3 100644 --- a/orchestra/admin/utils.py +++ b/orchestra/admin/utils.py @@ -36,7 +36,7 @@ def get_modeladmin(model, import_module=True): def insertattr(model, name, value): """ Inserts attribute to a modeladmin """ modeladmin = None - if isinstance(model, models.Model) + if issubclass(model, models.Model): modeladmin = get_modeladmin(model) modeladmin_class = type(modeladmin) elif not inspect.isclass(model): diff --git a/orchestra/contrib/orchestration/backends.py b/orchestra/contrib/orchestration/backends.py index d0e9e598..7c74ed4c 100644 --- a/orchestra/contrib/orchestration/backends.py +++ b/orchestra/contrib/orchestration/backends.py @@ -99,13 +99,11 @@ class ServiceBackend(plugins.Plugin, metaclass=ServiceMount): return None @classmethod - def get_backends(cls, instance=None, action=None, active=None): + def get_backends(cls, instance=None, action=None): backends = cls.get_plugins() included = [] # Filter for instance or action for backend in backends: - if active is not None and backend.get_name() not in active: - continue include = True if instance: opts = instance._meta @@ -205,5 +203,5 @@ class ServiceController(ServiceBackend): """ filter controller classes """ backends = super(ServiceController, cls).get_backends() return [ - backend for backend in backends if isinstance(backend, ServiceController) + backend for backend in backends if issubclass(backend, ServiceController) ] diff --git a/orchestra/contrib/orchestration/manager.py b/orchestra/contrib/orchestration/manager.py index a2a06724..a7cca3bf 100644 --- a/orchestra/contrib/orchestration/manager.py +++ b/orchestra/contrib/orchestration/manager.py @@ -1,4 +1,3 @@ - import logging import threading import traceback @@ -141,8 +140,7 @@ def collect(instance, action, **kwargs): """ collect operations """ operations = kwargs.get('operations', set()) route_cache = kwargs.get('route_cache', {}) - active_backends = kwargs.get('active_backends', None) - for backend_cls in ServiceBackend.get_backends(active=active_backends): + for backend_cls in ServiceBackend.get_backends(): # Check if there exists a related instance to be executed for this backend and action instances = [] if action in backend_cls.actions: diff --git a/orchestra/contrib/orchestration/middlewares.py b/orchestra/contrib/orchestration/middlewares.py index c1ece185..3b1a96d2 100644 --- a/orchestra/contrib/orchestration/middlewares.py +++ b/orchestra/contrib/orchestration/middlewares.py @@ -6,16 +6,13 @@ from django.db.models.signals import pre_delete, post_save, m2m_changed from django.dispatch import receiver from django.http.response import HttpResponseServerError -from orchestra.utils.python import OrderedSet, import_class +from orchestra.utils.python import OrderedSet -from . import manager, Operation, settings +from . import manager, Operation from .helpers import message_user from .models import BackendLog -router = import_class(settings.ORCHESTRATION_ROUTER) - - @receiver(post_save, dispatch_uid='orchestration.post_save_collector') def post_save_collector(sender, *args, **kwargs): if sender not in [BackendLog, Operation]: @@ -66,16 +63,6 @@ class OperationsMiddleware(object): return request.route_cache return {} - @classmethod - def get_active_cache(cls): - """ chache the routes to save sql queries """ - if hasattr(cls.thread_locals, 'request'): - request = cls.thread_locals.request - if not hasattr(request, 'active_cache'): - request.active_cache = router.get_active_backends() - return request.active_cache - return router.get_active_backends() - @classmethod def collect(cls, action, **kwargs): """ Collects all pending operations derived from model signals """ @@ -84,7 +71,6 @@ class OperationsMiddleware(object): return kwargs['operations'] = cls.get_pending_operations() kwargs['route_cache'] = cls.get_route_cache() - kwargs['active_backends'] = cls.get_active_cache() instance = kwargs.pop('instance') manager.collect(instance, action, **kwargs) diff --git a/orchestra/contrib/orchestration/models.py b/orchestra/contrib/orchestration/models.py index ef731fe0..14012d96 100644 --- a/orchestra/contrib/orchestration/models.py +++ b/orchestra/contrib/orchestration/models.py @@ -169,10 +169,6 @@ class Route(models.Model): servers.append(route.host) return servers - @classmethod - def get_active_backends(cls): - return cls.objects.filter(is_active=True).values_list('backend', flat=True) - def clean(self): if not self.match: self.match = 'True' diff --git a/orchestra/contrib/resources/backends.py b/orchestra/contrib/resources/backends.py index 37257a19..84d5e1c0 100644 --- a/orchestra/contrib/resources/backends.py +++ b/orchestra/contrib/resources/backends.py @@ -21,7 +21,7 @@ class ServiceMonitor(ServiceBackend): def get_plugins(cls): """ filter controller classes """ return [ - plugin for plugin in cls.plugins if isinstance(plugin, ServiceMonitor) + plugin for plugin in cls.plugins if issubclass(plugin, ServiceMonitor) ] @classmethod