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

71 lines
1.8 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
from apollo.db import Advisory
from apollo.db.serialize import Advisory_Pydantic
2023-02-04 23:24:31 +00:00
from apollo.db.advisory import fetch_advisories
2023-02-01 21:37:16 +00:00
router = APIRouter(tags=["advisories"])
T = TypeVar("T")
class Pagination(Page[T], Generic[T]):
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"),
)
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)