2013-11-14 01:16:14 +01:00
|
|
|
class SignalConnectMetaClass(type):
|
|
|
|
"""
|
|
|
|
Metaclass to connect the children of a base class to a Django signal.
|
|
|
|
|
|
|
|
Classes must have a signal argument and a get_dispatch_uid classmethod.
|
|
|
|
The signal argument must be the Django signal the class should be
|
|
|
|
connected to. The get_dispatch_uid classmethod must return a unique
|
2014-05-06 13:21:46 +02:00
|
|
|
value for each child class and None for base classes because they will
|
|
|
|
not be connected to the signal.
|
2013-11-14 01:16:14 +01:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
The classmethod get_all is added to every class using this metaclass.
|
|
|
|
Calling this on a base class or on child classes will retrieve all
|
|
|
|
connected children, one instance for each child class.
|
2013-12-09 23:56:01 +01:00
|
|
|
|
|
|
|
These instances will have a check_permission method which returns True
|
|
|
|
by default. You can override this method to return False on runtime if
|
|
|
|
you want to filter some children.
|
|
|
|
|
|
|
|
They will also have a get_default_weight method which returns the value
|
|
|
|
of the default_weight attribute which is 0 by default. You can override
|
|
|
|
the attribute or the method to sort the children.
|
2013-11-14 01:16:14 +01:00
|
|
|
|
2014-05-06 13:21:46 +02:00
|
|
|
Don't forget to set up the __init__ method so that it is able to receive
|
|
|
|
wildcard keyword arguments (see example below). This is necessary
|
|
|
|
because of Django's signal API.
|
|
|
|
|
2013-11-14 01:16:14 +01:00
|
|
|
Example:
|
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
class Base(object, metaclass=SignalConnectMetaClass):
|
2013-11-14 01:16:14 +01:00
|
|
|
signal = django.dispatch.Signal()
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2013-11-14 01:16:14 +01:00
|
|
|
@classmethod
|
|
|
|
def get_dispatch_uid(cls):
|
|
|
|
if not cls.__name__ == 'Base':
|
|
|
|
return cls.__name__
|
|
|
|
|
|
|
|
class Child(Base):
|
2015-02-18 01:45:39 +01:00
|
|
|
pass
|
2013-11-14 01:16:14 +01:00
|
|
|
|
|
|
|
child = Base.get_all(request)[0]
|
|
|
|
assert Child == type(child)
|
|
|
|
"""
|
|
|
|
def __new__(metaclass, class_name, class_parents, class_attributes):
|
|
|
|
"""
|
2013-12-09 23:56:01 +01:00
|
|
|
Creates the class and connects it to the signal if so. Adds all
|
|
|
|
default attributes and methods.
|
2013-11-14 01:16:14 +01:00
|
|
|
"""
|
2015-02-18 01:45:39 +01:00
|
|
|
class_attributes['get_all'] = get_all
|
2017-08-24 12:26:55 +02:00
|
|
|
new_class = super().__new__(
|
2013-11-14 01:16:14 +01:00
|
|
|
metaclass, class_name, class_parents, class_attributes)
|
|
|
|
try:
|
|
|
|
dispatch_uid = new_class.get_dispatch_uid()
|
|
|
|
except AttributeError:
|
|
|
|
raise NotImplementedError('Your class %s must have a get_dispatch_uid classmethod.' % class_name)
|
|
|
|
if dispatch_uid is not None:
|
|
|
|
try:
|
|
|
|
signal = new_class.signal
|
|
|
|
except AttributeError:
|
|
|
|
raise NotImplementedError('Your class %s must have a signal argument, which must be a Django Signal instance.' % class_name)
|
|
|
|
else:
|
|
|
|
signal.connect(new_class, dispatch_uid=dispatch_uid)
|
2013-12-09 23:56:01 +01:00
|
|
|
attributes = {'check_permission': check_permission,
|
|
|
|
'get_default_weight': get_default_weight,
|
|
|
|
'default_weight': 0}
|
|
|
|
for name, attribute in attributes.items():
|
|
|
|
if not hasattr(new_class, name):
|
|
|
|
setattr(new_class, name, attribute)
|
2013-11-14 01:16:14 +01:00
|
|
|
return new_class
|
|
|
|
|
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
@classmethod # type: ignore
|
2015-02-18 01:45:39 +01:00
|
|
|
def get_all(cls, request=None):
|
2013-11-14 01:16:14 +01:00
|
|
|
"""
|
|
|
|
Collects all objects of the class created by the SignalConnectMetaClass
|
2013-12-09 23:56:01 +01:00
|
|
|
from all apps via signal. They are sorted using the get_default_weight
|
|
|
|
method. Does not return objects where check_permission returns False.
|
2013-11-14 01:16:14 +01:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
A django.http.HttpRequest object can optionally be given.
|
2013-11-14 01:16:14 +01:00
|
|
|
|
2013-12-03 01:50:10 +01:00
|
|
|
This classmethod is added as get_all classmethod to every class using
|
|
|
|
the SignalConnectMetaClass.
|
2013-11-14 01:16:14 +01:00
|
|
|
"""
|
2015-02-18 01:45:39 +01:00
|
|
|
kwargs = {'sender': cls}
|
|
|
|
if request is not None:
|
|
|
|
kwargs['request'] = request
|
|
|
|
all_objects = [obj for __, obj in cls.signal.send(**kwargs) if obj.check_permission()]
|
2013-12-09 23:56:01 +01:00
|
|
|
all_objects.sort(key=lambda obj: obj.get_default_weight())
|
2013-11-14 01:16:14 +01:00
|
|
|
return all_objects
|
|
|
|
|
|
|
|
|
|
|
|
def check_permission(self):
|
|
|
|
"""
|
|
|
|
Returns True by default. Override this to filter some children on runtime.
|
|
|
|
|
|
|
|
This method is added to every instance of classes using the
|
|
|
|
SignalConnectMetaClass.
|
|
|
|
"""
|
|
|
|
return True
|
2013-12-09 23:56:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_default_weight(self):
|
|
|
|
"""
|
|
|
|
Returns the value of the default_weight attribute by default. Override
|
|
|
|
this to sort some children on runtime.
|
|
|
|
|
|
|
|
This method is added to every instance of classes using the
|
|
|
|
SignalConnectMetaClass.
|
|
|
|
"""
|
|
|
|
return self.default_weight
|