mirror of
https://github.com/netbox-community/netbox.git
synced 2026-03-21 20:18:38 -06:00
Reject reserved action names in register_model_actions()
This commit is contained in:
@@ -6,7 +6,7 @@ from django.db.models import Model, Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from netbox.registry import registry
|
||||
from users.constants import CONSTRAINT_TOKEN_USER
|
||||
from users.constants import CONSTRAINT_TOKEN_USER, RESERVED_ACTIONS
|
||||
|
||||
__all__ = (
|
||||
'ModelAction',
|
||||
@@ -53,6 +53,8 @@ def register_model_actions(model: type[Model], actions: list[ModelAction | str])
|
||||
for action in actions:
|
||||
if isinstance(action, str):
|
||||
action = ModelAction(name=action)
|
||||
if action.name in RESERVED_ACTIONS:
|
||||
raise ValueError(f"'{action.name}' is a reserved action and cannot be registered.")
|
||||
if action not in registry['model_actions'][label]:
|
||||
registry['model_actions'][label].append(action)
|
||||
|
||||
|
||||
@@ -86,6 +86,11 @@ class RegisterModelActionsTest(TestCase):
|
||||
actions = registry['model_actions']['dcim.site']
|
||||
self.assertEqual(len(actions), 1)
|
||||
|
||||
def test_reserved_action_rejected(self):
|
||||
for action_name in ('view', 'add', 'change', 'delete'):
|
||||
with self.assertRaises(ValueError):
|
||||
register_model_actions(Site, [ModelAction(action_name)])
|
||||
|
||||
|
||||
class ObjectPermissionFormTest(TestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user