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" %}