Adding bulletin support.
This commit is contained in:
74
packetserver/http/routers/bulletins.py
Normal file
74
packetserver/http/routers/bulletins.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# packetserver/http/routers/bulletins.py
|
||||
from fastapi import APIRouter, Path, Query, Depends, HTTPException
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import transaction
|
||||
|
||||
from packetserver.http.dependencies import get_current_http_user
|
||||
from packetserver.http.auth import HttpUser
|
||||
from packetserver.server.bulletin import Bulletin # core class
|
||||
|
||||
router = APIRouter(prefix="/api/v1", tags=["bulletins"])
|
||||
|
||||
|
||||
@router.get("/bulletins")
|
||||
async def list_bulletins(
|
||||
limit: Optional[int] = Query(20, le=100, description="Max bulletins to return"),
|
||||
since: Optional[str] = Query(None, description="ISO UTC timestamp filter")
|
||||
):
|
||||
from packetserver.runners.http_server import get_db_connection
|
||||
conn = get_db_connection()
|
||||
root = conn.root()
|
||||
|
||||
bulletins_list = root.get('bulletins', [])
|
||||
if not bulletins_list:
|
||||
return {"bulletins": [], "total": 0}
|
||||
|
||||
since_dt = None
|
||||
if since:
|
||||
try:
|
||||
since_dt = datetime.fromisoformat(since.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="Invalid 'since' format")
|
||||
|
||||
filtered = []
|
||||
for bull in reversed(bulletins_list): # newest first (assuming appended order)
|
||||
if since_dt and bull.created_at < since_dt:
|
||||
continue
|
||||
filtered.append({
|
||||
"id": bull.id,
|
||||
"author": bull.author,
|
||||
"subject": bull.subject,
|
||||
"body": bull.body,
|
||||
"created_at": bull.created_at.isoformat() + "Z",
|
||||
"updated_at": bull.updated_at.isoformat() + "Z",
|
||||
})
|
||||
|
||||
return {
|
||||
"bulletins": filtered[:limit],
|
||||
"total": len(filtered)
|
||||
}
|
||||
|
||||
|
||||
@router.get("/bulletins/{bid}")
|
||||
async def get_bulletin(
|
||||
bid: int = Path(..., description="Bulletin ID"),
|
||||
current_user: HttpUser = Depends(get_current_http_user) # require auth, but public read
|
||||
):
|
||||
from packetserver.runners.http_server import get_db_connection
|
||||
conn = get_db_connection()
|
||||
root = conn.root()
|
||||
|
||||
bulletins_list = root.get('bulletins', [])
|
||||
for bull in bulletins_list:
|
||||
if bull.id == bid:
|
||||
return {
|
||||
"id": bull.id,
|
||||
"author": bull.author,
|
||||
"subject": bull.subject,
|
||||
"body": bull.body,
|
||||
"created_at": bull.created_at.isoformat() + "Z",
|
||||
"updated_at": bull.updated_at.isoformat() + "Z",
|
||||
}
|
||||
|
||||
raise HTTPException(status_code=404, detail="Bulletin not found")
|
||||
@@ -4,7 +4,7 @@ from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pathlib import Path
|
||||
|
||||
from .routers import public, profile, messages, send # dashboard removed for now
|
||||
from .routers import public, profile, messages, send, bulletins
|
||||
|
||||
BASE_DIR = Path(__file__).parent.resolve()
|
||||
|
||||
@@ -28,4 +28,5 @@ app.include_router(public.router)
|
||||
app.include_router(profile.router)
|
||||
app.include_router(messages.router)
|
||||
app.include_router(send.router)
|
||||
app.include_router(dashboard.router) # now works
|
||||
app.include_router(dashboard.router)
|
||||
app.include_router(bulletins.router)
|
||||
Reference in New Issue
Block a user