From aa9569fce1b308819039450e3d2b852c37128209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Fri, 30 Sep 2016 14:55:51 -0700 Subject: [PATCH] Fixed #27218 -- Returned LogEntry instances from ModelAdmin.log_*() methods. --- django/contrib/admin/models.py | 2 +- django/contrib/admin/options.py | 6 +++--- tests/admin_utils/test_logentry.py | 8 ++++++++ tests/modeladmin/tests.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index 4f7a225fc5c..a8245a20e4b 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -23,7 +23,7 @@ class LogEntryManager(models.Manager): def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''): if isinstance(change_message, list): change_message = json.dumps(change_message) - self.model.objects.create( + return self.model.objects.create( user_id=user_id, content_type_id=content_type_id, object_id=force_text(object_id), diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 0a249efcb31..e18e14a70fb 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -712,7 +712,7 @@ class ModelAdmin(BaseModelAdmin): The default implementation creates an admin LogEntry object. """ from django.contrib.admin.models import LogEntry, ADDITION - LogEntry.objects.log_action( + return LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=get_content_type_for_model(object).pk, object_id=object.pk, @@ -728,7 +728,7 @@ class ModelAdmin(BaseModelAdmin): The default implementation creates an admin LogEntry object. """ from django.contrib.admin.models import LogEntry, CHANGE - LogEntry.objects.log_action( + return LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=get_content_type_for_model(object).pk, object_id=object.pk, @@ -745,7 +745,7 @@ class ModelAdmin(BaseModelAdmin): The default implementation creates an admin LogEntry object. """ from django.contrib.admin.models import LogEntry, DELETION - LogEntry.objects.log_action( + return LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=get_content_type_for_model(object).pk, object_id=object.pk, diff --git a/tests/admin_utils/test_logentry.py b/tests/admin_utils/test_logentry.py index 4ea182e2aab..aaa67ffc133 100644 --- a/tests/admin_utils/test_logentry.py +++ b/tests/admin_utils/test_logentry.py @@ -179,6 +179,14 @@ class LogEntryTests(TestCase): log_entry.action_flag = 4 self.assertEqual(six.text_type(log_entry), 'LogEntry Object') + def test_log_action(self): + content_type_pk = ContentType.objects.get_for_model(Article).pk + log_entry = LogEntry.objects.log_action( + self.user.pk, content_type_pk, self.a1.pk, repr(self.a1), CHANGE, + change_message='Changed something else', + ) + self.assertEqual(log_entry, LogEntry.objects.latest('action_time')) + def test_recentactions_without_content_type(self): """ If a LogEntry is missing content_type it will not display it in span diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py index e00b69b5861..c9cde5a2c99 100644 --- a/tests/modeladmin/tests.py +++ b/tests/modeladmin/tests.py @@ -4,11 +4,13 @@ from datetime import date from django import forms from django.contrib.admin import BooleanFieldListFilter, SimpleListFilter +from django.contrib.admin.models import LogEntry from django.contrib.admin.options import ( HORIZONTAL, VERTICAL, ModelAdmin, TabularInline, ) from django.contrib.admin.sites import AdminSite from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect +from django.contrib.auth.models import User from django.core.checks import Error from django.forms.models import BaseModelFormSet from django.forms.widgets import Select @@ -533,6 +535,14 @@ class ModelAdminTests(TestCase): ['extra', 'transport', 'id', 'DELETE', 'main_band'] ) + def test_log_actions(self): + ma = ModelAdmin(Band, self.site) + mock_request = MockRequest() + mock_request.user = User.objects.create(username='bill') + self.assertEqual(ma.log_addition(mock_request, self.band, 'added'), LogEntry.objects.latest('action_time')) + self.assertEqual(ma.log_change(mock_request, self.band, 'changed'), LogEntry.objects.latest('action_time')) + self.assertEqual(ma.log_change(mock_request, self.band, 'deleted'), LogEntry.objects.latest('action_time')) + class CheckTestCase(SimpleTestCase):