From 5e95cfbc8a844c11682db57186ac01d9732e631d Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 27 Sep 2021 14:58:07 -0700 Subject: [PATCH] fix(FastAPI): get_pkgbase -> get_pkg_or_base `get_pkgbase` has been replaced with `get_pkg_or_base`, which is quite similar, but it does take a new `cls` keyword argument which is to be the model class which we search for via its `Name` column. Additionally, this change fixes a bug in the `/packages/{name}` route by supplying the Package object in question to the context and modifying the template to use it instead of a hacky through-base workaround. Examples: pkgbase = get_pkg_or_base("some_pkgbase_name", PackageBase) pkg = get_pkg_or_base("some_package_name", Package) Signed-off-by: Kevin Morris --- aurweb/packages/util.py | 22 +++++++++++----------- aurweb/routers/packages.py | 11 +++++++---- templates/packages/show.html | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/aurweb/packages/util.py b/aurweb/packages/util.py index 18ac7a5a..696c158f 100644 --- a/aurweb/packages/util.py +++ b/aurweb/packages/util.py @@ -1,6 +1,6 @@ from collections import defaultdict from http import HTTPStatus -from typing import Dict, List +from typing import Dict, List, Union import orjson @@ -101,24 +101,24 @@ def provides_list(package: Package, depname: str) -> list: return string -def get_pkgbase(name: str) -> PackageBase: +def get_pkg_or_base(name: str, cls: Union[Package, PackageBase] = PackageBase): """ Get a PackageBase instance by its name or raise a 404 if - it can't be foudn in the database. + it can't be found in the database. - :param name: PackageBase.Name - :raises HTTPException: With status code 404 if PackageBase doesn't exist - :return: PackageBase instance + :param name: {Package,PackageBase}.Name + :raises HTTPException: With status code 404 if record doesn't exist + :return: {Package,PackageBase} instance """ - pkgbase = db.query(PackageBase).filter(PackageBase.Name == name).first() - if not pkgbase: - raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND)) - provider = db.query(OfficialProvider).filter( OfficialProvider.Name == name).first() if provider: raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND)) - return pkgbase + instance = db.query(cls).filter(cls.Name == name).first() + if cls == PackageBase and not instance: + raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND)) + + return instance @register_filter("out_of_date") diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index aa20e5fa..8fd7717b 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -23,7 +23,7 @@ from aurweb.models.package_source import PackageSource from aurweb.models.package_vote import PackageVote from aurweb.models.relation_type import CONFLICTS_ID from aurweb.packages.search import PackageSearch -from aurweb.packages.util import get_pkgbase, query_notified, query_voted +from aurweb.packages.util import get_pkg_or_base, query_notified, query_voted from aurweb.templates import make_context, render_template router = APIRouter() @@ -143,11 +143,14 @@ async def make_single_context(request: Request, @router.get("/packages/{name}") async def package(request: Request, name: str) -> Response: - # Get the PackageBase. - pkgbase = get_pkgbase(name) + # Get the Package. + pkg = get_pkg_or_base(name, Package) + pkgbase = (get_pkg_or_base(name, PackageBase) + if not pkg else pkg.PackageBase) # Add our base information. context = await make_single_context(request, pkgbase) + context["package"] = pkg # Package sources. context["sources"] = db.query(PackageSource).join(Package).join( @@ -181,7 +184,7 @@ async def package(request: Request, name: str) -> Response: @router.get("/pkgbase/{name}") async def package_base(request: Request, name: str) -> Response: # Get the PackageBase. - pkgbase = get_pkgbase(name) + pkgbase = get_pkg_or_base(name, PackageBase) # If this is not a split package, redirect to /packages/{name}. if pkgbase.packages.count() == 1: diff --git a/templates/packages/show.html b/templates/packages/show.html index 7480f573..0bf8d9fd 100644 --- a/templates/packages/show.html +++ b/templates/packages/show.html @@ -3,7 +3,7 @@ {% block pageContent %} {% include "partials/packages/search.html" %}
-

{{ 'Package Details' | tr }}: {{ pkgbase.Name }} {{ pkgbase.packages.first().Version }}

+

{{ 'Package Details' | tr }}: {{ package.Name }} {{ package.Version }}

{% set result = pkgbase %} {% include "partials/packages/actions.html" %}