mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Refactor navigation resources and menu
This commit is contained in:
@ -60,7 +60,7 @@ Create the HTML template for the object view. (The other views each typically em
|
|||||||
|
|
||||||
## 10. Add the model to the navigation menu
|
## 10. Add the model to the navigation menu
|
||||||
|
|
||||||
Add the relevant navigation menu items in `netbox/netbox/navigation_menu.py`.
|
Add the relevant navigation menu items in `netbox/netbox/navigation/menu.py`.
|
||||||
|
|
||||||
## 11. REST API components
|
## 11. REST API components
|
||||||
|
|
||||||
|
92
netbox/netbox/navigation/__init__.py
Normal file
92
netbox/netbox/navigation/__init__.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Sequence, Optional
|
||||||
|
|
||||||
|
from utilities.choices import ButtonColorChoices
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = (
|
||||||
|
'get_model_item',
|
||||||
|
'get_model_buttons',
|
||||||
|
'Menu',
|
||||||
|
'MenuGroup',
|
||||||
|
'MenuItem',
|
||||||
|
'MenuItemButton',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Navigation menu data classes
|
||||||
|
#
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MenuItemButton:
|
||||||
|
|
||||||
|
link: str
|
||||||
|
title: str
|
||||||
|
icon_class: str
|
||||||
|
permissions: Optional[Sequence[str]] = ()
|
||||||
|
color: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MenuItem:
|
||||||
|
|
||||||
|
link: str
|
||||||
|
link_text: str
|
||||||
|
permissions: Optional[Sequence[str]] = ()
|
||||||
|
buttons: Optional[Sequence[MenuItemButton]] = ()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MenuGroup:
|
||||||
|
|
||||||
|
label: str
|
||||||
|
items: Sequence[MenuItem]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Menu:
|
||||||
|
|
||||||
|
label: str
|
||||||
|
icon_class: str
|
||||||
|
groups: Sequence[MenuGroup]
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Utility functions
|
||||||
|
#
|
||||||
|
|
||||||
|
def get_model_item(app_label, model_name, label, actions=('add', 'import')):
|
||||||
|
return MenuItem(
|
||||||
|
link=f'{app_label}:{model_name}_list',
|
||||||
|
link_text=label,
|
||||||
|
permissions=[f'{app_label}.view_{model_name}'],
|
||||||
|
buttons=get_model_buttons(app_label, model_name, actions)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_model_buttons(app_label, model_name, actions=('add', 'import')):
|
||||||
|
buttons = []
|
||||||
|
|
||||||
|
if 'add' in actions:
|
||||||
|
buttons.append(
|
||||||
|
MenuItemButton(
|
||||||
|
link=f'{app_label}:{model_name}_add',
|
||||||
|
title='Add',
|
||||||
|
icon_class='mdi mdi-plus-thick',
|
||||||
|
permissions=[f'{app_label}.add_{model_name}'],
|
||||||
|
color=ButtonColorChoices.GREEN
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if 'import' in actions:
|
||||||
|
buttons.append(
|
||||||
|
MenuItemButton(
|
||||||
|
link=f'{app_label}:{model_name}_import',
|
||||||
|
title='Import',
|
||||||
|
icon_class='mdi mdi-upload',
|
||||||
|
permissions=[f'{app_label}.add_{model_name}'],
|
||||||
|
color=ButtonColorChoices.CYAN
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return buttons
|
@ -1,86 +1,5 @@
|
|||||||
from dataclasses import dataclass
|
|
||||||
from typing import Sequence, Optional
|
|
||||||
|
|
||||||
from extras.registry import registry
|
from extras.registry import registry
|
||||||
from utilities.choices import ButtonColorChoices
|
from .navigation import *
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Nav menu data classes
|
|
||||||
#
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class MenuItemButton:
|
|
||||||
|
|
||||||
link: str
|
|
||||||
title: str
|
|
||||||
icon_class: str
|
|
||||||
permissions: Optional[Sequence[str]] = ()
|
|
||||||
color: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class MenuItem:
|
|
||||||
|
|
||||||
link: str
|
|
||||||
link_text: str
|
|
||||||
permissions: Optional[Sequence[str]] = ()
|
|
||||||
buttons: Optional[Sequence[MenuItemButton]] = ()
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class MenuGroup:
|
|
||||||
|
|
||||||
label: str
|
|
||||||
items: Sequence[MenuItem]
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Menu:
|
|
||||||
|
|
||||||
label: str
|
|
||||||
icon_class: str
|
|
||||||
groups: Sequence[MenuGroup]
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Utility functions
|
|
||||||
#
|
|
||||||
|
|
||||||
def get_model_item(app_label, model_name, label, actions=('add', 'import')):
|
|
||||||
return MenuItem(
|
|
||||||
link=f'{app_label}:{model_name}_list',
|
|
||||||
link_text=label,
|
|
||||||
permissions=[f'{app_label}.view_{model_name}'],
|
|
||||||
buttons=get_model_buttons(app_label, model_name, actions)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_model_buttons(app_label, model_name, actions=('add', 'import')):
|
|
||||||
buttons = []
|
|
||||||
|
|
||||||
if 'add' in actions:
|
|
||||||
buttons.append(
|
|
||||||
MenuItemButton(
|
|
||||||
link=f'{app_label}:{model_name}_add',
|
|
||||||
title='Add',
|
|
||||||
icon_class='mdi mdi-plus-thick',
|
|
||||||
permissions=[f'{app_label}.add_{model_name}'],
|
|
||||||
color=ButtonColorChoices.GREEN
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if 'import' in actions:
|
|
||||||
buttons.append(
|
|
||||||
MenuItemButton(
|
|
||||||
link=f'{app_label}:{model_name}_import',
|
|
||||||
title='Import',
|
|
||||||
icon_class='mdi mdi-upload',
|
|
||||||
permissions=[f'{app_label}.add_{model_name}'],
|
|
||||||
color=ButtonColorChoices.CYAN
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return buttons
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
@ -2,7 +2,7 @@ from typing import Dict
|
|||||||
from django import template
|
from django import template
|
||||||
from django.template import Context
|
from django.template import Context
|
||||||
|
|
||||||
from netbox.navigation_menu import MENUS
|
from netbox.navigation.menu import MENUS
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
Reference in New Issue
Block a user