PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 53ms app.codeStats 1ms

/django/contrib/admin/models.py

https://code.google.com/p/mango-py/
Python | 56 lines | 54 code | 2 blank | 0 comment | 0 complexity | 883e38ce7ad8e6accfd3dd1038ef5b31 MD5 | raw file
 1from django.db import models
 2from django.contrib.contenttypes.models import ContentType
 3from django.contrib.auth.models import User
 4from django.contrib.admin.util import quote
 5from django.utils.translation import ugettext_lazy as _
 6from django.utils.encoding import smart_unicode
 7from django.utils.safestring import mark_safe
 8
 9ADDITION = 1
10CHANGE = 2
11DELETION = 3
12
13class 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
18class 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    objects = LogEntryManager()
27    class Meta:
28        verbose_name = _('log entry')
29        verbose_name_plural = _('log entries')
30        db_table = 'django_admin_log'
31        ordering = ('-action_time',)
32
33    def __repr__(self):
34        return smart_unicode(self.action_time)
35
36    def is_addition(self):
37        return self.action_flag == ADDITION
38
39    def is_change(self):
40        return self.action_flag == CHANGE
41
42    def is_deletion(self):
43        return self.action_flag == DELETION
44
45    def get_edited_object(self):
46        "Returns the edited object represented by this log entry"
47        return self.content_type.get_object_for_this_type(pk=self.object_id)
48
49    def get_admin_url(self):
50        """
51        Returns the admin URL to edit the object represented by this log entry.
52        This is relative to the Django admin index page.
53        """
54        if self.content_type and self.object_id:
55            return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, quote(self.object_id)))
56        return None