Fixes #21357: Add API for registering custom model actions #246

Open
opened 2026-04-05 16:23:31 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @jnovinger on 3/3/2026

Fixes: #21357

Adds register_model_actions() API allowing plugins and core to register custom permission actions that appear as checkboxes in ObjectPermissionForm.

Changes:

  • Add model_actions registry store
  • Add ModelAction dataclass and register_model_actions() function in utilities.permissions
  • Add ObjectTypeSplitMultiSelectWidget with data attributes for JS targeting
  • Add RegisteredActionsWidget for rendering grouped action checkboxes
  • Update ObjectPermissionForm with consolidated Actions fieldset
  • Add JavaScript to show/hide actions based on selected object types
  • Register core actions: DataSource.sync, Device.render_config, VirtualMachine.render_config
  • Add unit tests for ModelAction and register_model_actions

Plugin usage:

# In plugin's apps.py ready() method
from utilities.permissions import ModelAction, register_model_actions
from .models import MyModel

register_model_actions(MyModel, [
    ModelAction('custom_action', help_text='Description of the action'),
])
image

Testing instructions:

  1. Navigate to Admin > Permissions > Object Permissions > Add
  2. In the "Object types" field, select models that have registered actions:
    • Data Source (Core)
    • Device (DCIM)
    • Virtual Machine (Virtualization)
  3. Observe the "Custom actions" section appears with checkboxes grouped by model
  4. Remove all models with registered actions - the "Custom actions" field should hide completely
  5. Select a mix of models with and without registered actions - only applicable action groups should show
*Originally created by @jnovinger on 3/3/2026* ### Fixes: #21357 Adds `register_model_actions()` API allowing plugins and core to register custom permission actions that appear as checkboxes in ObjectPermissionForm. **Changes:** - Add `model_actions` registry store - Add `ModelAction` dataclass and `register_model_actions()` function in `utilities.permissions` - Add `ObjectTypeSplitMultiSelectWidget` with data attributes for JS targeting - Add `RegisteredActionsWidget` for rendering grouped action checkboxes - Update `ObjectPermissionForm` with consolidated Actions fieldset - Add JavaScript to show/hide actions based on selected object types - Register core actions: `DataSource.sync`, `Device.render_config`, `VirtualMachine.render_config` - Add unit tests for `ModelAction` and `register_model_actions` **Plugin usage:** ```python # In plugin's apps.py ready() method from utilities.permissions import ModelAction, register_model_actions from .models import MyModel register_model_actions(MyModel, [ ModelAction('custom_action', help_text='Description of the action'), ]) ``` <img width="836" height="786" alt="image" src="https://github.com/user-attachments/assets/cb24b4c8-ecfb-420f-bf8b-c3fa5fd1895c" /> **Testing instructions:** 1. Navigate to Admin > Permissions > Object Permissions > Add 2. In the "Object types" field, select models that have registered actions: - Data Source (Core) - Device (DCIM) - Virtual Machine (Virtualization) 3. Observe the "Custom actions" section appears with checkboxes grouped by model 4. Remove all models with registered actions - the "Custom actions" field should hide completely 5. Select a mix of models with and without registered actions - only applicable action groups should show
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#246