1 | from django.db import models
|
---|
2 | from django.contrib.contenttypes.models import ContentType
|
---|
3 | from django.contrib.auth.models import User
|
---|
4 | from django.utils.translation import gettext_lazy as _
|
---|
5 |
|
---|
6 | ADDITION = 1
|
---|
7 | CHANGE = 2
|
---|
8 | DELETION = 3
|
---|
9 |
|
---|
10 | class LogEntryManager(models.Manager):
|
---|
11 | def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
|
---|
12 | e = self.model(None, None, user_id, content_type_id, object_id, object_repr[:200], action_flag, change_message)
|
---|
13 | e.save()
|
---|
14 |
|
---|
15 | class LogEntry(models.Model):
|
---|
16 | action_time = models.DateTimeField(_('action time'), auto_now=True)
|
---|
17 | user = models.ForeignKey(User)
|
---|
18 | content_type = models.ForeignKey(ContentType, blank=True, null=True)
|
---|
19 | object_id = models.IntegerField(_('object id'), blank=True, null=True)
|
---|
20 | object_repr = models.CharField(_('object repr'), maxlength=200)
|
---|
21 | action_flag = models.PositiveSmallIntegerField(_('action flag'))
|
---|
22 | change_message = models.TextField(_('change message'), blank=True)
|
---|
23 | objects = LogEntryManager()
|
---|
24 | class Meta:
|
---|
25 | verbose_name = _('log entry')
|
---|
26 | verbose_name_plural = _('log entries')
|
---|
27 | db_table = 'django_admin_log'
|
---|
28 | ordering = ('-action_time',)
|
---|
29 |
|
---|
30 | def __repr__(self):
|
---|
31 | return str(self.action_time)
|
---|
32 |
|
---|
33 | def is_addition(self):
|
---|
34 | return self.action_flag == ADDITION
|
---|
35 |
|
---|
36 | def is_change(self):
|
---|
37 | return self.action_flag == CHANGE
|
---|
38 |
|
---|
39 | def is_deletion(self):
|
---|
40 | return self.action_flag == DELETION
|
---|
41 |
|
---|
42 | def get_edited_object(self):
|
---|
43 | "Returns the edited object represented by this log entry"
|
---|
44 | return self.content_type.get_object_for_this_type(pk=self.object_id)
|
---|
45 |
|
---|
46 | def get_admin_url(self):
|
---|
47 | """
|
---|
48 | Returns the admin URL to edit the object represented by this log entry.
|
---|
49 | This is relative to the Django admin index page.
|
---|
50 | """
|
---|
51 | return "%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, self.object_id)
|
---|