2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.auth.models import User
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.contrib.admin.util import quote
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.utils.encoding import smart_unicode
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
ADDITION = 1
|
|
|
|
CHANGE = 2
|
|
|
|
DELETION = 3
|
|
|
|
|
|
|
|
class LogEntryManager(models.Manager):
|
|
|
|
def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message)
|
2006-05-02 09:31:56 +08:00
|
|
|
e.save()
|
|
|
|
|
|
|
|
class LogEntry(models.Model):
|
|
|
|
action_time = models.DateTimeField(_('action time'), auto_now=True)
|
|
|
|
user = models.ForeignKey(User)
|
|
|
|
content_type = models.ForeignKey(ContentType, blank=True, null=True)
|
|
|
|
object_id = models.TextField(_('object id'), blank=True, null=True)
|
2007-08-05 13:14:46 +08:00
|
|
|
object_repr = models.CharField(_('object repr'), max_length=200)
|
2006-05-02 09:31:56 +08:00
|
|
|
action_flag = models.PositiveSmallIntegerField(_('action flag'))
|
|
|
|
change_message = models.TextField(_('change message'), blank=True)
|
2011-08-12 22:14:39 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
objects = LogEntryManager()
|
2011-08-12 22:14:39 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('log entry')
|
|
|
|
verbose_name_plural = _('log entries')
|
|
|
|
db_table = 'django_admin_log'
|
|
|
|
ordering = ('-action_time',)
|
|
|
|
|
|
|
|
def __repr__(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return smart_unicode(self.action_time)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-04-28 22:54:09 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
if self.action_flag == ADDITION:
|
|
|
|
return _('Added "%(object)s".') % {'object': self.object_repr}
|
|
|
|
elif self.action_flag == CHANGE:
|
|
|
|
return _('Changed "%(object)s" - %(changes)s') % {'object': self.object_repr, 'changes': self.change_message}
|
|
|
|
elif self.action_flag == DELETION:
|
|
|
|
return _('Deleted "%(object)s."') % {'object': self.object_repr}
|
|
|
|
|
2011-05-05 01:36:34 +08:00
|
|
|
return _('LogEntry Object')
|
2011-04-28 22:54:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def is_addition(self):
|
|
|
|
return self.action_flag == ADDITION
|
|
|
|
|
|
|
|
def is_change(self):
|
|
|
|
return self.action_flag == CHANGE
|
|
|
|
|
|
|
|
def is_deletion(self):
|
|
|
|
return self.action_flag == DELETION
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2010-12-23 00:24:30 +08:00
|
|
|
if self.content_type and self.object_id:
|
2012-07-03 07:33:44 +08:00
|
|
|
return "%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, quote(self.object_id))
|
2011-04-28 22:54:09 +08:00
|
|
|
return None
|