django-auditlog/src/auditlog/registry.py

107 lines
3.4 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-10-20 13:25:48 +00:00
from django.db.models.signals import pre_save, post_save, post_delete
from django.db.models import Model
class AuditlogModelRegistry(object):
2013-10-20 13:25:48 +00:00
"""
2013-11-06 19:48:16 +00:00
A registry that keeps track of the models that use Auditlog to track changes.
2013-10-20 13:25:48 +00:00
"""
def __init__(self, create=True, update=True, delete=True, custom=None):
from auditlog.receivers import log_create, log_update, log_delete
self._registry = {}
2013-10-20 13:25:48 +00:00
self._signals = {}
if create:
self._signals[post_save] = log_create
if update:
self._signals[pre_save] = log_update
if delete:
self._signals[post_delete] = log_delete
if custom is not None:
self._signals.update(custom)
2015-05-14 23:25:44 +00:00
def register(self, model, include_fields=[], exclude_fields=[]):
2013-10-20 13:25:48 +00:00
"""
Register a model with auditlog. Auditlog will then track mutations on this model's instances.
2015-05-14 23:25:44 +00:00
:param model: The model to register.
:type model: Model
:param include_fields: The fields to include. Implicitly excludes all other fields.
2015-05-31 13:06:06 +00:00
:type include_fields: list
2015-05-14 23:25:44 +00:00
:param exclude_fields: The fields to exclude. Overrides the fields to include.
2015-05-31 13:06:06 +00:00
:type exclude_fields: list
2013-10-20 13:25:48 +00:00
"""
if issubclass(model, Model):
self._registry[model] = {
2015-05-14 23:25:44 +00:00
'include_fields': include_fields,
'exclude_fields': exclude_fields,
}
2013-10-20 13:25:48 +00:00
self._connect_signals(model)
else:
raise TypeError("Supplied model is not a valid model.")
2013-10-20 13:25:48 +00:00
def contains(self, model):
"""
Check if a model is registered with auditlog.
2015-05-14 23:25:44 +00:00
:param model: The model to check.
:type model: Model
:return: Whether the model has been registered.
:rtype: bool
2013-10-20 13:25:48 +00:00
"""
return model in self._registry
def unregister(self, model):
"""
Unregister a model with auditlog. This will not affect the database.
2015-05-14 23:25:44 +00:00
:param model: The model to unregister.
:type model: Model
2013-10-20 13:25:48 +00:00
"""
try:
del self._registry[model]
2013-10-20 13:25:48 +00:00
except KeyError:
pass
else:
self._disconnect_signals(model)
def _connect_signals(self, model):
"""
Connect signals for the model.
"""
for signal in self._signals:
receiver = self._signals[signal]
2013-10-20 13:25:48 +00:00
signal.connect(receiver, sender=model, dispatch_uid=self._dispatch_uid(signal, model))
def _disconnect_signals(self, model):
"""
Disconnect signals for the model.
"""
for signal, receiver in self._signals.items():
signal.disconnect(sender=model, dispatch_uid=self._dispatch_uid(signal, model))
2013-10-20 13:25:48 +00:00
def _dispatch_uid(self, signal, model):
"""
Generate a dispatch_uid.
"""
return (self.__class__, model, signal)
def get_model_fields(self, model):
return {
'include_fields': self._registry[model]['include_fields'],
'exclude_fields': self._registry[model]['exclude_fields'],
}
2013-10-20 13:25:48 +00:00
class AuditLogModelRegistry(AuditlogModelRegistry):
def __init__(self, *args, **kwargs):
super(AuditLogModelRegistry, self).__init__(*args, **kwargs)
raise DeprecationWarning("Use AuditlogModelRegistry instead of AuditLogModelRegistry, AuditLogModelRegistry will be removed in django-auditlog 0.4.0 or later.")
auditlog = AuditlogModelRegistry()