1
|
6
|
from django.db.models.signals import pre_save, post_save, pre_delete, m2m_changed
|
2
|
|
|
3
|
|
|
4
|
6
|
class ActivityStreamRegistrar(object):
|
5
|
|
|
6
|
6
|
def __init__(self):
|
7
|
6
|
self.models = []
|
8
|
|
|
9
|
6
|
def connect(self, model):
|
10
|
|
# Always register model; the signal handlers will check if activity stream is enabled.
|
11
|
6
|
from cyborgbackup.main.signals import (activity_stream_create, activity_stream_update,
|
12
|
|
activity_stream_delete, activity_stream_associate)
|
13
|
|
|
14
|
6
|
if model not in self.models:
|
15
|
6
|
self.models.append(model)
|
16
|
6
|
post_save.connect(activity_stream_create,
|
17
|
|
sender=model,
|
18
|
|
dispatch_uid=str(self.__class__) + str(model) + "_create")
|
19
|
6
|
pre_save.connect(activity_stream_update,
|
20
|
|
sender=model,
|
21
|
|
dispatch_uid=str(self.__class__) + str(model) + "_update")
|
22
|
6
|
pre_delete.connect(activity_stream_delete,
|
23
|
|
sender=model,
|
24
|
|
dispatch_uid=str(self.__class__) + str(model) + "_delete")
|
25
|
|
|
26
|
6
|
for m2mfield in model._meta.many_to_many:
|
27
|
6
|
try:
|
28
|
6
|
m2m_attr = getattr(model, m2mfield.name)
|
29
|
6
|
m2m_changed.connect(activity_stream_associate, sender=m2m_attr.through,
|
30
|
|
dispatch_uid=str(self.__class__) + str(m2m_attr.through) + "_associate")
|
31
|
0
|
except AttributeError:
|
32
|
0
|
pass
|
33
|
|
|
34
|
6
|
def disconnect(self, model):
|
35
|
0
|
if model in self.models:
|
36
|
0
|
post_save.disconnect(dispatch_uid=str(self.__class__) + str(model) + "_create")
|
37
|
0
|
pre_save.disconnect(dispatch_uid=str(self.__class__) + str(model) + "_update")
|
38
|
0
|
pre_delete.disconnect(dispatch_uid=str(self.__class__) + str(model) + "_delete")
|
39
|
0
|
self.models.pop(model)
|
40
|
|
|
41
|
0
|
for m2mfield in model._meta.many_to_many:
|
42
|
0
|
m2m_attr = getattr(model, m2mfield.name)
|
43
|
0
|
m2m_changed.disconnect(dispatch_uid=str(self.__class__) + str(m2m_attr.through) + "_associate")
|
44
|
|
|
45
|
|
|
46
|
6
|
activity_stream_registrar = ActivityStreamRegistrar()
|