mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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:
parent
f849e8b696
commit
5e95cfbc8a
3 changed files with 19 additions and 16 deletions
|
@ -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")
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
{% block pageContent %}
|
||||
{% include "partials/packages/search.html" %}
|
||||
<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 %}
|
||||
{% include "partials/packages/actions.html" %}
|
||||
|
|
Loading…
Add table
Reference in a new issue