fix: pkgbase -> package redirection

We were redirecting in some error-cases, which this commit sorts out:
- package count == 1 and package base name != package name
- was redirecting to {name} and not the only associated Package

Now, when we have a package base name that mismatches its only
package, we display the package base page. Otherwise, we redirect
to the first package's page.

Closes #282

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-02-14 15:24:14 -08:00
parent 708ade4dbf
commit 29061c000c
No known key found for this signature in database
GPG key ID: F7E46DED420788F3

View file

@ -39,14 +39,17 @@ async def pkgbase(request: Request, name: str) -> Response:
# Get the PackageBase.
pkgbase = get_pkg_or_base(name, PackageBase)
# If this is not a split package, redirect to /packages/{name}.
if pkgbase.packages.count() == 1:
return RedirectResponse(f"/packages/{name}",
# Redirect to /packages if there's only one related Package
# and its name matches its PackageBase.
packages = pkgbase.packages.all()
pkg = packages[0]
if len(packages) == 1 and pkg.Name == pkgbase.Name:
return RedirectResponse(f"/packages/{pkg.Name}",
status_code=int(HTTPStatus.SEE_OTHER))
# Add our base information.
context = pkgbaseutil.make_context(request, pkgbase)
context["packages"] = pkgbase.packages.all()
context["packages"] = packages
return render_template(request, "pkgbase/index.html", context)