mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 12:12:53 -06:00
Fix support for legacy action dicts
This commit is contained in:
parent
63fd6ea1c5
commit
759ac64a1b
@ -9,6 +9,18 @@ __all__ = (
|
|||||||
'TableMixin',
|
'TableMixin',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO: Remove in NetBox v4.5
|
||||||
|
LEGACY_ACTIONS = {
|
||||||
|
'add': object_actions.AddObject,
|
||||||
|
'edit': object_actions.EditObject,
|
||||||
|
'delete': object_actions.DeleteObject,
|
||||||
|
'export': object_actions.BulkExport,
|
||||||
|
'bulk_import': object_actions.BulkImport,
|
||||||
|
'bulk_edit': object_actions.BulkEdit,
|
||||||
|
'bulk_rename': object_actions.BulkRename,
|
||||||
|
'bulk_delete': object_actions.BulkDelete,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ActionsMixin:
|
class ActionsMixin:
|
||||||
"""
|
"""
|
||||||
@ -21,26 +33,22 @@ class ActionsMixin:
|
|||||||
"""
|
"""
|
||||||
actions = tuple()
|
actions = tuple()
|
||||||
|
|
||||||
# TODO: Remove in NetBox v4.6
|
# TODO: Remove in NetBox v4.5
|
||||||
@staticmethod
|
def _convert_legacy_actions(self):
|
||||||
def _get_legacy_action(name):
|
|
||||||
"""
|
"""
|
||||||
Given a legacy action name, return the corresponding action class.
|
Convert a legacy dictionary mapping action name to required permissions to a list of ObjectAction subclasses.
|
||||||
"""
|
"""
|
||||||
action = {
|
if type(self.actions) is not dict:
|
||||||
'add': object_actions.AddObject,
|
return
|
||||||
'edit': object_actions.EditObject,
|
|
||||||
'delete': object_actions.DeleteObject,
|
|
||||||
'export': object_actions.BulkExport,
|
|
||||||
'bulk_import': object_actions.BulkImport,
|
|
||||||
'bulk_edit': object_actions.BulkEdit,
|
|
||||||
'bulk_rename': object_actions.BulkRename,
|
|
||||||
'bulk_delete': object_actions.BulkDelete,
|
|
||||||
}.get(name)
|
|
||||||
if name is None:
|
|
||||||
raise ValueError(f"Unknown action: {action}")
|
|
||||||
|
|
||||||
return action
|
actions = []
|
||||||
|
for name in self.actions.keys():
|
||||||
|
try:
|
||||||
|
actions.append(LEGACY_ACTIONS[name])
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError(f"Unsupported legacy action: {name}")
|
||||||
|
|
||||||
|
self.actions = actions
|
||||||
|
|
||||||
def get_permitted_actions(self, user, model=None):
|
def get_permitted_actions(self, user, model=None):
|
||||||
"""
|
"""
|
||||||
@ -48,13 +56,15 @@ class ActionsMixin:
|
|||||||
"""
|
"""
|
||||||
model = model or self.queryset.model
|
model = model or self.queryset.model
|
||||||
|
|
||||||
|
# TODO: Remove in NetBox v4.5
|
||||||
|
# Handle legacy action sets
|
||||||
|
self._convert_legacy_actions()
|
||||||
|
|
||||||
# Resolve required permissions for each action
|
# Resolve required permissions for each action
|
||||||
permitted_actions = []
|
permitted_actions = []
|
||||||
for action in self.actions:
|
for action in self.actions:
|
||||||
# Backward compatibility
|
|
||||||
perms = self._get_legacy_action(action) if type(action) is str else action.permissions_required
|
|
||||||
required_permissions = [
|
required_permissions = [
|
||||||
get_permission_for_model(model, perm) for perm in perms
|
get_permission_for_model(model, perm) for perm in action.permissions_required
|
||||||
]
|
]
|
||||||
if not required_permissions or user.has_perms(required_permissions):
|
if not required_permissions or user.has_perms(required_permissions):
|
||||||
permitted_actions.append(action)
|
permitted_actions.append(action)
|
||||||
|
Loading…
Reference in New Issue
Block a user