diff --git a/packetserver/http/routers/bulletins.py b/packetserver/http/routers/bulletins.py index 6c0b6df..ceaf0d4 100644 --- a/packetserver/http/routers/bulletins.py +++ b/packetserver/http/routers/bulletins.py @@ -1,5 +1,5 @@ -from fastapi import APIRouter, Path, Query, Depends, HTTPException, Request, status -from fastapi.responses import HTMLResponse +from fastapi import APIRouter, Path, Query, Depends, HTTPException, Request, status, Form +from fastapi.responses import HTMLResponse, RedirectResponse from typing import Optional, List from pydantic import BaseModel, Field, constr from datetime import datetime @@ -130,6 +130,48 @@ async def bulletin_list_page( {"request": request, "bulletins": bulletins, "current_user": current_user.username} ) +@html_router.get("/bulletins/new", response_class=HTMLResponse) +async def bulletin_new_form( + request: Request, + current_user: HttpUser = Depends(get_current_http_user) # require login, consistent with site +): + return templates.TemplateResponse( + "bulletin_new.html", + {"request": request, "error": None} + ) + +@html_router.post("/bulletins/new") +async def bulletin_new_submit( + request: Request, + subject: str = Form(...), + body: str = Form(...), + current_user: HttpUser = Depends(get_current_http_user) +): + if not subject.strip() or not body.strip(): + return templates.TemplateResponse( + "bulletin_new.html", + {"request": request, "error": "Subject and body are required."}, + status_code=400 + ) + from packetserver.runners.http_server import get_db_connection + conn = get_db_connection() + root = conn.root() + + if 'bulletins' not in root: + root['bulletins'] = PersistentList() + + new_bulletin = Bulletin( + author=current_user.username, + subject=subject.strip(), + text=body.strip() + ) + + new_id = new_bulletin.write_new(root) + + transaction.commit() + + return RedirectResponse(url=f"/bulletins/{new_id}", status_code=303) + @html_router.get("/bulletins/{bid}", response_class=HTMLResponse) async def bulletin_detail_page( request: Request, diff --git a/packetserver/http/routers/dashboard.py b/packetserver/http/routers/dashboard.py index 7b0e42a..bc60540 100644 --- a/packetserver/http/routers/dashboard.py +++ b/packetserver/http/routers/dashboard.py @@ -10,6 +10,7 @@ router = APIRouter(tags=["dashboard"]) # Import the function at module level (safe now that circular import is fixed) from packetserver.http.routers.messages import get_messages as api_get_messages +from .bulletins import list_bulletins @router.get("/dashboard", response_class=HTMLResponse) @@ -26,9 +27,17 @@ async def dashboard( ) messages = messages_resp["messages"] + bulletins_resp = await list_bulletins(limit=10, since=None) + recent_bulletins = bulletins_resp["bulletins"] + return templates.TemplateResponse( "dashboard.html", - {"request": request, "current_user": current_user.username, "messages": messages} + { + "request": request, + "current_user": current_user.username, + "messages": messages, + "bulletins": recent_bulletins + } ) @router.get("/dashboard/profile", response_class=HTMLResponse) diff --git a/packetserver/http/static/css/style.css b/packetserver/http/static/css/style.css new file mode 100644 index 0000000..e69de29 diff --git a/packetserver/http/templates/base.html b/packetserver/http/templates/base.html index 1f075d0..cc3f2dd 100644 --- a/packetserver/http/templates/base.html +++ b/packetserver/http/templates/base.html @@ -6,6 +6,12 @@ {% block title %}PacketServer Dashboard{% endblock %} {# optional custom #} +