From 8b3e714ecf409ed6c9628c3f2a4e033cbfa4253b Mon Sep 17 00:00:00 2001 From: Adam Donaghy Date: Mon, 6 Jan 2020 22:10:40 +1100 Subject: [PATCH] Fixed #30980 -- Improved error message when checking uniqueness of admin actions' __name__. Thanks Keshav Kumar for the initial patch. --- django/contrib/admin/checks.py | 24 +++++++++++++++--------- docs/ref/checks.txt | 2 +- tests/modeladmin/test_checks.py | 5 ++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 0c32301284..ba754bd873 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -1,3 +1,4 @@ +import collections from itertools import chain from django.apps import apps @@ -985,15 +986,20 @@ class ModelAdminChecks(BaseModelAdminChecks): def _check_actions_uniqueness(self, obj): """Check that every action has a unique __name__.""" - names = [name for _, name, _ in obj._get_base_actions()] - if len(names) != len(set(names)): - return [checks.Error( - '__name__ attributes of actions defined in %s must be ' - 'unique.' % obj.__class__, - obj=obj.__class__, - id='admin.E130', - )] - return [] + errors = [] + names = collections.Counter(name for _, name, _ in obj._get_base_actions()) + for name, count in names.items(): + if count > 1: + errors.append(checks.Error( + '__name__ attributes of actions defined in %s must be ' + 'unique. Name %r is not unique.' % ( + obj.__class__.__name__, + name, + ), + obj=obj.__class__, + id='admin.E130', + )) + return errors class InlineModelAdminChecks(BaseModelAdminChecks): diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index fcbfec98cb..a080b5bdf5 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -633,7 +633,7 @@ with the admin site: * **admin.E129**: ```` must define a ``has__permission()`` method for the ```` action. * **admin.E130**: ``__name__`` attributes of actions defined in - ```` must be unique. + ```` must be unique. Name ```` is not unique. ``InlineModelAdmin`` ~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index 98cc56d67c..308f4a19eb 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -1441,9 +1441,8 @@ class ActionsCheckTests(CheckTestCase): self.assertIsInvalid( BandAdmin, Band, - "__name__ attributes of actions defined in " - ".BandAdmin'> must be unique.", + "__name__ attributes of actions defined in BandAdmin must be " + "unique. Name 'action' is not unique.", id='admin.E130', )