Add ModelAction and register_model_actions() API for custom permission actions

This commit is contained in:
Jason Novinger
2026-02-20 13:45:52 -06:00
parent 6eafffb497
commit 9de5a0c584
2 changed files with 29 additions and 1 deletions
+1
View File
@@ -28,6 +28,7 @@ registry = Registry({
'denormalized_fields': collections.defaultdict(list),
'event_types': dict(),
'filtersets': dict(),
'model_actions': collections.defaultdict(list),
'model_features': dict(),
'models': collections.defaultdict(set),
'plugins': dict(),
+28 -1
View File
@@ -1,19 +1,46 @@
from dataclasses import dataclass
from django.apps import apps
from django.conf import settings
from django.db.models import Q
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
__all__ = (
'ModelAction',
'get_permission_for_model',
'permission_is_exempt',
'qs_filter_from_constraints',
'register_model_actions',
'resolve_permission',
'resolve_permission_type',
)
@dataclass
class ModelAction:
name: str
help_text: str = ''
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
if isinstance(other, ModelAction):
return self.name == other.name
return self.name == other
def register_model_actions(model: type[Model], actions: list[ModelAction | str]):
label = f'{model._meta.app_label}.{model._meta.model_name}'
for action in actions:
if isinstance(action, str):
action = ModelAction(name=action)
registry['model_actions'][label].append(action)
def get_permission_for_model(model, action):
"""
Resolve the named permission for a given model (or instance) and action (e.g. view or add).