mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Introduce resolve_permission() utility function
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
|
||||
def get_permission_for_model(model, action):
|
||||
"""
|
||||
Resolve the named permission for a given model (or instance) and action (e.g. view or add).
|
||||
@ -13,3 +16,20 @@ def get_permission_for_model(model, action):
|
||||
action,
|
||||
model._meta.model_name
|
||||
)
|
||||
|
||||
|
||||
def resolve_permission(name):
|
||||
"""
|
||||
Given a permission name, return the relevant ContentType and action. For example, "dcim.view_site" returns
|
||||
(Site, "view").
|
||||
|
||||
:param name: Permission name in the format <app>.<action>_<model>
|
||||
"""
|
||||
app_label, codename = name.split('.')
|
||||
action, model_name = codename.split('_')
|
||||
try:
|
||||
content_type = ContentType.objects.get(app_label=app_label, model=model_name)
|
||||
except ContentType.DoesNotExist:
|
||||
raise ValueError(f"Unknown app/model for {name}")
|
||||
|
||||
return content_type, action
|
||||
|
Reference in New Issue
Block a user