You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 lines
3.2 KiB

  1. from __future__ import unicode_literals
  2. from django.conf import settings
  3. from django.contrib.admin.utils import quote
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.core.urlresolvers import NoReverseMatch, reverse
  6. from django.db import models
  7. from django.utils import timezone
  8. from django.utils.encoding import python_2_unicode_compatible, smart_text
  9. from django.utils.translation import ugettext, ugettext_lazy as _
  10. ADDITION = 1
  11. CHANGE = 2
  12. DELETION = 3
  13. class LogEntryManager(models.Manager):
  14. use_in_migrations = True
  15. def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
  16. self.model.objects.create(
  17. user_id=user_id,
  18. content_type_id=content_type_id,
  19. object_id=smart_text(object_id),
  20. object_repr=object_repr[:200],
  21. action_flag=action_flag,
  22. change_message=change_message,
  23. )
  24. @python_2_unicode_compatible
  25. class LogEntry(models.Model):
  26. action_time = models.DateTimeField(
  27. _('action time'),
  28. default=timezone.now,
  29. editable=False,
  30. )
  31. user = models.ForeignKey(
  32. settings.AUTH_USER_MODEL,
  33. models.CASCADE,
  34. verbose_name=_('user'),
  35. )
  36. content_type = models.ForeignKey(
  37. ContentType,
  38. models.SET_NULL,
  39. verbose_name=_('content type'),
  40. blank=True, null=True,
  41. )
  42. object_id = models.TextField(_('object id'), blank=True, null=True)
  43. object_repr = models.CharField(_('object repr'), max_length=200)
  44. action_flag = models.PositiveSmallIntegerField(_('action flag'))
  45. change_message = models.TextField(_('change message'), blank=True)
  46. objects = LogEntryManager()
  47. class Meta:
  48. verbose_name = _('log entry')
  49. verbose_name_plural = _('log entries')
  50. db_table = 'django_admin_log'
  51. ordering = ('-action_time',)
  52. def __repr__(self):
  53. return smart_text(self.action_time)
  54. def __str__(self):
  55. if self.is_addition():
  56. return ugettext('Added "%(object)s".') % {'object': self.object_repr}
  57. elif self.is_change():
  58. return ugettext('Changed "%(object)s" - %(changes)s') % {
  59. 'object': self.object_repr,
  60. 'changes': self.change_message,
  61. }
  62. elif self.is_deletion():
  63. return ugettext('Deleted "%(object)s."') % {'object': self.object_repr}
  64. return ugettext('LogEntry Object')
  65. def is_addition(self):
  66. return self.action_flag == ADDITION
  67. def is_change(self):
  68. return self.action_flag == CHANGE
  69. def is_deletion(self):
  70. return self.action_flag == DELETION
  71. def get_edited_object(self):
  72. "Returns the edited object represented by this log entry"
  73. return self.content_type.get_object_for_this_type(pk=self.object_id)
  74. def get_admin_url(self):
  75. """
  76. Returns the admin URL to edit the object represented by this log entry.
  77. """
  78. if self.content_type and self.object_id:
  79. url_name = 'admin:%s_%s_change' % (self.content_type.app_label, self.content_type.model)
  80. try:
  81. return reverse(url_name, args=(quote(self.object_id),))
  82. except NoReverseMatch:
  83. pass
  84. return None