mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-15 03:32:53 -06:00
* Closes #13638: Add optional staff_only attribute to MenuItem * Add missing file * Add release note
This commit is contained in:
parent
2544e2bf18
commit
f962fb3b53
@ -65,12 +65,15 @@ item1 = PluginMenuItem(
|
|||||||
A `PluginMenuItem` has the following attributes:
|
A `PluginMenuItem` has the following attributes:
|
||||||
|
|
||||||
| Attribute | Required | Description |
|
| Attribute | Required | Description |
|
||||||
|---------------|----------|------------------------------------------------------|
|
|---------------|----------|----------------------------------------------------------------------------------------------------------|
|
||||||
| `link` | Yes | Name of the URL path to which this menu item links |
|
| `link` | Yes | Name of the URL path to which this menu item links |
|
||||||
| `link_text` | Yes | The text presented to the user |
|
| `link_text` | Yes | The text presented to the user |
|
||||||
| `permissions` | - | A list of permissions required to display this link |
|
| `permissions` | - | A list of permissions required to display this link |
|
||||||
|
| `staff_only` | - | Display only for users who have `is_staff` set to true (any specified permissions will also be required) |
|
||||||
| `buttons` | - | An iterable of PluginMenuButton instances to include |
|
| `buttons` | - | An iterable of PluginMenuButton instances to include |
|
||||||
|
|
||||||
|
!!! info "The `staff_only` attribute was introduced in NetBox v3.6.1."
|
||||||
|
|
||||||
## Menu Buttons
|
## Menu Buttons
|
||||||
|
|
||||||
Each menu item can include a set of buttons. These can be handy for providing shortcuts related to the menu item. For instance, most items in NetBox's navigation menu include buttons to create and import new objects.
|
Each menu item can include a set of buttons. These can be handy for providing shortcuts related to the menu item. For instance, most items in NetBox's navigation menu include buttons to create and import new objects.
|
||||||
|
@ -36,9 +36,10 @@ class PluginMenuItem:
|
|||||||
permissions = []
|
permissions = []
|
||||||
buttons = []
|
buttons = []
|
||||||
|
|
||||||
def __init__(self, link, link_text, permissions=None, buttons=None):
|
def __init__(self, link, link_text, staff_only=False, permissions=None, buttons=None):
|
||||||
self.link = link
|
self.link = link
|
||||||
self.link_text = link_text
|
self.link_text = link_text
|
||||||
|
self.staff_only = staff_only
|
||||||
if permissions is not None:
|
if permissions is not None:
|
||||||
if type(permissions) not in (list, tuple):
|
if type(permissions) not in (list, tuple):
|
||||||
raise TypeError("Permissions must be passed as a tuple or list.")
|
raise TypeError("Permissions must be passed as a tuple or list.")
|
||||||
|
@ -34,6 +34,7 @@ class MenuItem:
|
|||||||
link: str
|
link: str
|
||||||
link_text: str
|
link_text: str
|
||||||
permissions: Optional[Sequence[str]] = ()
|
permissions: Optional[Sequence[str]] = ()
|
||||||
|
staff_only: Optional[bool] = False
|
||||||
buttons: Optional[Sequence[MenuItemButton]] = ()
|
buttons: Optional[Sequence[MenuItemButton]] = ()
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,7 +26,10 @@ def nav(context: Context) -> Dict:
|
|||||||
for group in menu.groups:
|
for group in menu.groups:
|
||||||
items = []
|
items = []
|
||||||
for item in group.items:
|
for item in group.items:
|
||||||
if user.has_perms(item.permissions):
|
if not user.has_perms(item.permissions):
|
||||||
|
continue
|
||||||
|
if item.staff_only and not user.is_staff:
|
||||||
|
continue
|
||||||
buttons = [
|
buttons = [
|
||||||
button for button in item.buttons if user.has_perms(button.permissions)
|
button for button in item.buttons if user.has_perms(button.permissions)
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user