feat: organisationsübergreifende Geräteverwaltung im Admin-Bereich
routers/admin_geraete.py mit echter Funktionalität: alle Geräte aller Organisationen auflisten, pro Gerät der Auftragskatalog (Wiederverwendung von auftragskatalog.html und der Logik aus routers/geraete.py, nur ohne Org-Besitz-Check - Admins dürfen jedes Gerät direkt per ID öffnen). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
b44096b48f
commit
33c4aebfec
@ -1,14 +1,90 @@
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
from anode_client import anode_request
|
||||
from auth import require_admin
|
||||
from routers.geraete import OPTIONEN_FELDER, 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 []
|
||||
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/platzhalter.html", {"user": user, "titel": "Geräte"}
|
||||
request, "admin/geraete_liste.html", {"user": user, "devices": devices}
|
||||
)
|
||||
|
||||
|
||||
@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")
|
||||
|
||||
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)
|
||||
|
||||
27
templates/admin/geraete_liste.html
Normal file
27
templates/admin/geraete_liste.html
Normal file
@ -0,0 +1,27 @@
|
||||
{% extends "admin/layout.html" %}
|
||||
{% block title %}Geräte — Tuxflotte Admin{% endblock %}
|
||||
{% block admin_content %}
|
||||
<h2>Alle Geräte</h2>
|
||||
{% if not devices %}
|
||||
<p>Keine Geräte gefunden.</p>
|
||||
{% else %}
|
||||
<figure>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>Organisation</th><th>Hostname</th><th>Kennung</th><th>Letzter Check-in</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for device in devices %}
|
||||
<tr>
|
||||
<td>{{ device.organization_name }}</td>
|
||||
<td>{{ device.hostname or "—" }}</td>
|
||||
<td><code>{{ device.device_fingerprint }}</code></td>
|
||||
<td>{{ device.agent_last_checkin or "noch nie" }}</td>
|
||||
<td><a href="/admin/geraete/{{ device.id }}" role="button" class="outline">Auftragskatalog →</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</figure>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
Loading…
x
Reference in New Issue
Block a user