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,29 +942,30 @@ class ModelAdmin(BaseModelAdmin):
change_message.append({'changed': {'fields': form.changed_data}}) change_message.append({'changed': {'fields': form.changed_data}})
if formsets: if formsets:
for formset in formsets: with translation_override(None):
for added_object in formset.new_objects: for formset in formsets:
change_message.append({ for added_object in formset.new_objects:
'added': { change_message.append({
'name': force_text(added_object._meta.verbose_name), 'added': {
'object': force_text(added_object), '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({ for changed_object, changed_fields in formset.changed_objects:
'changed': { change_message.append({
'name': force_text(changed_object._meta.verbose_name), 'changed': {
'object': force_text(changed_object), 'name': force_text(changed_object._meta.verbose_name),
'fields': changed_fields, 'object': force_text(changed_object),
} 'fields': changed_fields,
}) }
for deleted_object in formset.deleted_objects: })
change_message.append({ for deleted_object in formset.deleted_objects:
'deleted': { change_message.append({
'name': force_text(deleted_object._meta.verbose_name), 'deleted': {
'object': force_text(deleted_object), 'name': force_text(deleted_object._meta.verbose_name),
} 'object': force_text(deleted_object),
}) }
})
return change_message return change_message
def message_user(self, request, message, level=messages.INFO, extra_tags='', def message_user(self, request, message, level=messages.INFO, extra_tags='',

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.