mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Support menu items that are callables
This commit is contained in:
parent
0466c8ef9b
commit
d0129811e2
@ -38,9 +38,12 @@ class PluginMenuItem:
|
||||
permissions = []
|
||||
buttons = []
|
||||
|
||||
def __init__(self, link, link_text, auth_required=False, staff_only=False, permissions=None, buttons=None):
|
||||
def __init__(
|
||||
self, link, link_text, url=None, auth_required=False, staff_only=False, permissions=None, buttons=None
|
||||
):
|
||||
self.link = link
|
||||
self.link_text = link_text
|
||||
self.url = url
|
||||
self.auth_required = auth_required
|
||||
self.staff_only = staff_only
|
||||
if permissions is not None:
|
||||
@ -61,10 +64,11 @@ class PluginMenuButton:
|
||||
color = ButtonColorChoices.DEFAULT
|
||||
permissions = []
|
||||
|
||||
def __init__(self, link, title, icon_class, color=None, permissions=None):
|
||||
def __init__(self, link, title, icon_class, url=None, color=None, permissions=None):
|
||||
self.link = link
|
||||
self.title = title
|
||||
self.icon_class = icon_class
|
||||
self.url = url
|
||||
if permissions is not None:
|
||||
if type(permissions) not in (list, tuple):
|
||||
raise TypeError(_("Permissions must be passed as a tuple or list."))
|
||||
|
@ -53,8 +53,11 @@ def register_template_extensions(class_list):
|
||||
|
||||
|
||||
def register_menu(menu):
|
||||
if not isinstance(menu, PluginMenu):
|
||||
raise TypeError(_("{item} must be an instance of netbox.plugins.PluginMenuItem").format(item=menu))
|
||||
if not (isinstance(menu, PluginMenu) or callable(menu)):
|
||||
raise TypeError(_(
|
||||
"{item} must be an instance of netbox.plugins.PluginMenu "
|
||||
"or a callable returning such an instance").format(item=menu)
|
||||
)
|
||||
registry['plugins']['menus'].append(menu)
|
||||
|
||||
|
||||
@ -64,15 +67,17 @@ def register_menu_items(section_name, class_list):
|
||||
"""
|
||||
# Validation
|
||||
for menu_link in class_list:
|
||||
if not isinstance(menu_link, PluginMenuItem):
|
||||
raise TypeError(_("{menu_link} must be an instance of netbox.plugins.PluginMenuItem").format(
|
||||
menu_link=menu_link
|
||||
))
|
||||
if not (isinstance(menu_link, PluginMenuItem) or callable(menu_link)):
|
||||
raise TypeError(_(
|
||||
"{menu_link} must be an instance of netbox.plugins.PluginMenuItem "
|
||||
"or a callable returning such an instance").format(menu_link=menu_link)
|
||||
)
|
||||
for button in menu_link.buttons:
|
||||
if not isinstance(button, PluginMenuButton):
|
||||
raise TypeError(_("{button} must be an instance of netbox.plugins.PluginMenuButton").format(
|
||||
button=button
|
||||
))
|
||||
if not (isinstance(button, PluginMenuButton) or callable(button)):
|
||||
raise TypeError(_(
|
||||
"{button} must be an instance of netbox.plugins.PluginMenuButton "
|
||||
"or a callable returning such an instance").format(button=button)
|
||||
)
|
||||
|
||||
registry['plugins']['menu_items'][section_name] = class_list
|
||||
|
||||
|
@ -41,11 +41,11 @@
|
||||
</div>
|
||||
{% for item, buttons in items %}
|
||||
<div class="dropdown-item d-flex justify-content-between ps-3 py-0">
|
||||
<a href="{% url item.link %}" class="d-inline-flex flex-fill py-1">{{ item.link_text }}</a>
|
||||
<a href="{% if item.url %}{{ item.url }}{% else %}{% url item.link %}{% endif %}" class="d-inline-flex flex-fill py-1">{{ item.link_text }}</a>
|
||||
{% if buttons %}
|
||||
<div class="btn-group ms-1">
|
||||
{% for button in buttons %}
|
||||
<a href="{% url button.link %}" class="btn btn-sm btn-{{ button.color|default:"outline" }} lh-2 px-2" title="{{ button.title }}">
|
||||
<a href="{% if button.url %}{{ button.url }}{% else %}{% url button.link %}"{% endif %} class="btn btn-sm btn-{{ button.color|default:"outline" }} lh-2 px-2" title="{{ button.title }}">
|
||||
<i class="{{ button.icon_class }}"></i>
|
||||
</a>
|
||||
{% endfor %}
|
||||
|
@ -22,10 +22,14 @@ def nav(context):
|
||||
|
||||
# Construct the navigation menu based upon the current user's permissions
|
||||
for menu in MENUS:
|
||||
if callable(menu):
|
||||
menu = menu()
|
||||
groups = []
|
||||
for group in menu.groups:
|
||||
items = []
|
||||
for item in group.items:
|
||||
if callable(item):
|
||||
item = item()
|
||||
if getattr(item, 'auth_required', False) and not user.is_authenticated:
|
||||
continue
|
||||
if not user.has_perms(item.permissions):
|
||||
@ -33,7 +37,9 @@ def nav(context):
|
||||
if item.staff_only and not user.is_staff:
|
||||
continue
|
||||
buttons = [
|
||||
button for button in item.buttons if user.has_perms(button.permissions)
|
||||
button for button in [
|
||||
button() if callable(button) else button for button in item.buttons
|
||||
] if user.has_perms(button.permissions)
|
||||
]
|
||||
items.append((item, buttons))
|
||||
if items:
|
||||
|
Loading…
Reference in New Issue
Block a user