]> git.ipfire.org Git - dbl.git/commitdiff
api: Add a handler to post comments to reports
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 11:02:35 +0000 (11:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 11:02:35 +0000 (11:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dbl/api/reports.py
src/dbl/reports.py

index 08c0d80b3329a5395e29ad73993000bcf79570f9..6e1a8e84ac7c986c40051f8e00316eea474c8de3 100644 (file)
@@ -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)
index de6c1868ca29dfa22496d0c6de09a071a3e3dc45..3b46d72d22775ed2efb6c69cfeabd9e5de503333 100644 (file)
@@ -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