76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
from django.http import HttpRequest, HttpResponseForbidden
|
|
from django.shortcuts import redirect
|
|
from django.utils.html import format_html
|
|
from datetime import datetime
|
|
|
|
APP_META = {
|
|
"ax4": {"root": "/", "icon": "logo.png", "name": "Axia<sup>4</sup>", "title": "Axia4"},
|
|
"account": {"root": "/account/", "icon": "logo-account.png", "name": "Mi Cuenta", "title": "Mi Cuenta"},
|
|
"aulatek": {"root": "/aulatek/", "icon": "logo-entreaulas.png", "name": "AulaTek", "title": "AulaTek"},
|
|
"club": {"root": "/club/", "icon": "logo-club.png", "name": "La web del Club<sup>3</sup>", "title": "La web del Club"},
|
|
"sysadmin": {"root": "/sysadmin/", "icon": "logo-sysadmin.png", "name": "SysAdmin", "title": "SysAdmin"},
|
|
}
|
|
|
|
|
|
def safe_redirect(value: str, default: str = "/") -> str:
|
|
if value and value.startswith("/") and not value.startswith("//"):
|
|
return value
|
|
return default
|
|
|
|
|
|
def require_axia_login(request: HttpRequest):
|
|
if not getattr(request, "axia_user", None):
|
|
return redirect(f"/login/?redir={request.get_full_path()}")
|
|
return None
|
|
|
|
|
|
def require_permission(request: HttpRequest, permission: str):
|
|
gate = require_axia_login(request)
|
|
if gate:
|
|
return gate
|
|
if not request.axia_user.has_permission(permission):
|
|
return HttpResponseForbidden("No tienes permiso para acceder a esta seccion.")
|
|
return None
|
|
|
|
|
|
def build_sidebar(app_code: str, request: HttpRequest):
|
|
current_path = request.path
|
|
if app_code == "account":
|
|
items = [
|
|
{"href": "/logout/", "label": "Cerrar sesion"},
|
|
]
|
|
elif app_code == "aulatek":
|
|
items = [
|
|
{"section": "Atajos", "href": "/aulatek/", "label": "Aulas"},
|
|
{"href": "/aulatek/supercafe/", "label": "SuperCafe", "icon": "/static/iconexperience/purchase_order_cart.png"},
|
|
]
|
|
elif app_code == "sysadmin":
|
|
items = [
|
|
{"section": "Atajos", "href": "/sysadmin/users/", "label": "Nuevo usuario"},
|
|
]
|
|
elif app_code == "club":
|
|
today_str = datetime.now().strftime("%Y-%m-%d")
|
|
items = [
|
|
{"href": "/club/", "label": "Calendario de salidas"},
|
|
{"href": f"/club/cal/{today_str}/", "label": "Salida de hoy"},
|
|
{"href": "/club/upload/", "label": "Subir fotos"},
|
|
]
|
|
else:
|
|
items = []
|
|
|
|
for item in items:
|
|
item["active"] = current_path == item.get("href")
|
|
return items
|
|
|
|
|
|
def shell_context(request: HttpRequest, app_code: str, page_title: str):
|
|
meta = APP_META[app_code]
|
|
return {
|
|
"page_title": page_title,
|
|
"app_root": meta["root"],
|
|
"app_icon": meta["icon"],
|
|
"app_name": format_html(meta["name"]),
|
|
"sidebar_links": build_sidebar(app_code, request),
|
|
"app_code": app_code,
|
|
}
|