2013-05-30 10:33:00 +00:00
|
|
|
""" Boilerplate for now, will serve a purpose soon! """
|
2013-07-18 20:46:00 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
from django.db import models
|
2020-09-26 21:21:43 +00:00
|
|
|
from django.utils.encoding import force_str
|
2021-03-09 11:44:05 +00:00
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
2013-07-07 14:13:48 +00:00
|
|
|
|
2013-08-02 10:52:38 +00:00
|
|
|
from .utils import quote
|
2013-05-31 14:33:28 +00:00
|
|
|
|
|
|
|
|
|
2013-07-18 20:46:00 +00:00
|
|
|
class LogEntryManager(models.Manager):
|
2013-07-18 20:59:08 +00:00
|
|
|
def log_action(self, user_id, obj, action_flag, change_message=''):
|
|
|
|
|
content_type_id = ContentType.objects.get_for_model(obj).id
|
2013-07-18 20:46:00 +00:00
|
|
|
e = self.model(None, None, user_id, content_type_id,
|
2021-03-09 11:33:11 +00:00
|
|
|
force_str(obj.id), force_str(obj)[:200],
|
2013-07-18 20:59:08 +00:00
|
|
|
action_flag, change_message)
|
2013-07-18 20:46:00 +00:00
|
|
|
e.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogEntry(models.Model):
|
|
|
|
|
ADDITION = 1
|
|
|
|
|
CHANGE = 2
|
|
|
|
|
DELETION = 3
|
|
|
|
|
|
|
|
|
|
action_time = models.DateTimeField(_('action time'), auto_now=True)
|
2013-07-18 20:59:08 +00:00
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL,
|
2018-05-10 17:58:25 +00:00
|
|
|
related_name='log_entries',
|
|
|
|
|
on_delete=models.CASCADE)
|
2013-07-18 20:59:08 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, blank=True, null=True,
|
2018-05-10 17:58:25 +00:00
|
|
|
related_name='log_entries',
|
|
|
|
|
on_delete=models.CASCADE)
|
2013-07-18 20:46:00 +00:00
|
|
|
object_id = models.TextField(_('object id'), blank=True, null=True)
|
|
|
|
|
object_repr = models.CharField(_('object repr'), max_length=200)
|
|
|
|
|
action_flag = models.PositiveSmallIntegerField(_('action flag'))
|
|
|
|
|
change_message = models.TextField(_('change message'), blank=True)
|
|
|
|
|
|
|
|
|
|
objects = LogEntryManager()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _('log entry')
|
|
|
|
|
verbose_name_plural = _('log entries')
|
|
|
|
|
ordering = ('-action_time',)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-03-09 11:33:11 +00:00
|
|
|
return force_str(self.action_time)
|
2013-07-18 20:46:00 +00:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
if self.action_flag == self.ADDITION:
|
2021-03-09 11:44:05 +00:00
|
|
|
return gettext('Added "%(object)s".') % {
|
2013-07-18 20:46:00 +00:00
|
|
|
'object': self.object_repr}
|
|
|
|
|
elif self.action_flag == self.CHANGE:
|
2021-03-09 11:44:05 +00:00
|
|
|
return gettext('Changed "%(object)s" - %(changes)s') % {
|
2013-07-18 20:46:00 +00:00
|
|
|
'object': self.object_repr,
|
|
|
|
|
'changes': self.change_message,
|
|
|
|
|
}
|
|
|
|
|
elif self.action_flag == self.DELETION:
|
2021-03-09 11:44:05 +00:00
|
|
|
return gettext('Deleted "%(object)s."') % {
|
2013-07-18 20:46:00 +00:00
|
|
|
'object': self.object_repr}
|
|
|
|
|
|
2021-03-09 11:44:05 +00:00
|
|
|
return gettext('LogEntry Object')
|
2013-07-18 20:46:00 +00:00
|
|
|
|
|
|
|
|
def is_addition(self):
|
|
|
|
|
return self.action_flag == self.ADDITION
|
|
|
|
|
|
|
|
|
|
def is_change(self):
|
|
|
|
|
return self.action_flag == self.CHANGE
|
|
|
|
|
|
|
|
|
|
def is_deletion(self):
|
|
|
|
|
return self.action_flag == self.DELETION
|
|
|
|
|
|
2013-07-19 00:31:08 +00:00
|
|
|
@property
|
|
|
|
|
def action_type(self):
|
2013-07-19 00:33:19 +00:00
|
|
|
if self.is_addition():
|
2013-07-19 00:31:08 +00:00
|
|
|
return _('added')
|
2013-07-19 00:33:19 +00:00
|
|
|
if self.is_change():
|
2013-07-19 00:31:08 +00:00
|
|
|
return _('changed')
|
|
|
|
|
if self.is_deletion():
|
|
|
|
|
return _('deleted')
|
|
|
|
|
return ''
|
|
|
|
|
|
2013-07-18 20:46:00 +00:00
|
|
|
def get_edited_object(self):
|
|
|
|
|
"Returns the edited object represented by this log entry"
|
|
|
|
|
return self.content_type.get_object_for_this_type(pk=self.object_id)
|
|
|
|
|
|
|
|
|
|
def get_admin_url(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the admin URL to edit the object represented by this log entry.
|
|
|
|
|
This is relative to the Django admin index page.
|
|
|
|
|
"""
|
|
|
|
|
if self.content_type and self.object_id:
|
|
|
|
|
return '{0.app_label}/{0.model}/{1}'.format(
|
|
|
|
|
self.content_type,
|
|
|
|
|
quote(self.object_id)
|
|
|
|
|
)
|
|
|
|
|
return None
|