From d5bac7e4493e7de1e59c744da33b7a561c167485 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 4 Sep 2015 11:12:24 -0400 Subject: [PATCH] Fixed #25353 -- Changed LogEntry.action_time to a "date created". --- .../0002_logentry_remove_auto_add.py | 25 +++++++++++++++++++ django/contrib/admin/models.py | 18 +++++++++---- tests/admin_views/tests.py | 10 ++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 django/contrib/admin/migrations/0002_logentry_remove_auto_add.py diff --git a/django/contrib/admin/migrations/0002_logentry_remove_auto_add.py b/django/contrib/admin/migrations/0002_logentry_remove_auto_add.py new file mode 100644 index 0000000000..fb66c31bd3 --- /dev/null +++ b/django/contrib/admin/migrations/0002_logentry_remove_auto_add.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.utils import timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('admin', '0001_initial'), + ] + + # No database changes; removes auto_add and adds default/editable. + operations = [ + migrations.AlterField( + model_name='logentry', + name='action_time', + field=models.DateTimeField( + verbose_name='action time', + default=timezone.now, + editable=False, + ), + ), + ] diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index f51a015935..6b6b1f4142 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -5,6 +5,7 @@ from django.contrib.admin.utils import quote from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import NoReverseMatch, reverse from django.db import models +from django.utils import timezone from django.utils.encoding import python_2_unicode_compatible, smart_text from django.utils.translation import ugettext, ugettext_lazy as _ @@ -17,16 +18,23 @@ class LogEntryManager(models.Manager): use_in_migrations = True def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''): - e = self.model( - None, None, user_id, content_type_id, smart_text(object_id), - object_repr[:200], action_flag, change_message + self.model.objects.create( + user_id=user_id, + content_type_id=content_type_id, + object_id=smart_text(object_id), + object_repr=object_repr[:200], + action_flag=action_flag, + change_message=change_message, ) - e.save() @python_2_unicode_compatible class LogEntry(models.Model): - action_time = models.DateTimeField(_('action time'), auto_now=True) + action_time = models.DateTimeField( + _('action time'), + default=timezone.now, + editable=False, + ) user = models.ForeignKey( settings.AUTH_USER_MODEL, models.CASCADE, diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index f10075d49f..986add8479 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -2405,6 +2405,16 @@ class AdminViewStringPrimaryKeyTest(TestCase): edited_obj = logentry.get_edited_object() self.assertEqual(logentry.object_id, str(edited_obj.pk)) + def test_logentry_save(self): + """" + LogEntry.action_time is a timestamp of the date when the entry was + created. It shouldn't be updated on a subsequent save(). + """ + logentry = LogEntry.objects.get(content_type__model__iexact="modelwithstringprimarykey") + action_time = logentry.action_time + logentry.save() + self.assertEqual(logentry.action_time, action_time) + def test_deleteconfirmation_link(self): "The link from the delete confirmation page referring back to the changeform of the object should be quoted" response = self.client.get(reverse('admin:admin_views_modelwithstringprimarykey_delete', args=(quote(self.pk),)))