diff options
| author | Jacob Rief <jacob.rief@gmail.com> | 2023-02-08 18:37:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-08 18:37:32 +0100 |
| commit | 473283d2414fa4bbf1e38d663fe4a58f49bf72b9 (patch) | |
| tree | 63e9f30aff3a6b95e47d3e737750e863644945e0 /tests | |
| parent | 1964e4367f293336b47e30af6e10a5eca5fdfb35 (diff) | |
Fixed #34303 –- Allowed customizing admin site log entry list.
Added AdminSite.get_log_entries() as an override point and made this
available to the template via each_context().
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_changelist/tests.py | 10 | ||||
| -rw-r--r-- | tests/admin_utils/admin.py | 16 | ||||
| -rw-r--r-- | tests/admin_utils/test_logentry.py | 29 | ||||
| -rw-r--r-- | tests/admin_utils/urls.py | 3 |
4 files changed, 54 insertions, 4 deletions
diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index a2db24fea1..413d6d4d7f 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -1595,7 +1595,12 @@ class GetAdminLogTests(TestCase): {% get_admin_log %} works if the user model's primary key isn't named 'id'. """ - context = Context({"user": CustomIdUser()}) + context = Context( + { + "user": CustomIdUser(), + "log_entries": LogEntry.objects.all(), + } + ) template = Template( "{% load log %}{% get_admin_log 10 as admin_log for_user user %}" ) @@ -1608,6 +1613,7 @@ class GetAdminLogTests(TestCase): user.save() ct = ContentType.objects.get_for_model(User) LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1) + context = Context({"log_entries": LogEntry.objects.all()}) t = Template( "{% load log %}" "{% get_admin_log 100 as admin_log %}" @@ -1615,7 +1621,7 @@ class GetAdminLogTests(TestCase): "{{ entry|safe }}" "{% endfor %}" ) - self.assertEqual(t.render(Context({})), "Added “<User: jondoe>”.") + self.assertEqual(t.render(context), "Added “<User: jondoe>”.") def test_missing_args(self): msg = "'get_admin_log' statements require two arguments" diff --git a/tests/admin_utils/admin.py b/tests/admin_utils/admin.py index 967f37faf3..a7cfcfa6b9 100644 --- a/tests/admin_utils/admin.py +++ b/tests/admin_utils/admin.py @@ -35,3 +35,19 @@ site = admin.AdminSite(name="admin") site.register(Article) site.register(ArticleProxy) site.register(Site, SiteAdmin) + + +class CustomAdminSite(admin.AdminSite): + def get_log_entries(self, request): + from django.contrib.contenttypes.models import ContentType + + log_entries = super().get_log_entries(request) + return log_entries.filter( + content_type__in=ContentType.objects.get_for_models( + *self._registry.keys() + ).values() + ) + + +custom_site = CustomAdminSite(name="custom_admin") +custom_site.register(Article) diff --git a/tests/admin_utils/test_logentry.py b/tests/admin_utils/test_logentry.py index dfa7962b1e..b700fe54b9 100644 --- a/tests/admin_utils/test_logentry.py +++ b/tests/admin_utils/test_logentry.py @@ -10,7 +10,7 @@ from django.urls import reverse from django.utils import translation from django.utils.html import escape -from .models import Article, ArticleProxy, Site +from .models import Article, ArticleProxy, Car, Site @override_settings(ROOT_URLCONF="admin_utils.urls") @@ -318,3 +318,30 @@ class LogEntryTests(TestCase): with self.subTest(action_flag=action_flag): log = LogEntry(action_flag=action_flag) self.assertEqual(log.get_action_flag_display(), display_name) + + def test_hook_get_log_entries(self): + LogEntry.objects.log_action( + self.user.pk, + ContentType.objects.get_for_model(Article).pk, + self.a1.pk, + "Article changed", + CHANGE, + change_message="Article changed message", + ) + c1 = Car.objects.create() + LogEntry.objects.log_action( + self.user.pk, + ContentType.objects.get_for_model(Car).pk, + c1.pk, + "Car created", + ADDITION, + change_message="Car created message", + ) + response = self.client.get(reverse("admin:index")) + self.assertContains(response, "Article changed") + self.assertContains(response, "Car created") + + # site "custom_admin" only renders log entries of registered models + response = self.client.get(reverse("custom_admin:index")) + self.assertContains(response, "Article changed") + self.assertNotContains(response, "Car created") diff --git a/tests/admin_utils/urls.py b/tests/admin_utils/urls.py index 1b6a97e4bd..69db6afc46 100644 --- a/tests/admin_utils/urls.py +++ b/tests/admin_utils/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from .admin import site +from .admin import custom_site, site urlpatterns = [ path("test_admin/admin/", site.urls), + path("test_admin/custom_admin/", custom_site.urls), ] |
