Neue Route /admin/geraete/{device_id}/log fasst device_merkmal_events
(Auftragskatalog, bereits bestehend) und die neuen
device_installation_events (Migration 0025 in provisioning-server,
Boot-Medium-Meilensteine/Fehlschläge) chronologisch zusammen - eigenes
Template (templates/admin/geraete_log.html), bewusst getrennt von der
Kunden-Seite (templates/geraete_log.html bleibt unverändert), da noch
offen ist, ob/wie umfangreich Installationsereignisse dem Kunden selbst
gezeigt werden sollen.
162 lines
6.2 KiB
Python
162 lines
6.2 KiB
Python
from fastapi import APIRouter, Depends, Form, HTTPException, Request
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
from anode_client import anode_request
|
|
from auth import require_admin
|
|
from routers.geraete import OPTIONEN_FELDER, anreichere_mit_quelle_url, gruppiere_nach_kategorie
|
|
from templating import templates
|
|
|
|
|
|
router = APIRouter(prefix="/admin/geraete")
|
|
|
|
|
|
def find_device(device_id: str) -> dict | None:
|
|
result = anode_request("GET", "/api/v1/devices")
|
|
devices = result.get("devices", []) if result.get("success") else []
|
|
|
|
for device in devices:
|
|
if device["id"] == device_id:
|
|
return device
|
|
|
|
return None
|
|
|
|
|
|
@router.get("")
|
|
def index(request: Request, user: dict = Depends(require_admin)):
|
|
result = anode_request("GET", "/api/v1/devices")
|
|
devices = result.get("devices", []) if result.get("success") else []
|
|
|
|
# Admin-Liste ist organisationsübergreifend - jede Zeile braucht die
|
|
# OE-Auswahl ihrer eigenen Organisation für "In OE verschieben", daher
|
|
# je vorkommender Organisation einmal (nicht pro Gerät) abfragen.
|
|
oe_by_org: dict[str, list[dict]] = {}
|
|
for organization_id in {device["organization_id"] for device in devices}:
|
|
oe_result = anode_request("GET", f"/api/v1/organizations/{organization_id}/organisationseinheiten")
|
|
oe_by_org[organization_id] = oe_result.get("organisationseinheiten", []) if oe_result.get("success") else []
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"admin/geraete_liste.html",
|
|
{"user": user, "devices": devices, "oe_by_org": oe_by_org},
|
|
)
|
|
|
|
|
|
@router.post("/{device_id}/oe")
|
|
def oe_verschieben(device_id: str, oe_id: str = Form(...), user: dict = Depends(require_admin)):
|
|
if find_device(device_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/oe", json={"oe_id": oe_id})
|
|
|
|
return RedirectResponse("/admin/geraete", status_code=303)
|
|
|
|
|
|
@router.get("/{device_id}/log")
|
|
def log_ansicht(request: Request, device_id: str, user: dict = Depends(require_admin)):
|
|
device = find_device(device_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
# Merkmal-Anwendungen (Auftragskatalog) UND Installations-Meilensteine/
|
|
# Fehlschlaege des Boot-Mediums selbst (siehe device_installation_events,
|
|
# Migration 0025 in provisioning-server) zusammengefuehrt, neueste
|
|
# zuerst - bewusst nur hier im Admin-Bereich (siehe
|
|
# templates/admin/geraete_log.html), nicht auf der Kunden-Seite.
|
|
merkmal_result = anode_request("GET", f"/api/v1/devices/{device_id}/events")
|
|
merkmal_events = merkmal_result.get("events", []) if merkmal_result.get("success") else []
|
|
|
|
installation_result = anode_request("GET", f"/api/v1/devices/{device_id}/installation-events")
|
|
installation_events = installation_result.get("events", []) if installation_result.get("success") else []
|
|
|
|
events = sorted(merkmal_events + installation_events, key=lambda e: e["occurred_at"], reverse=True)
|
|
|
|
return templates.TemplateResponse(
|
|
request, "admin/geraete_log.html", {"user": user, "device": device, "events": events}
|
|
)
|
|
|
|
|
|
@router.get("/{device_id}")
|
|
def auftragskatalog_ansicht(request: Request, device_id: str, user: dict = Depends(require_admin)):
|
|
device = find_device(device_id)
|
|
|
|
if device is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
result = anode_request("GET", f"/api/v1/devices/{device_id}/auftragskatalog")
|
|
katalog = result.get("auftragskatalog", []) if result.get("success") else []
|
|
fehler = None if result.get("success") else result.get("message")
|
|
|
|
katalog = anreichere_mit_quelle_url(
|
|
katalog,
|
|
f"/admin/kunden/{device['organization_id']}/organisationseinheiten",
|
|
f"/admin/kunden/{device['organization_id']}/gruppen",
|
|
)
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"auftragskatalog.html",
|
|
{
|
|
"user": user,
|
|
"device": device,
|
|
"kategorien": gruppiere_nach_kategorie(katalog),
|
|
"fehler": fehler,
|
|
"optionen_felder": OPTIONEN_FELDER,
|
|
"zurueck_url": "/admin/geraete",
|
|
},
|
|
)
|
|
|
|
|
|
@router.post("/{device_id}/auftragskatalog/{merkmal_key}/select")
|
|
async def auftrag_select(
|
|
request: Request, device_id: str, merkmal_key: str, user: dict = Depends(require_admin)
|
|
):
|
|
if find_device(device_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
form = await request.form()
|
|
optionen = {
|
|
feld["key"]: (form.get(f"optionen.{feld['key']}") == "on")
|
|
for feld in OPTIONEN_FELDER.get(merkmal_key, [])
|
|
}
|
|
|
|
anode_request(
|
|
"POST",
|
|
f"/api/v1/devices/{device_id}/auftragskatalog/{merkmal_key}/select",
|
|
json={"optionen": optionen},
|
|
)
|
|
|
|
return RedirectResponse(f"/admin/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/{device_id}/auftragskatalog/{merkmal_key}/deselect")
|
|
def auftrag_deselect(device_id: str, merkmal_key: str, user: dict = Depends(require_admin)):
|
|
if find_device(device_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request(
|
|
"POST", f"/api/v1/devices/{device_id}/auftragskatalog/{merkmal_key}/deselect"
|
|
)
|
|
|
|
return RedirectResponse(f"/admin/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/{device_id}/debug-modus/aktivieren")
|
|
def debug_modus_aktivieren(device_id: str, user: dict = Depends(require_admin)):
|
|
if find_device(device_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("POST", f"/api/v1/devices/{device_id}/debug-mode")
|
|
|
|
return RedirectResponse(f"/admin/geraete/{device_id}", status_code=303)
|
|
|
|
|
|
@router.post("/{device_id}/debug-modus/deaktivieren")
|
|
def debug_modus_deaktivieren(device_id: str, user: dict = Depends(require_admin)):
|
|
if find_device(device_id) is None:
|
|
raise HTTPException(status_code=404, detail="Gerät wurde nicht gefunden.")
|
|
|
|
anode_request("DELETE", f"/api/v1/devices/{device_id}/debug-mode")
|
|
|
|
return RedirectResponse(f"/admin/geraete/{device_id}", status_code=303)
|