1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#9072: Implement a mechanism for dynamically registering model detail views

This commit is contained in:
jeremystretch
2022-10-06 16:20:35 -04:00
parent 664d5db5eb
commit 0d7851ed9d
7 changed files with 158 additions and 26 deletions

35
netbox/utilities/urls.py Normal file
View File

@ -0,0 +1,35 @@
from django.urls import path
from django.utils.module_loading import import_string
from django.views.generic import View
from extras.registry import registry
def get_model_urls(app_label, model_name):
"""
Return a list of URL paths for detail views registered to the given model.
Args:
app_label: App/plugin name
model_name: Model name
"""
paths = []
# Retrieve registered views for this model
try:
views = registry['views'][app_label][model_name]
except KeyError:
# No views have been registered for this model
views = []
for view in views:
# Import the view class or function
callable = import_string(view['path'])
if issubclass(callable, View):
callable = callable.as_view()
# Create a path to the view
paths.append(
path(f"{view['name']}/", callable, name=f"{model_name}_{view['name']}", kwargs=view['kwargs'])
)
return paths