feat: add personalized navigation and tenant filtering
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
from netbox.plugins import PluginTemplateExtension
|
||||
from tenancy.models import Tenant
|
||||
|
||||
from .models import NavigationPreference
|
||||
from .navigation_helpers import get_visible_menus, normalize_preferences
|
||||
from .runtime import navigation_customization_enabled, tenant_filter_enabled
|
||||
|
||||
|
||||
class UtilitiesGlobalContent(PluginTemplateExtension):
|
||||
def head(self):
|
||||
request = self.context["request"]
|
||||
preference_data = {"order": [], "hidden": [], "menus": []}
|
||||
if request.user.is_authenticated and navigation_customization_enabled():
|
||||
descriptors = get_visible_menus(request.user)
|
||||
preference = NavigationPreference.objects.filter(user=request.user).first()
|
||||
order, hidden = normalize_preferences(
|
||||
preference.menu_order if preference else [],
|
||||
preference.hidden_menus if preference else [],
|
||||
[item["key"] for item in descriptors],
|
||||
)
|
||||
preference_data = {
|
||||
"order": order,
|
||||
"hidden": hidden,
|
||||
"menus": descriptors,
|
||||
}
|
||||
return self.render(
|
||||
"netbox_utilities/head.html",
|
||||
{
|
||||
"navigation_enabled": request.user.is_authenticated and navigation_customization_enabled(),
|
||||
"navigation_preferences": preference_data,
|
||||
},
|
||||
)
|
||||
|
||||
def navbar(self):
|
||||
request = self.context["request"]
|
||||
if not request.user.is_authenticated or not tenant_filter_enabled():
|
||||
return ""
|
||||
|
||||
tenants = Tenant.objects.restrict(request.user, "view").order_by("name")
|
||||
return self.render(
|
||||
"netbox_utilities/tenant_dropdown.html",
|
||||
{
|
||||
"tenants": tenants,
|
||||
"selected_tenant": getattr(request, "netbox_utilities_tenant", None),
|
||||
"return_url": request.get_full_path(),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
template_extensions = [UtilitiesGlobalContent]
|
||||
Reference in New Issue
Block a user