Extend inline documentation

This commit is contained in:
Jan-Jelle Kester 2013-11-06 20:48:16 +01:00
parent 9bfc316097
commit 1b2503d371
3 changed files with 16 additions and 3 deletions

View file

@ -11,6 +11,10 @@ class AuditLogMiddleware(object):
"""
def process_request(self, request):
"""
Gets the current user from the request and prepares and connects a signal receiver with the user already
attached to it.
"""
if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated():
user = request.user
else:
@ -20,9 +24,18 @@ class AuditLogMiddleware(object):
pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=(self.__class__, request), 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))
return response
def set_actor(self, user, sender, instance, **kwargs):
"""
Signal receiver with an extra, required 'user' kwarg. This method becomes a real (valid) signal receiver when
it is curried with the actor.
"""
if sender == LogEntry and isinstance(user, settings.AUTH_USER_MODEL) and instance.actor is None:
instance.actor = user

View file

@ -30,8 +30,8 @@ class LogEntryManager(models.Manager):
if isinstance(pk, int):
kwargs['object_id'] = pk
# Delete log entries with the same pk as a newly created model. This should only happen when all records were
# deleted / the table was truncated.
# Delete log entries with the same pk as a newly created model. This should only be necessary when an pk is
# used twice.
if kwargs.get('action', None) is LogEntry.Action.CREATE:
if kwargs.get('object_id', None) is not None and self.filter(content_type=kwargs.get('content_type'), object_id=kwargs.get('object_id')).exists():
self.filter(content_type=kwargs.get('content_type'), object_id=kwargs.get('object_id')).delete()

View file

@ -5,7 +5,7 @@ from auditlog.receivers import log_create, log_update, log_delete
class AuditLogModelRegistry(object):
"""
A registry that keeps track of the models that use auditlog.
A registry that keeps track of the models that use Auditlog to track changes.
"""
def __init__(self, create=True, update=True, delete=True, custom=None):