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