distro-tools/apollo/server/routes/api_advisories.py

78 lines
2.0 KiB
Python
Raw Normal View History

2023-02-04 23:24:31 +00:00
from typing import TypeVar, Generic, Optional
2023-02-01 21:37:16 +00:00
2023-02-04 23:24:31 +00:00
from fastapi import APIRouter, Depends
2023-02-01 21:37:16 +00:00
from fastapi.exceptions import HTTPException
2023-02-04 23:24:31 +00:00
from fastapi_pagination import Params
2023-02-01 21:37:16 +00:00
from fastapi_pagination.links import Page
from fastapi_pagination.ext.tortoise import paginate
2023-03-23 16:01:52 +00:00
from apollo.db import Advisory, RedHatIndexState
2023-02-01 21:37:16 +00:00
from apollo.db.serialize import Advisory_Pydantic
router = APIRouter(tags=["advisories"])
T = TypeVar("T")
class Pagination(Page[T], Generic[T]):
2023-03-23 16:01:52 +00:00
last_updated_at: Optional[str]
2023-02-01 21:37:16 +00:00
class Config:
allow_population_by_field_name = True
fields = {"items": {"alias": "advisories"}}
@router.get(
"/",
response_model=Pagination[Advisory_Pydantic],
)
2023-02-04 23:24:31 +00:00
async def list_advisories(
params: Params = Depends(),
product: Optional[str] = None,
before_raw: Optional[str] = None,
after_raw: Optional[str] = None,
cve: Optional[str] = None,
synopsis: Optional[str] = None,
keyword: Optional[str] = None,
severity: Optional[str] = None,
kind: Optional[str] = None,
):
2023-02-01 21:37:16 +00:00
advisories = await paginate(
Advisory.all().prefetch_related(
"red_hat_advisory",
"packages",
"cves",
"fixes",
"affected_products",
).order_by("-published_at"),
)
2023-03-23 16:01:52 +00:00
state = await RedHatIndexState.first()
advisories.last_updated_at = state.last_indexed_at.isoformat("T").replace(
"+00:00",
"",
) + "Z"
2023-02-01 21:37:16 +00:00
return advisories
@router.get(
"/{advisory_name}",
response_model=Advisory_Pydantic,
)
async def get_advisory(advisory_name: str):
advisory = await Advisory.filter(name=advisory_name).prefetch_related(
"packages",
"cves",
"fixes",
"affected_products",
2023-02-04 23:24:31 +00:00
"packages",
"packages__supported_product",
"packages__supported_products_rh_mirror",
).get_or_none()
2023-02-01 21:37:16 +00:00
if advisory is None:
raise HTTPException(404)
return await Advisory_Pydantic.from_tortoise_orm(advisory)