Introduce DEFAULT_ACTION_PERMISSIONS constant

This commit is contained in:
Jeremy Stretch 2023-10-20 14:46:36 -04:00
parent 10324bfe6e
commit 804f22779d
5 changed files with 39 additions and 93 deletions

View File

@ -20,6 +20,7 @@ from circuits.models import Circuit, CircuitTermination
from extras.views import ObjectConfigContextView from extras.views import ObjectConfigContextView
from ipam.models import ASN, IPAddress, Prefix, VLAN, VLANGroup from ipam.models import ASN, IPAddress, Prefix, VLAN, VLANGroup
from ipam.tables import InterfaceVLANTable from ipam.tables import InterfaceVLANTable
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
from netbox.views import generic from netbox.views import generic
from tenancy.views import ObjectContactsView from tenancy.views import ObjectContactsView
from utilities.forms import ConfirmationForm from utilities.forms import ConfirmationForm
@ -47,11 +48,7 @@ CABLE_TERMINATION_TYPES = {
class DeviceComponentsView(generic.ObjectChildrenView): class DeviceComponentsView(generic.ObjectChildrenView):
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
'bulk_disconnect': {'change'}, 'bulk_disconnect': {'change'},
} }
@ -1978,11 +1975,7 @@ class DeviceModuleBaysView(DeviceComponentsView):
filterset = filtersets.ModuleBayFilterSet filterset = filtersets.ModuleBayFilterSet
template_name = 'dcim/device/modulebays.html' template_name = 'dcim/device/modulebays.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
tab = ViewTab( tab = ViewTab(
@ -2001,11 +1994,7 @@ class DeviceDeviceBaysView(DeviceComponentsView):
filterset = filtersets.DeviceBayFilterSet filterset = filtersets.DeviceBayFilterSet
template_name = 'dcim/device/devicebays.html' template_name = 'dcim/device/devicebays.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
tab = ViewTab( tab = ViewTab(
@ -2024,11 +2013,7 @@ class DeviceInventoryView(DeviceComponentsView):
filterset = filtersets.InventoryItemFilterSet filterset = filtersets.InventoryItemFilterSet
template_name = 'dcim/device/inventory.html' template_name = 'dcim/device/inventory.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
tab = ViewTab( tab = ViewTab(
@ -2209,11 +2194,7 @@ class ConsolePortListView(generic.ObjectListView):
table = tables.ConsolePortTable table = tables.ConsolePortTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2281,11 +2262,7 @@ class ConsoleServerPortListView(generic.ObjectListView):
table = tables.ConsoleServerPortTable table = tables.ConsoleServerPortTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2353,11 +2330,7 @@ class PowerPortListView(generic.ObjectListView):
table = tables.PowerPortTable table = tables.PowerPortTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2425,11 +2398,7 @@ class PowerOutletListView(generic.ObjectListView):
table = tables.PowerOutletTable table = tables.PowerOutletTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2497,11 +2466,7 @@ class InterfaceListView(generic.ObjectListView):
table = tables.InterfaceTable table = tables.InterfaceTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2617,11 +2582,7 @@ class FrontPortListView(generic.ObjectListView):
table = tables.FrontPortTable table = tables.FrontPortTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2689,11 +2650,7 @@ class RearPortListView(generic.ObjectListView):
table = tables.RearPortTable table = tables.RearPortTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2761,11 +2718,7 @@ class ModuleBayListView(generic.ObjectListView):
table = tables.ModuleBayTable table = tables.ModuleBayTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2825,11 +2778,7 @@ class DeviceBayListView(generic.ObjectListView):
table = tables.DeviceBayTable table = tables.DeviceBayTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
@ -2958,11 +2907,7 @@ class InventoryItemListView(generic.ObjectListView):
table = tables.InventoryItemTable table = tables.InventoryItemTable
template_name = 'dcim/component_list.html' template_name = 'dcim/component_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }

View File

@ -16,6 +16,7 @@ from core.tables import JobTable
from extras.dashboard.forms import DashboardWidgetAddForm, DashboardWidgetForm from extras.dashboard.forms import DashboardWidgetAddForm, DashboardWidgetForm
from extras.dashboard.utils import get_widget_class from extras.dashboard.utils import get_widget_class
from netbox.config import get_config, PARAMS from netbox.config import get_config, PARAMS
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
from netbox.views import generic from netbox.views import generic
from utilities.forms import ConfirmationForm, get_field_value from utilities.forms import ConfirmationForm, get_field_value
from utilities.htmx import is_htmx from utilities.htmx import is_htmx
@ -211,11 +212,7 @@ class ExportTemplateListView(generic.ObjectListView):
table = tables.ExportTemplateTable table = tables.ExportTemplateTable
template_name = 'extras/exporttemplate_list.html' template_name = 'extras/exporttemplate_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_sync': {'sync'}, 'bulk_sync': {'sync'},
} }
@ -589,11 +586,7 @@ class ConfigTemplateListView(generic.ObjectListView):
table = tables.ConfigTemplateTable table = tables.ConfigTemplateTable
template_name = 'extras/configtemplate_list.html' template_name = 'extras/configtemplate_list.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_sync': {'sync'}, 'bulk_sync': {'sync'},
} }

View File

@ -27,3 +27,12 @@ ADVISORY_LOCK_KEYS = {
'inventoryitem': 105700, 'inventoryitem': 105700,
'inventoryitemtemplate': 105800, 'inventoryitemtemplate': 105800,
} }
# Default view action permission mapping
DEFAULT_ACTION_PERMISSIONS = {
'add': {'add'},
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
}

View File

@ -1,5 +1,6 @@
import warnings import warnings
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
from utilities.permissions import get_permission_for_model from utilities.permissions import get_permission_for_model
__all__ = ( __all__ = (
@ -9,14 +10,15 @@ __all__ = (
class ActionsMixin: class ActionsMixin:
# Map action names to the set of required permissions for each """
actions = { Maps action names to the set of required permissions for each. Object list views reference this mapping to
'add': {'add'}, determine whether to render the applicable button for each action: The button will be rendered only if the user
'import': {'add'}, possesses the specified permission(s).
'export': {'view'},
'bulk_edit': {'change'}, Standard actions include: add, import, export, bulk_edit, and bulk_delete. Some views extend this default map
'bulk_delete': {'delete'}, with custom actions, such as bulk_sync.
} """
actions = DEFAULT_ACTION_PERMISSIONS
def get_permitted_actions(self, user, model=None): def get_permitted_actions(self, user, model=None):
""" """

View File

@ -16,6 +16,7 @@ from dcim.tables import DeviceTable
from extras.views import ObjectConfigContextView from extras.views import ObjectConfigContextView
from ipam.models import IPAddress from ipam.models import IPAddress
from ipam.tables import InterfaceVLANTable from ipam.tables import InterfaceVLANTable
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
from netbox.views import generic from netbox.views import generic
from tenancy.views import ObjectContactsView from tenancy.views import ObjectContactsView
from utilities.utils import count_related from utilities.utils import count_related
@ -360,11 +361,7 @@ class VirtualMachineInterfacesView(generic.ObjectChildrenView):
filterset = filtersets.VMInterfaceFilterSet filterset = filtersets.VMInterfaceFilterSet
template_name = 'virtualization/virtualmachine/interfaces.html' template_name = 'virtualization/virtualmachine/interfaces.html'
actions = { actions = {
'add': {'add'}, **DEFAULT_ACTION_PERMISSIONS,
'import': {'add'},
'export': {'view'},
'bulk_edit': {'change'},
'bulk_delete': {'delete'},
'bulk_rename': {'change'}, 'bulk_rename': {'change'},
} }
tab = ViewTab( tab = ViewTab(