Improve settings hierarchy conciseness
This commit is contained in:
119
core/context_processors.py
Normal file
119
core/context_processors.py
Normal file
@@ -0,0 +1,119 @@
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
def _tab(label: str, href: str, active: bool) -> dict:
|
||||
return {
|
||||
"label": label,
|
||||
"href": href,
|
||||
"active": bool(active),
|
||||
}
|
||||
|
||||
|
||||
def settings_hierarchy_nav(request):
|
||||
match = getattr(request, "resolver_match", None)
|
||||
if match is None:
|
||||
return {}
|
||||
|
||||
url_name = str(getattr(match, "url_name", "") or "")
|
||||
namespace = str(getattr(match, "namespace", "") or "")
|
||||
path = str(getattr(request, "path", "") or "")
|
||||
|
||||
notifications_href = reverse("notifications_settings")
|
||||
system_href = reverse("system_settings")
|
||||
accessibility_href = reverse("accessibility_settings")
|
||||
encryption_href = reverse("encryption_settings")
|
||||
permissions_href = reverse("permission_settings")
|
||||
security_2fa_href = reverse("security_2fa")
|
||||
ai_models_href = reverse("ai_models")
|
||||
ai_traces_href = reverse("ai_execution_log")
|
||||
commands_href = reverse("command_routing")
|
||||
tasks_href = reverse("tasks_settings")
|
||||
translation_href = reverse("translation_settings")
|
||||
availability_href = reverse("availability_settings")
|
||||
|
||||
general_routes = {
|
||||
"notifications_settings",
|
||||
"notifications_update",
|
||||
"system_settings",
|
||||
"accessibility_settings",
|
||||
}
|
||||
security_routes = {
|
||||
"security_settings",
|
||||
"encryption_settings",
|
||||
"permission_settings",
|
||||
"security_2fa",
|
||||
}
|
||||
ai_routes = {
|
||||
"ai_settings",
|
||||
"ai_models",
|
||||
"ais",
|
||||
"ai_create",
|
||||
"ai_update",
|
||||
"ai_delete",
|
||||
"ai_execution_log",
|
||||
}
|
||||
modules_routes = {
|
||||
"modules_settings",
|
||||
"command_routing",
|
||||
"tasks_settings",
|
||||
"translation_settings",
|
||||
"availability_settings",
|
||||
}
|
||||
|
||||
two_factor_security_routes = {
|
||||
"profile",
|
||||
"setup",
|
||||
"backup_tokens",
|
||||
"disable",
|
||||
"phone_create",
|
||||
"phone_delete",
|
||||
}
|
||||
|
||||
if url_name in general_routes:
|
||||
settings_nav = {
|
||||
"title": "General",
|
||||
"tabs": [
|
||||
_tab("Notifications", notifications_href, path == notifications_href),
|
||||
_tab("System", system_href, path == system_href),
|
||||
_tab("Accessibility", accessibility_href, path == accessibility_href),
|
||||
],
|
||||
}
|
||||
elif url_name in security_routes or (
|
||||
namespace == "two_factor" and url_name in two_factor_security_routes
|
||||
):
|
||||
settings_nav = {
|
||||
"title": "Security",
|
||||
"tabs": [
|
||||
_tab("Encryption", encryption_href, path == encryption_href),
|
||||
_tab("Permissions", permissions_href, path == permissions_href),
|
||||
_tab(
|
||||
"2FA",
|
||||
security_2fa_href,
|
||||
path == security_2fa_href or namespace == "two_factor",
|
||||
),
|
||||
],
|
||||
}
|
||||
elif url_name in ai_routes:
|
||||
settings_nav = {
|
||||
"title": "AI",
|
||||
"tabs": [
|
||||
_tab("Models", ai_models_href, path == ai_models_href),
|
||||
_tab("Traces", ai_traces_href, path == ai_traces_href),
|
||||
],
|
||||
}
|
||||
elif url_name in modules_routes:
|
||||
settings_nav = {
|
||||
"title": "Modules",
|
||||
"tabs": [
|
||||
_tab("Commands", commands_href, path == commands_href),
|
||||
_tab("Tasks", tasks_href, path == tasks_href),
|
||||
_tab("Translation", translation_href, path == translation_href),
|
||||
_tab("Availability", availability_href, path == availability_href),
|
||||
],
|
||||
}
|
||||
else:
|
||||
settings_nav = None
|
||||
|
||||
if not settings_nav:
|
||||
return {}
|
||||
return {"settings_nav": settings_nav}
|
||||
Reference in New Issue
Block a user