From 67eee5e4fa08f4404445ebf4f54b686e68a8530c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 1 Oct 2016 19:32:32 +0200 Subject: [PATCH] Fixed #27302 -- Fixed ModelAdmin.construct_change_message() changed field detection Thanks Ramiro Morales for the report and Tim Graham for the review. --- django/contrib/admin/options.py | 48 +++++++++++++++--------------- docs/releases/1.10.2.txt | 3 ++ tests/admin_utils/test_logentry.py | 20 +++++++++++-- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 3885997acb..0a249efcb3 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -929,7 +929,6 @@ class ModelAdmin(BaseModelAdmin): return urlencode({'_changelist_filters': preserved_filters}) return '' - @translation_override(None) def construct_change_message(self, request, form, formsets, add=False): """ Construct a JSON structure describing changes from a changed object. @@ -943,29 +942,30 @@ class ModelAdmin(BaseModelAdmin): change_message.append({'changed': {'fields': form.changed_data}}) if formsets: - for formset in formsets: - for added_object in formset.new_objects: - change_message.append({ - 'added': { - 'name': force_text(added_object._meta.verbose_name), - 'object': force_text(added_object), - } - }) - for changed_object, changed_fields in formset.changed_objects: - change_message.append({ - 'changed': { - 'name': force_text(changed_object._meta.verbose_name), - 'object': force_text(changed_object), - 'fields': changed_fields, - } - }) - for deleted_object in formset.deleted_objects: - change_message.append({ - 'deleted': { - 'name': force_text(deleted_object._meta.verbose_name), - 'object': force_text(deleted_object), - } - }) + with translation_override(None): + for formset in formsets: + for added_object in formset.new_objects: + change_message.append({ + 'added': { + 'name': force_text(added_object._meta.verbose_name), + 'object': force_text(added_object), + } + }) + for changed_object, changed_fields in formset.changed_objects: + change_message.append({ + 'changed': { + 'name': force_text(changed_object._meta.verbose_name), + 'object': force_text(changed_object), + 'fields': changed_fields, + } + }) + for deleted_object in formset.deleted_objects: + change_message.append({ + 'deleted': { + 'name': force_text(deleted_object._meta.verbose_name), + 'object': force_text(deleted_object), + } + }) return change_message def message_user(self, request, message, level=messages.INFO, extra_tags='', diff --git a/docs/releases/1.10.2.txt b/docs/releases/1.10.2.txt index d434a80bba..83c935e7c5 100644 --- a/docs/releases/1.10.2.txt +++ b/docs/releases/1.10.2.txt @@ -25,3 +25,6 @@ Bugfixes * Fixed a crash in ``runserver`` logging during a "Broken pipe" error (:ticket:`27271`). + +* Fixed a regression where unchanged localized date/time fields were listed as + changed in the admin's model history messages (:ticket:`27302`). diff --git a/tests/admin_utils/test_logentry.py b/tests/admin_utils/test_logentry.py index 8259bf0cc1..4ea182e2aa 100644 --- a/tests/admin_utils/test_logentry.py +++ b/tests/admin_utils/test_logentry.py @@ -25,7 +25,7 @@ class LogEntryTests(TestCase): self.a1 = Article.objects.create( site=self.site, title="Title", - created=datetime(2008, 3, 18, 11, 54), + created=datetime(2008, 3, 12, 11, 54), ) content_type_pk = ContentType.objects.get_for_model(Article).pk LogEntry.objects.log_action( @@ -51,7 +51,7 @@ class LogEntryTests(TestCase): """ post_data = { 'site': self.site.pk, 'title': 'Changed', 'hist': 'Some content', - 'created_0': '2008-03-18', 'created_1': '11:54', + 'created_0': '2008-03-12', 'created_1': '11:54', } change_url = reverse('admin:admin_utils_article_change', args=[quote(self.a1.pk)]) response = self.client.post(change_url, post_data) @@ -70,6 +70,22 @@ class LogEntryTests(TestCase): with translation.override('fr'): self.assertEqual(logentry.get_change_message(), 'Ajout.') + @override_settings(USE_L10N=True) + def test_logentry_change_message_localized_datetime_input(self): + """ + Localized date/time inputs shouldn't affect changed form data detection. + """ + post_data = { + 'site': self.site.pk, 'title': 'Changed', 'hist': 'Some content', + 'created_0': '12/03/2008', 'created_1': '11:54', + } + with translation.override('fr'): + change_url = reverse('admin:admin_utils_article_change', args=[quote(self.a1.pk)]) + response = self.client.post(change_url, post_data) + self.assertRedirects(response, reverse('admin:admin_utils_article_changelist')) + logentry = LogEntry.objects.filter(content_type__model__iexact='article').latest('id') + self.assertEqual(logentry.get_change_message(), 'Changed title and hist.') + def test_logentry_change_message_formsets(self): """ All messages for changed formsets are logged in a change message.