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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-27 14:58:07 -07:00
parent f849e8b696
commit 5e95cfbc8a
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 19 additions and 16 deletions

View file

@ -1,6 +1,6 @@
from collections import defaultdict from collections import defaultdict
from http import HTTPStatus from http import HTTPStatus
from typing import Dict, List from typing import Dict, List, Union
import orjson import orjson
@ -101,24 +101,24 @@ def provides_list(package: Package, depname: str) -> list:
return string 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 """ 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 :param name: {Package,PackageBase}.Name
:raises HTTPException: With status code 404 if PackageBase doesn't exist :raises HTTPException: With status code 404 if record doesn't exist
:return: PackageBase instance :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( provider = db.query(OfficialProvider).filter(
OfficialProvider.Name == name).first() OfficialProvider.Name == name).first()
if provider: if provider:
raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND)) 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") @register_filter("out_of_date")

View file

@ -23,7 +23,7 @@ from aurweb.models.package_source import PackageSource
from aurweb.models.package_vote import PackageVote from aurweb.models.package_vote import PackageVote
from aurweb.models.relation_type import CONFLICTS_ID from aurweb.models.relation_type import CONFLICTS_ID
from aurweb.packages.search import PackageSearch 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 from aurweb.templates import make_context, render_template
router = APIRouter() router = APIRouter()
@ -143,11 +143,14 @@ async def make_single_context(request: Request,
@router.get("/packages/{name}") @router.get("/packages/{name}")
async def package(request: Request, name: str) -> Response: async def package(request: Request, name: str) -> Response:
# Get the PackageBase. # Get the Package.
pkgbase = get_pkgbase(name) pkg = get_pkg_or_base(name, Package)
pkgbase = (get_pkg_or_base(name, PackageBase)
if not pkg else pkg.PackageBase)
# Add our base information. # Add our base information.
context = await make_single_context(request, pkgbase) context = await make_single_context(request, pkgbase)
context["package"] = pkg
# Package sources. # Package sources.
context["sources"] = db.query(PackageSource).join(Package).join( context["sources"] = db.query(PackageSource).join(Package).join(
@ -181,7 +184,7 @@ async def package(request: Request, name: str) -> Response:
@router.get("/pkgbase/{name}") @router.get("/pkgbase/{name}")
async def package_base(request: Request, name: str) -> Response: async def package_base(request: Request, name: str) -> Response:
# Get the PackageBase. # 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 this is not a split package, redirect to /packages/{name}.
if pkgbase.packages.count() == 1: if pkgbase.packages.count() == 1:

View file

@ -3,7 +3,7 @@
{% block pageContent %} {% block pageContent %}
{% include "partials/packages/search.html" %} {% include "partials/packages/search.html" %}
<div id="pkgdetails" class="box"> <div id="pkgdetails" class="box">
<h2>{{ 'Package Details' | tr }}: {{ pkgbase.Name }} {{ pkgbase.packages.first().Version }}</h2> <h2>{{ 'Package Details' | tr }}: {{ package.Name }} {{ package.Version }}</h2>
{% set result = pkgbase %} {% set result = pkgbase %}
{% include "partials/packages/actions.html" %} {% include "partials/packages/actions.html" %}