15
15
)
16
16
17
17
from auditlog .conf import settings
18
+ from auditlog .receivers import log_access , log_create , log_delete , log_update
18
19
from auditlog .signals import accessed
19
20
20
21
DispatchUID = tuple [int , int , int ]
@@ -31,34 +32,11 @@ class AuditlogModelRegistry:
31
32
32
33
DEFAULT_EXCLUDE_MODELS = ("auditlog.LogEntry" , "admin.LogEntry" )
33
34
34
- def __init__ (
35
- self ,
36
- create : bool = True ,
37
- update : bool = True ,
38
- delete : bool = True ,
39
- access : bool = True ,
40
- m2m : bool = True ,
41
- custom : Optional [dict [ModelSignal , Callable ]] = None ,
42
- ):
43
- from auditlog .receivers import log_access , log_create , log_delete , log_update
44
-
35
+ def __init__ (self ):
45
36
self ._registry = {}
46
37
self ._signals = {}
47
38
self ._m2m_signals = defaultdict (dict )
48
39
49
- if create :
50
- self ._signals [post_save ] = log_create
51
- if update :
52
- self ._signals [pre_save ] = log_update
53
- if delete :
54
- self ._signals [post_delete ] = log_delete
55
- if access :
56
- self ._signals [accessed ] = log_access
57
- self ._m2m = m2m
58
-
59
- if custom is not None :
60
- self ._signals .update (custom )
61
-
62
40
def register (
63
41
self ,
64
42
model : ModelBase = None ,
@@ -70,6 +48,8 @@ def register(
70
48
serialize_data : bool = False ,
71
49
serialize_kwargs : Optional [dict [str , Any ]] = None ,
72
50
serialize_auditlog_fields_only : bool = False ,
51
+ actions : Optional [dict [str , bool ]] = None ,
52
+ custom : Optional [dict [ModelSignal , Callable ]] = None ,
73
53
):
74
54
"""
75
55
Register a model with auditlog. Auditlog will then track mutations on this model's instances.
@@ -81,10 +61,31 @@ def register(
81
61
:param mask_fields: The fields to mask for sensitive info.
82
62
:param m2m_fields: The fields to handle as many to many.
83
63
:param serialize_data: Option to include a dictionary of the objects state in the auditlog.
84
- :param serialize_kwargs: Optional kwargs to pass to Django serializer
64
+ :param serialize_kwargs: Optional kwargs to pass to Django serializer.
85
65
:param serialize_auditlog_fields_only: Only fields being considered in changes will be serialized.
66
+ :param actions: Enble log entry on create, update, delete, access and m2m fields.
67
+ :param custom: Configure a custom signal when register.
86
68
"""
87
69
70
+ actions = actions or {}
71
+ create = actions .get ("create" , True )
72
+ update = actions .get ("update" , True )
73
+ delete = actions .get ("delete" , True )
74
+ access = actions .get ("access" , True )
75
+ m2m = actions .get ("m2m" , True )
76
+
77
+ if create :
78
+ self ._signals [post_save ] = log_create
79
+ if update :
80
+ self ._signals [pre_save ] = log_update
81
+ if delete :
82
+ self ._signals [post_delete ] = log_delete
83
+ if access :
84
+ self ._signals [accessed ] = log_access
85
+
86
+ if custom is not None :
87
+ self ._signals .update (custom )
88
+
88
89
if include_fields is None :
89
90
include_fields = []
90
91
if exclude_fields is None :
@@ -122,7 +123,7 @@ def registrar(cls):
122
123
"serialize_kwargs" : serialize_kwargs ,
123
124
"serialize_auditlog_fields_only" : serialize_auditlog_fields_only ,
124
125
}
125
- self ._connect_signals (cls )
126
+ self ._connect_signals (cls , m2m = m2m )
126
127
127
128
# We need to return the class, as the decorator is basically
128
129
# syntactic sugar for:
@@ -180,7 +181,7 @@ def get_serialize_options(self, model: ModelBase):
180
181
),
181
182
}
182
183
183
- def _connect_signals (self , model ):
184
+ def _connect_signals (self , model , m2m : bool = False ):
184
185
"""
185
186
Connect signals for the model.
186
187
"""
@@ -192,7 +193,7 @@ def _connect_signals(self, model):
192
193
sender = model ,
193
194
dispatch_uid = self ._dispatch_uid (signal , receiver ),
194
195
)
195
- if self . _m2m :
196
+ if m2m :
196
197
for field_name in self ._registry [model ]["m2m_fields" ]:
197
198
receiver = make_log_m2m_changes (field_name )
198
199
self ._m2m_signals [model ][field_name ] = receiver
0 commit comments