mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -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
@ -64,12 +64,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 |
|
||||||
| `buttons` | - | An iterable of PluginMenuButton instances to include |
|
| `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 |
|
||||||
|
|
||||||
|
!!! info "The `staff_only` attribute was introduced in NetBox v3.6.1."
|
||||||
|
|
||||||
## Menu Buttons
|
## Menu Buttons
|
||||||
|
|
||||||
|
@ -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,11 +26,14 @@ 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):
|
||||||
buttons = [
|
continue
|
||||||
button for button in item.buttons if user.has_perms(button.permissions)
|
if item.staff_only and not user.is_staff:
|
||||||
]
|
continue
|
||||||
items.append((item, buttons))
|
buttons = [
|
||||||
|
button for button in item.buttons if user.has_perms(button.permissions)
|
||||||
|
]
|
||||||
|
items.append((item, buttons))
|
||||||
if items:
|
if items:
|
||||||
groups.append((group, items))
|
groups.append((group, items))
|
||||||
if groups:
|
if groups:
|
||||||
|
Loading…
Reference in New Issue
Block a user