From: Michael Tremer Date: Tue, 3 Mar 2026 11:02:35 +0000 (+0000) Subject: api: Add a handler to post comments to reports X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=661bff9afe7ab7dbd25dfe289ae6bfe584537ba8;p=dbl.git api: Add a handler to post comments to reports Signed-off-by: Michael Tremer --- diff --git a/src/dbl/api/reports.py b/src/dbl/api/reports.py index 08c0d80..6e1a8e8 100644 --- a/src/dbl/api/reports.py +++ b/src/dbl/api/reports.py @@ -50,6 +50,11 @@ class CloseReport(pydantic.BaseModel): accept: bool = True +class Comment(pydantic.BaseModel): + # Comment + comment: str + + # Create a router router = fastapi.APIRouter( prefix="/reports", @@ -119,5 +124,21 @@ async def get_comments( ) -> list[reports.ReportComment]: return [comment async for comment in report.get_comments()] +@router.post("/{id}/comments") +async def submit_comment( + comment: Comment, + report: reports.Report = fastapi.Depends(get_report_from_path), + user: users.User = fastapi.Depends(require_current_user), +) -> reports.ReportComment: + """ + Posts a comment to a report + """ + # Fail if the report has already been closed + if report.is_closed(): + raise fastapi.HTTPException(409, "The report is closed and cannot receive any comments") + + # Create the comment + return await report.comment(comment=comment.comment, reporter=user) + # Include our endpoints app.include_router(router) diff --git a/src/dbl/reports.py b/src/dbl/reports.py index de6c186..3b46d72 100644 --- a/src/dbl/reports.py +++ b/src/dbl/reports.py @@ -446,6 +446,9 @@ class Report(sqlmodel.SQLModel, database.BackendMixin, table=True): """ Posts a new comment to this report """ + if isinstance(reporter, users.User): + reporter = reporter.uid + # Write the comment to the database comment = await self.backend.db.insert( ReportComment, @@ -455,6 +458,9 @@ class Report(sqlmodel.SQLModel, database.BackendMixin, table=True): points = points, ) + # Manifest the object in the database immediately to assign the ID + await self.backend.db.flush_and_refresh(comment) + # Send a notification about this comment if notify: pass # XXX TODO