From: Michael Tremer Date: Tue, 30 Dec 2025 15:24:33 +0000 (+0000) Subject: lists: Implement fetching reports over the API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e762c5a76b8568f0a8ad26ebbe14c2d5d733c23;p=dbl.git lists: Implement fetching reports over the API Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/api/lists.py b/src/dnsbl/api/lists.py index c2bf310..069c2ce 100644 --- a/src/dnsbl/api/lists.py +++ b/src/dnsbl/api/lists.py @@ -58,6 +58,14 @@ def get_list(list = fastapi.Depends(get_list_from_path)) -> lists.List: def get_list_sources(list = fastapi.Depends(get_list_from_path)) -> typing.List[sources.Source]: return list.sources +@router.get("/{list}/reports") +def get_list_reports( + list = fastapi.Depends(get_list_from_path), + open: bool | None = None, + limit: int | None = None +) -> typing.List[reports.Report]: + return list.get_reports(open=open, limit=limit) + class CreateReport(pydantic.BaseModel): # Domain name : str diff --git a/src/dnsbl/lists.py b/src/dnsbl/lists.py index 276f689..125ce02 100644 --- a/src/dnsbl/lists.py +++ b/src/dnsbl/lists.py @@ -28,6 +28,7 @@ import typing from . import checker from . import database from . import exporters +from . import reports from . import sources from . import util from .i18n import _ @@ -331,6 +332,39 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): # Reports reports : typing.List["Report"] = sqlmodel.Relationship(back_populates="list") + def get_reports(self, open=None, limit=None): + """ + Fetches the most recent reports + """ + stmt = ( + sqlmodel + .select( + reports.Report, + ) + .where( + reports.Report.list == self, + ) + .order_by( + reports.Report.reported_at.desc(), + ) + ) + + # Filter for open/closed reports only + if open is True: + stmt = stmt.where( + reports.Report.closed_at == False, + ) + elif open is False: + stmt = stmt.where( + reports.Report.closed_at == True, + ) + + # Optionally apply the limit + if limit: + stmt = stmt.limit(limit) + + return self.backend.db.fetch_as_list(stmt) + # Report! def report(self, **kwargs):