-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmodel_signals.py
348 lines (293 loc) · 14.1 KB
/
model_signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import json
import logging
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.contrib.contenttypes.models import ContentType
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
from django.db import transaction
from django.db.models import signals
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.module_loading import import_string
from easyaudit.middleware.easyaudit import get_current_request, \
get_current_user
from easyaudit.models import CRUDEvent
from easyaudit.settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, \
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS, LOGGING_BACKEND, \
DATABASE_ALIAS
from easyaudit.utils import get_m2m_field_name, model_delta
logger = logging.getLogger(__name__)
audit_logger = import_string(LOGGING_BACKEND)()
def should_audit(instance):
"""Returns True or False to indicate whether the instance
should be audited or not, depending on the project settings."""
# do not audit any model listed in UNREGISTERED_CLASSES
for unregistered_class in UNREGISTERED_CLASSES:
if isinstance(instance, unregistered_class):
return False
# only audit models listed in REGISTERED_CLASSES (if it's set)
if len(REGISTERED_CLASSES) > 0:
for registered_class in REGISTERED_CLASSES:
if isinstance(instance, registered_class):
break
else:
return False
# all good
return True
def get_current_user_details():
user_id = None
user_pk_as_string = None
try:
user = get_current_user()
if user and not isinstance(user, AnonymousUser):
if getattr(settings, "DJANGO_EASY_AUDIT_CHECK_IF_REQUEST_USER_EXISTS", True):
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
user_id, user_pk_as_string = user.id, str(user.pk)
except:
pass
return user_id, user_pk_as_string
# signals
def pre_save(sender, instance, raw, using, update_fields, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-save"""
if raw:
# Return if loading Fixtures
return
try:
if not should_audit(instance):
return False
with transaction.atomic(using=using):
try:
object_json_repr = serializers.serialize("json", [instance])
except Exception:
# We need a better way for this to work. ManyToMany will fail on pre_save on create
return None
# Determine if the instance is a create
created = instance.pk is None or instance._state.adding
# created or updated?
if not created:
old_model = sender.objects.get(pk=instance.pk)
delta = model_delta(old_model, instance)
if not delta and getattr(settings, "DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP", False):
return False
changed_fields = json.dumps(delta)
event_type = CRUDEvent.UPDATE
# user
user_id, user_pk_as_string = get_current_user_details()
# callbacks
kwargs['request'] = get_current_request() # make request available for callbacks
create_crud_event = all(
callback(instance, object_json_repr, created, raw, using, update_fields, **kwargs)
for callback in CRUD_DIFFERENCE_CALLBACKS if callable(callback))
# create crud event only if all callbacks returned True
if create_crud_event and not created:
c_t = ContentType.objects.get_for_model(instance)
def crud_flow():
try:
# atomicity based on the easyaudit database alias
with transaction.atomic(using=DATABASE_ALIAS):
crud_event = audit_logger.crud({
'event_type': event_type,
'object_repr': str(instance),
'object_json_repr': object_json_repr,
'changed_fields': changed_fields,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': user_pk_as_string,
})
except Exception as e:
try:
logger.exception(
"easy audit had a pre_save exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
except Exception:
pass
if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a pre-save exception.')
def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-save"""
if raw:
# Return if loading Fixtures
return
try:
if not should_audit(instance):
return False
with transaction.atomic(using=using):
object_json_repr = serializers.serialize("json", [instance])
# created or updated?
if created:
event_type = CRUDEvent.CREATE
# user
user_id, user_pk_as_string = get_current_user_details()
# callbacks
kwargs['request'] = get_current_request() # make request available for callbacks
create_crud_event = all(callback(instance, object_json_repr,
created, raw, using, update_fields, **kwargs)
for callback in CRUD_DIFFERENCE_CALLBACKS
if callable(callback))
# create crud event only if all callbacks returned True
if create_crud_event and created:
c_t = ContentType.objects.get_for_model(instance)
def crud_flow():
try:
with transaction.atomic(using=DATABASE_ALIAS):
crud_event = audit_logger.crud({
'event_type': event_type,
'object_repr': str(instance),
'object_json_repr': object_json_repr,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': user_pk_as_string
})
except Exception as e:
try:
logger.exception(
"easy audit had a post_save exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
except Exception:
pass
if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a post-save exception.')
def _m2m_rev_field_name(model1, model2):
"""Gets the name of the reverse m2m accessor from `model1` to `model2`
For example, if User has a ManyToManyField connected to Group,
`_m2m_rev_field_name(Group, User)` retrieves the name of the field on
Group that lists a group's Users. (By default, this field is called
`user_set`, but the name can be overridden).
"""
m2m_field_names = [
rel.get_accessor_name() for rel in model1._meta.get_fields()
if rel.many_to_many
and rel.auto_created
and rel.related_model == model2
]
return m2m_field_names[0]
def m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#m2m-changed"""
try:
if not should_audit(instance):
return False
if action not in ("post_add", "post_remove", "post_clear"):
return False
with transaction.atomic(using=using):
object_json_repr = serializers.serialize("json", [instance])
if reverse:
if action == 'post_add':
event_type = CRUDEvent.M2M_ADD_REV
elif action == 'post_remove':
event_type = CRUDEvent.M2M_REMOVE_REV
elif action == 'post_clear':
event_type = CRUDEvent.M2M_CLEAR_REV
else:
event_type = CRUDEvent.M2M_CHANGE_REV # just in case
# add reverse M2M changes to event. must use json lib because
# django serializers ignore extra fields.
tmp_repr = json.loads(object_json_repr)
m2m_rev_field = _m2m_rev_field_name(instance._meta.concrete_model, model)
related_instances = getattr(instance, m2m_rev_field).all()
related_ids = [r.pk for r in related_instances]
tmp_repr[0]['m2m_rev_model'] = force_str(model._meta)
tmp_repr[0]['m2m_rev_pks'] = related_ids
tmp_repr[0]['m2m_rev_action'] = action
object_json_repr = json.dumps(tmp_repr, cls=DjangoJSONEncoder)
else:
if action == 'post_add':
event_type = CRUDEvent.M2M_ADD
elif action == 'post_remove':
event_type = CRUDEvent.M2M_REMOVE
elif action == 'post_clear':
event_type = CRUDEvent.M2M_CLEAR
else:
event_type = CRUDEvent.M2M_CHANGE # just in case
# user
user_id, user_pk_as_string = get_current_user_details()
c_t = ContentType.objects.get_for_model(instance)
def crud_flow():
try:
if action == "post_clear":
changed_fields = []
else:
changed_fields = json.dumps({get_m2m_field_name(model, instance): list(pk_set)}, cls=DjangoJSONEncoder)
with transaction.atomic(using=DATABASE_ALIAS):
crud_event = audit_logger.crud({
'event_type': event_type,
'object_repr': str(instance),
'object_json_repr': object_json_repr,
'changed_fields': changed_fields,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': user_pk_as_string
})
except Exception as e:
try:
logger.exception(
"easy audit had a m2m_changed exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
except Exception:
pass
if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had an m2m-changed exception.')
def post_delete(sender, instance, using, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-delete"""
try:
if not should_audit(instance):
return False
with transaction.atomic(using=using):
object_json_repr = serializers.serialize("json", [instance])
# user
user_id, user_pk_as_string = get_current_user_details()
c_t = ContentType.objects.get_for_model(instance)
# object id to be used later
obj_id = instance.pk
def crud_flow():
try:
with transaction.atomic(using=DATABASE_ALIAS):
# crud event
crud_event = audit_logger.crud({
'event_type': CRUDEvent.DELETE,
'object_repr': str(instance),
'object_json_repr': object_json_repr,
'content_type_id': c_t.id,
'object_id': obj_id,
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': user_pk_as_string
})
except Exception as e:
try:
logger.exception(
"easy audit had a post_delete exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
except Exception:
pass
if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a post-delete exception.')
if WATCH_MODEL_EVENTS:
signals.post_save.connect(post_save, dispatch_uid='easy_audit_signals_post_save')
signals.pre_save.connect(pre_save, dispatch_uid='easy_audit_signals_pre_save')
signals.m2m_changed.connect(m2m_changed, dispatch_uid='easy_audit_signals_m2m_changed')
signals.post_delete.connect(post_delete, dispatch_uid='easy_audit_signals_post_delete')