Fixed #24490 -- Set LogEntry.change_message when adding an object.
This commit is contained in:
parent
a3e75f6bed
commit
aea02ddfb7
|
@ -696,7 +696,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True):
|
||||
return self.paginator(queryset, per_page, orphans, allow_empty_first_page)
|
||||
|
||||
def log_addition(self, request, object):
|
||||
def log_addition(self, request, object, message):
|
||||
"""
|
||||
Log that an object has been successfully added.
|
||||
|
||||
|
@ -708,7 +708,8 @@ class ModelAdmin(BaseModelAdmin):
|
|||
content_type_id=get_content_type_for_model(object).pk,
|
||||
object_id=object.pk,
|
||||
object_repr=force_text(object),
|
||||
action_flag=ADDITION
|
||||
action_flag=ADDITION,
|
||||
change_message=message,
|
||||
)
|
||||
|
||||
def log_change(self, request, object, message):
|
||||
|
@ -724,7 +725,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
object_id=object.pk,
|
||||
object_repr=force_text(object),
|
||||
action_flag=CHANGE,
|
||||
change_message=message
|
||||
change_message=message,
|
||||
)
|
||||
|
||||
def log_deletion(self, request, object, object_repr):
|
||||
|
@ -740,7 +741,7 @@ class ModelAdmin(BaseModelAdmin):
|
|||
content_type_id=get_content_type_for_model(object).pk,
|
||||
object_id=object.pk,
|
||||
object_repr=object_repr,
|
||||
action_flag=DELETION
|
||||
action_flag=DELETION,
|
||||
)
|
||||
|
||||
def action_checkbox(self, obj):
|
||||
|
@ -920,12 +921,14 @@ class ModelAdmin(BaseModelAdmin):
|
|||
return urlencode({'_changelist_filters': preserved_filters})
|
||||
return ''
|
||||
|
||||
def construct_change_message(self, request, form, formsets):
|
||||
def construct_change_message(self, request, form, formsets, add=False):
|
||||
"""
|
||||
Construct a change message from a changed object.
|
||||
"""
|
||||
change_message = []
|
||||
if form.changed_data:
|
||||
if add:
|
||||
change_message.append(_('Added.'))
|
||||
elif form.changed_data:
|
||||
change_message.append(_('Changed %s.') % get_text_list(form.changed_data, _('and')))
|
||||
|
||||
if formsets:
|
||||
|
@ -1373,11 +1376,11 @@ class ModelAdmin(BaseModelAdmin):
|
|||
if all_valid(formsets) and form_validated:
|
||||
self.save_model(request, new_object, form, not add)
|
||||
self.save_related(request, form, formsets, not add)
|
||||
change_message = self.construct_change_message(request, form, formsets, add)
|
||||
if add:
|
||||
self.log_addition(request, new_object)
|
||||
self.log_addition(request, new_object, change_message)
|
||||
return self.response_add(request, new_object)
|
||||
else:
|
||||
change_message = self.construct_change_message(request, form, formsets)
|
||||
self.log_change(request, new_object, change_message)
|
||||
return self.response_change(request, new_object)
|
||||
else:
|
||||
|
|
|
@ -8,7 +8,7 @@ import unittest
|
|||
|
||||
from django.contrib.admin import ModelAdmin
|
||||
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
||||
from django.contrib.admin.models import DELETION, LogEntry
|
||||
from django.contrib.admin.models import ADDITION, DELETION, LogEntry
|
||||
from django.contrib.admin.options import TO_FIELD_VAR
|
||||
from django.contrib.admin.templatetags.admin_static import static
|
||||
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
||||
|
@ -1542,6 +1542,17 @@ class AdminViewPermissionsTest(TestCase):
|
|||
self.assertEqual(mail.outbox[0].subject, 'Greetings from a created object')
|
||||
self.client.get(reverse('admin:logout'))
|
||||
|
||||
# Check that the addition was logged correctly
|
||||
addition_log = LogEntry.objects.all()[0]
|
||||
new_article = Article.objects.last()
|
||||
article_ct = ContentType.objects.get_for_model(Article)
|
||||
self.assertEqual(addition_log.user_id, self.u2.pk)
|
||||
self.assertEqual(addition_log.content_type_id, article_ct.pk)
|
||||
self.assertEqual(addition_log.object_id, str(new_article.pk))
|
||||
self.assertEqual(addition_log.object_repr, "Døm ikke")
|
||||
self.assertEqual(addition_log.action_flag, ADDITION)
|
||||
self.assertEqual(addition_log.change_message, "Added.")
|
||||
|
||||
# Super can add too, but is redirected to the change list view
|
||||
self.client.get(self.index_url)
|
||||
self.client.post(login_url, self.super_login)
|
||||
|
|
Loading…
Reference in New Issue