Use issubclass

This commit is contained in:
Marc Aymerich 2015-04-14 15:22:01 +00:00
parent 5606b14e28
commit ddefad503a
6 changed files with 7 additions and 29 deletions

View File

@ -36,7 +36,7 @@ def get_modeladmin(model, import_module=True):
def insertattr(model, name, value): def insertattr(model, name, value):
""" Inserts attribute to a modeladmin """ """ Inserts attribute to a modeladmin """
modeladmin = None modeladmin = None
if isinstance(model, models.Model) if issubclass(model, models.Model):
modeladmin = get_modeladmin(model) modeladmin = get_modeladmin(model)
modeladmin_class = type(modeladmin) modeladmin_class = type(modeladmin)
elif not inspect.isclass(model): elif not inspect.isclass(model):

View File

@ -99,13 +99,11 @@ class ServiceBackend(plugins.Plugin, metaclass=ServiceMount):
return None return None
@classmethod @classmethod
def get_backends(cls, instance=None, action=None, active=None): def get_backends(cls, instance=None, action=None):
backends = cls.get_plugins() backends = cls.get_plugins()
included = [] included = []
# Filter for instance or action # Filter for instance or action
for backend in backends: for backend in backends:
if active is not None and backend.get_name() not in active:
continue
include = True include = True
if instance: if instance:
opts = instance._meta opts = instance._meta
@ -205,5 +203,5 @@ class ServiceController(ServiceBackend):
""" filter controller classes """ """ filter controller classes """
backends = super(ServiceController, cls).get_backends() backends = super(ServiceController, cls).get_backends()
return [ return [
backend for backend in backends if isinstance(backend, ServiceController) backend for backend in backends if issubclass(backend, ServiceController)
] ]

View File

@ -1,4 +1,3 @@
import logging import logging
import threading import threading
import traceback import traceback
@ -141,8 +140,7 @@ def collect(instance, action, **kwargs):
""" collect operations """ """ collect operations """
operations = kwargs.get('operations', set()) operations = kwargs.get('operations', set())
route_cache = kwargs.get('route_cache', {}) route_cache = kwargs.get('route_cache', {})
active_backends = kwargs.get('active_backends', None) for backend_cls in ServiceBackend.get_backends():
for backend_cls in ServiceBackend.get_backends(active=active_backends):
# Check if there exists a related instance to be executed for this backend and action # Check if there exists a related instance to be executed for this backend and action
instances = [] instances = []
if action in backend_cls.actions: if action in backend_cls.actions:

View File

@ -6,16 +6,13 @@ from django.db.models.signals import pre_delete, post_save, m2m_changed
from django.dispatch import receiver from django.dispatch import receiver
from django.http.response import HttpResponseServerError 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 .helpers import message_user
from .models import BackendLog from .models import BackendLog
router = import_class(settings.ORCHESTRATION_ROUTER)
@receiver(post_save, dispatch_uid='orchestration.post_save_collector') @receiver(post_save, dispatch_uid='orchestration.post_save_collector')
def post_save_collector(sender, *args, **kwargs): def post_save_collector(sender, *args, **kwargs):
if sender not in [BackendLog, Operation]: if sender not in [BackendLog, Operation]:
@ -66,16 +63,6 @@ class OperationsMiddleware(object):
return request.route_cache return request.route_cache
return {} 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 @classmethod
def collect(cls, action, **kwargs): def collect(cls, action, **kwargs):
""" Collects all pending operations derived from model signals """ """ Collects all pending operations derived from model signals """
@ -84,7 +71,6 @@ class OperationsMiddleware(object):
return return
kwargs['operations'] = cls.get_pending_operations() kwargs['operations'] = cls.get_pending_operations()
kwargs['route_cache'] = cls.get_route_cache() kwargs['route_cache'] = cls.get_route_cache()
kwargs['active_backends'] = cls.get_active_cache()
instance = kwargs.pop('instance') instance = kwargs.pop('instance')
manager.collect(instance, action, **kwargs) manager.collect(instance, action, **kwargs)

View File

@ -169,10 +169,6 @@ class Route(models.Model):
servers.append(route.host) servers.append(route.host)
return servers return servers
@classmethod
def get_active_backends(cls):
return cls.objects.filter(is_active=True).values_list('backend', flat=True)
def clean(self): def clean(self):
if not self.match: if not self.match:
self.match = 'True' self.match = 'True'

View File

@ -21,7 +21,7 @@ class ServiceMonitor(ServiceBackend):
def get_plugins(cls): def get_plugins(cls):
""" filter controller classes """ """ filter controller classes """
return [ return [
plugin for plugin in cls.plugins if isinstance(plugin, ServiceMonitor) plugin for plugin in cls.plugins if issubclass(plugin, ServiceMonitor)
] ]
@classmethod @classmethod