Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / django / contrib / admin / models.py @ 1a305335

History | View | Annotate | Download (2.61 KB)

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.contrib.admin.util import quote
5
from django.utils.translation import ugettext_lazy as _
6
from django.utils.encoding import smart_unicode
7
from django.utils.safestring import mark_safe
8

    
9
ADDITION = 1
10
CHANGE = 2
11
DELETION = 3
12

    
13
class LogEntryManager(models.Manager):
14
    def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
15
        e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message)
16
        e.save()
17

    
18
class LogEntry(models.Model):
19
    action_time = models.DateTimeField(_('action time'), auto_now=True)
20
    user = models.ForeignKey(User)
21
    content_type = models.ForeignKey(ContentType, blank=True, null=True)
22
    object_id = models.TextField(_('object id'), blank=True, null=True)
23
    object_repr = models.CharField(_('object repr'), max_length=200)
24
    action_flag = models.PositiveSmallIntegerField(_('action flag'))
25
    change_message = models.TextField(_('change message'), blank=True)
26

    
27
    objects = LogEntryManager()
28

    
29
    class Meta:
30
        verbose_name = _('log entry')
31
        verbose_name_plural = _('log entries')
32
        db_table = 'django_admin_log'
33
        ordering = ('-action_time',)
34

    
35
    def __repr__(self):
36
        return smart_unicode(self.action_time)
37

    
38
    def __unicode__(self):
39
        if self.action_flag == ADDITION:
40
            return _('Added "%(object)s".') % {'object': self.object_repr}
41
        elif self.action_flag == CHANGE:
42
            return _('Changed "%(object)s" - %(changes)s') % {'object': self.object_repr, 'changes': self.change_message}
43
        elif self.action_flag == DELETION:
44
            return _('Deleted "%(object)s."') % {'object': self.object_repr}
45

    
46
        return _('LogEntry Object')
47

    
48
    def is_addition(self):
49
        return self.action_flag == ADDITION
50

    
51
    def is_change(self):
52
        return self.action_flag == CHANGE
53

    
54
    def is_deletion(self):
55
        return self.action_flag == DELETION
56

    
57
    def get_edited_object(self):
58
        "Returns the edited object represented by this log entry"
59
        return self.content_type.get_object_for_this_type(pk=self.object_id)
60

    
61
    def get_admin_url(self):
62
        """
63
        Returns the admin URL to edit the object represented by this log entry.
64
        This is relative to the Django admin index page.
65
        """
66
        if self.content_type and self.object_id:
67
            return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, quote(self.object_id)))
68
        return None