]> git.ipfire.org Git - dbl.git/commitdiff
api: Create a new endpoint to submit reports
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 2 Mar 2026 17:56:29 +0000 (17:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 2 Mar 2026 17:56:29 +0000 (17:56 +0000)
This makes the API slightly cleaner and moves all report things
together.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dbl/api/reports.py

index 8f0e340e49de97faba4f1c90412f93c086692a89..4853ddabfee92a768fd26d2cdbe383a53e7726bf 100644 (file)
@@ -26,9 +26,24 @@ import uuid
 from .. import reports
 
 # Import the main app
+from . import require_api_key
 from . import app
 from . import backend
 
+class CreateReport(pydantic.BaseModel):
+       # List
+       list: str
+
+       # Domain
+       name: str
+
+       # Comment
+       comment: str = ""
+
+       # Block?
+       block: bool = True
+
+
 # Create a router
 router = fastapi.APIRouter(
        prefix="/reports",
@@ -52,6 +67,24 @@ async def get_report_from_path(id: uuid.UUID = fastapi.Path(...)) -> reports.Rep
 async def get_reports() -> typing.List[reports.Report]:
        return [l async for l in backend.reports]
 
+@router.post("")
+async def create_report(
+       report: CreateReport,
+       user = fastapi.Depends(require_api_key),
+) -> reports.Report:
+       # Fetch the list
+       list = await backend.lists.get_by_slug(report.list)
+       if not list:
+               raise fastapi.HTTPException(400, "Could not find list '%s'" % report.list)
+
+       # Create the report
+       return await list.report(
+               name        = report.name,
+               reported_by = user,
+               comment     = report.comment,
+               block       = report.block,
+       )
+
 @router.get("/{id}")
 async def get_report(report = fastapi.Depends(get_report_from_path)) -> reports.Report:
        return report