diff --git a/packetserver/http/routers/objects_html.py b/packetserver/http/routers/objects_html.py index 3cb02e1..6df8b60 100644 --- a/packetserver/http/routers/objects_html.py +++ b/packetserver/http/routers/objects_html.py @@ -1,10 +1,15 @@ from fastapi import APIRouter, Depends, Request, Form, File, UploadFile from fastapi.responses import HTMLResponse, RedirectResponse +from uuid import UUID +import base64 + from packetserver.http.dependencies import get_current_http_user from packetserver.http.auth import HttpUser from packetserver.http.server import templates from packetserver.http.routers.objects import router as api_router # to call internal endpoints from packetserver.http.database import DbDependency +from packetserver.http.routers.objects import get_object_metadata as api_get_metadata + router = APIRouter(tags=["objects_html"]) @@ -28,4 +33,55 @@ async def objects_page( "current_user": current_user.username, "objects": objects } - ) \ No newline at end of file + ) + +@router.get("/objects/{uuid}", response_class=HTMLResponse) +async def object_detail_page( + request: Request, + uuid: UUID, + db: DbDependency, + current_user: HttpUser = Depends(get_current_http_user) +): + # Call the existing metadata API function + obj = api_get_metadata(uuid=uuid, db=db, current_user=current_user) + + return templates.TemplateResponse( + "object_detail.html", + { + "request": request, + "current_user": current_user.username, + "obj": obj + } + ) + +@router.post("/objects/{uuid}") +async def update_object( + db: DbDependency, + uuid: UUID, + request: Request, + name: str = Form(None), + private: str = Form("off"), # checkbox sends "on" if checked + new_text: str = Form(None), + new_file: UploadFile = File(None), + new_base64: str = Form(None), + current_user: HttpUser = Depends(get_current_http_user) +): + payload = {} + if name is not None: + payload["name"] = name + payload["private"] = (private == "on") + + if new_text is not None and new_text.strip(): + payload["data_text"] = new_text.strip() + elif new_file and new_file.filename: + content = await new_file.read() + payload["data_base64"] = base64.b64encode(content).decode('ascii') + elif new_base64 and new_base64.strip(): + payload["data_base64"] = new_base64.strip() + + # Call the PATCH API internally (simple requests or direct function call) + from packetserver.http.routers.objects import update_object as api_update + await api_update(uuid=uuid, payload=ObjectUpdate(**payload), db=db, current_user=current_user) + + # Redirect back to the detail page (or /objects list) + return RedirectResponse(url=f"/objects/{uuid}", status_code=303) \ No newline at end of file diff --git a/packetserver/http/templates/object_detail.html b/packetserver/http/templates/object_detail.html new file mode 100644 index 0000000..36e6756 --- /dev/null +++ b/packetserver/http/templates/object_detail.html @@ -0,0 +1,86 @@ +{% extends "base.html" %} + +{% block title %}{{ obj.name }} - Edit Object{% endblock %} + +{% block content %} +