Fixed #30980 -- Improved error message when checking uniqueness of admin actions' __name__.

Thanks Keshav Kumar for the initial patch.
This commit is contained in:
Adam Donaghy 2020-01-06 22:10:40 +11:00 committed by Mariusz Felisiak
parent e2d9d66a22
commit 8b3e714ecf
3 changed files with 18 additions and 13 deletions

View File

@ -1,3 +1,4 @@
import collections
from itertools import chain from itertools import chain
from django.apps import apps from django.apps import apps
@ -985,15 +986,20 @@ class ModelAdminChecks(BaseModelAdminChecks):
def _check_actions_uniqueness(self, obj): def _check_actions_uniqueness(self, obj):
"""Check that every action has a unique __name__.""" """Check that every action has a unique __name__."""
names = [name for _, name, _ in obj._get_base_actions()] errors = []
if len(names) != len(set(names)): names = collections.Counter(name for _, name, _ in obj._get_base_actions())
return [checks.Error( for name, count in names.items():
'__name__ attributes of actions defined in %s must be ' if count > 1:
'unique.' % obj.__class__, errors.append(checks.Error(
obj=obj.__class__, '__name__ attributes of actions defined in %s must be '
id='admin.E130', 'unique. Name %r is not unique.' % (
)] obj.__class__.__name__,
return [] name,
),
obj=obj.__class__,
id='admin.E130',
))
return errors
class InlineModelAdminChecks(BaseModelAdminChecks): class InlineModelAdminChecks(BaseModelAdminChecks):

View File

@ -633,7 +633,7 @@ with the admin site:
* **admin.E129**: ``<modeladmin>`` must define a ``has_<foo>_permission()`` * **admin.E129**: ``<modeladmin>`` must define a ``has_<foo>_permission()``
method for the ``<action>`` action. method for the ``<action>`` action.
* **admin.E130**: ``__name__`` attributes of actions defined in * **admin.E130**: ``__name__`` attributes of actions defined in
``<modeladmin>`` must be unique. ``<modeladmin>`` must be unique. Name ``<name>`` is not unique.
``InlineModelAdmin`` ``InlineModelAdmin``
~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~

View File

@ -1441,9 +1441,8 @@ class ActionsCheckTests(CheckTestCase):
self.assertIsInvalid( self.assertIsInvalid(
BandAdmin, Band, BandAdmin, Band,
"__name__ attributes of actions defined in " "__name__ attributes of actions defined in BandAdmin must be "
"<class 'modeladmin.test_checks.ActionsCheckTests." "unique. Name 'action' is not unique.",
"test_actions_not_unique.<locals>.BandAdmin'> must be unique.",
id='admin.E130', id='admin.E130',
) )