Skip to content

Commit

Permalink
Fixed #35237 -- Merged system checks for admin actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored and felixxm committed Feb 21, 2024
1 parent a084c5d commit 98e6f23
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 8 additions & 14 deletions django/contrib/admin/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,7 @@ def check(self, admin_obj, **kwargs):
*self._check_list_editable(admin_obj),
*self._check_search_fields(admin_obj),
*self._check_date_hierarchy(admin_obj),
*self._check_action_permission_methods(admin_obj),
*self._check_actions_uniqueness(admin_obj),
*self._check_actions(admin_obj),
]

def _check_save_as(self, obj):
Expand Down Expand Up @@ -1195,13 +1194,12 @@ def _check_date_hierarchy(self, obj):
else:
return []

def _check_action_permission_methods(self, obj):
"""
Actions with an allowed_permission attribute require the ModelAdmin to
implement a has_<perm>_permission() method for each permission.
"""
actions = obj._get_base_actions()
def _check_actions(self, obj):
errors = []
actions = obj._get_base_actions()

# Actions with an allowed_permission attribute require the ModelAdmin
# to implement a has_<perm>_permission() method for each permission.
for func, name, _ in actions:
if not hasattr(func, "allowed_permissions"):
continue
Expand All @@ -1220,12 +1218,8 @@ def _check_action_permission_methods(self, obj):
id="admin.E129",
)
)
return errors

def _check_actions_uniqueness(self, obj):
"""Check that every action has a unique __name__."""
errors = []
names = collections.Counter(name for _, name, _ in obj._get_base_actions())
# Names need to be unique.
names = collections.Counter(name for _, name, _ in actions)
for name, count in names.items():
if count > 1:
errors.append(
Expand Down
5 changes: 4 additions & 1 deletion django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,10 @@ def action_checkbox(self, obj):

@staticmethod
def _get_action_description(func, name):
return getattr(func, "short_description", capfirst(name.replace("_", " ")))
try:
return func.short_description
except AttributeError:
return capfirst(name.replace("_", " "))

def _get_base_actions(self):
"""Return the list of actions, prior to any request-based filtering."""
Expand Down

0 comments on commit 98e6f23

Please sign in to comment.