Fixed #27302 -- Fixed ModelAdmin.construct_change_message() changed field detection

Thanks Ramiro Morales for the report and Tim Graham for the review.
This commit is contained in:
Claude Paroz 2016-10-01 19:32:32 +02:00
parent aa7dc2dc39
commit 67eee5e4fa
3 changed files with 45 additions and 26 deletions

View File

@ -929,7 +929,6 @@ class ModelAdmin(BaseModelAdmin):
return urlencode({'_changelist_filters': preserved_filters}) return urlencode({'_changelist_filters': preserved_filters})
return '' return ''
@translation_override(None)
def construct_change_message(self, request, form, formsets, add=False): def construct_change_message(self, request, form, formsets, add=False):
""" """
Construct a JSON structure describing changes from a changed object. Construct a JSON structure describing changes from a changed object.
@ -943,6 +942,7 @@ class ModelAdmin(BaseModelAdmin):
change_message.append({'changed': {'fields': form.changed_data}}) change_message.append({'changed': {'fields': form.changed_data}})
if formsets: if formsets:
with translation_override(None):
for formset in formsets: for formset in formsets:
for added_object in formset.new_objects: for added_object in formset.new_objects:
change_message.append({ change_message.append({

View File

@ -25,3 +25,6 @@ Bugfixes
* Fixed a crash in ``runserver`` logging during a "Broken pipe" error * Fixed a crash in ``runserver`` logging during a "Broken pipe" error
(:ticket:`27271`). (:ticket:`27271`).
* Fixed a regression where unchanged localized date/time fields were listed as
changed in the admin's model history messages (:ticket:`27302`).

View File

@ -25,7 +25,7 @@ class LogEntryTests(TestCase):
self.a1 = Article.objects.create( self.a1 = Article.objects.create(
site=self.site, site=self.site,
title="Title", 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 content_type_pk = ContentType.objects.get_for_model(Article).pk
LogEntry.objects.log_action( LogEntry.objects.log_action(
@ -51,7 +51,7 @@ class LogEntryTests(TestCase):
""" """
post_data = { post_data = {
'site': self.site.pk, 'title': 'Changed', 'hist': 'Some content', '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)]) change_url = reverse('admin:admin_utils_article_change', args=[quote(self.a1.pk)])
response = self.client.post(change_url, post_data) response = self.client.post(change_url, post_data)
@ -70,6 +70,22 @@ class LogEntryTests(TestCase):
with translation.override('fr'): with translation.override('fr'):
self.assertEqual(logentry.get_change_message(), 'Ajout.') 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): def test_logentry_change_message_formsets(self):
""" """
All messages for changed formsets are logged in a change message. All messages for changed formsets are logged in a change message.