mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Merge pull request #3 from vfigueiro/master
Ensure set_actor signal disconnection
This commit is contained in:
commit
6c7990c9a2
1 changed files with 13 additions and 2 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import time
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models.signals import pre_save
|
||||
from django.utils.functional import curry
|
||||
|
|
@ -21,18 +23,27 @@ class AuditlogMiddleware(object):
|
|||
else:
|
||||
user = None
|
||||
|
||||
request.auditlog_ts = time.time()
|
||||
set_actor = curry(self.set_actor, user)
|
||||
pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=(self.__class__, request), weak=False)
|
||||
pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=(self.__class__, request.auditlog_ts), weak=False)
|
||||
|
||||
def process_response(self, request, response):
|
||||
"""
|
||||
Disconnects the signal receiver to prevent it from staying active.
|
||||
"""
|
||||
# Disconnecting the signal receiver is required because it will not be garbage collected (non-weak reference)
|
||||
pre_save.disconnect(dispatch_uid=(self.__class__, request))
|
||||
pre_save.disconnect(sender=LogEntry, dispatch_uid=(self.__class__, request.auditlog_ts))
|
||||
|
||||
return response
|
||||
|
||||
def process_exception(self, request, exception):
|
||||
"""
|
||||
Disconnects the signal receiver to prevent it from staying active in case of an exception.
|
||||
"""
|
||||
pre_save.disconnect(sender=LogEntry, dispatch_uid=(self.__class__, request.auditlog_ts))
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def set_actor(user, sender, instance, **kwargs):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue