fix(routers.packages): handle package source display

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-12-30 23:04:39 -08:00
parent be7a96076e
commit 8f8929f324
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 66 additions and 4 deletions

View file

@ -1,13 +1,13 @@
from collections import defaultdict from collections import defaultdict
from http import HTTPStatus from http import HTTPStatus
from typing import Dict, List, Union from typing import Dict, List, Tuple, Union
import orjson import orjson
from fastapi import HTTPException, Request from fastapi import HTTPException, Request
from sqlalchemy import orm from sqlalchemy import orm
from aurweb import db, l10n, models, util from aurweb import config, db, l10n, models, util
from aurweb.models import Package, PackageBase, User from aurweb.models import Package, PackageBase, User
from aurweb.models.official_provider import OFFICIAL_BASE, OfficialProvider from aurweb.models.official_provider import OFFICIAL_BASE, OfficialProvider
from aurweb.models.package_comaintainer import PackageComaintainer from aurweb.models.package_comaintainer import PackageComaintainer
@ -382,3 +382,30 @@ def pkg_required(pkgname: str, provides: List[str], limit: int) \
PackageDependency.DepName.in_(targets) PackageDependency.DepName.in_(targets)
).order_by(Package.Name.asc()).limit(limit) ).order_by(Package.Name.asc()).limit(limit)
return query.all() return query.all()
@register_filter("source_uri")
def source_uri(pkgsrc: models.PackageSource) -> Tuple[str, str]:
"""
Produce a (text, uri) tuple out of `pkgsrc`.
In this filter, we cover various cases:
1. If "::" is anywhere in the Source column, split the string,
which should produce a (text, uri), where text is before "::"
and uri is after "::".
2. Otherwise, if "://" is anywhere in the Source column, it's just
some sort of URI, which we'll return varbatim as both text and uri.
3. Otherwise, we'll return a path to the source file in a uri produced
out of options.source_file_uri formatted with the source file and
the package base name.
:param pkgsrc: PackageSource instance
:return (text, uri) tuple
"""
if "::" in pkgsrc.Source:
return pkgsrc.Source.split("::", 1)
elif "://" in pkgsrc.Source:
return (pkgsrc.Source, pkgsrc.Source)
path = config.get("options", "source_file_uri")
pkgbasename = pkgsrc.Package.PackageBase.Name
return (pkgsrc.Source, path % (pkgsrc.Source, pkgbasename))

View file

@ -65,8 +65,9 @@
<div> <div>
<ul id="pkgsrcslist"> <ul id="pkgsrcslist">
{% for src in sources %} {% for src in sources %}
{% set file, uri = (src | source_uri) %}
<li> <li>
<a href="{{ src.Source }}">{{ src.Source }}</a> <a href="{{ uri }}">{{ file }}</a>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

View file

@ -5,12 +5,13 @@ import pytest
from fastapi import HTTPException from fastapi import HTTPException
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from aurweb import asgi, db from aurweb import asgi, config, db
from aurweb.models.account_type import USER_ID from aurweb.models.account_type import USER_ID
from aurweb.models.official_provider import OFFICIAL_BASE, OfficialProvider from aurweb.models.official_provider import OFFICIAL_BASE, OfficialProvider
from aurweb.models.package import Package from aurweb.models.package import Package
from aurweb.models.package_base import PackageBase from aurweb.models.package_base import PackageBase
from aurweb.models.package_notification import PackageNotification from aurweb.models.package_notification import PackageNotification
from aurweb.models.package_source import PackageSource
from aurweb.models.package_vote import PackageVote from aurweb.models.package_vote import PackageVote
from aurweb.models.user import User from aurweb.models.user import User
from aurweb.packages import util from aurweb.packages import util
@ -100,3 +101,36 @@ def test_query_notified(maintainer: User, package: Package):
def test_pkgreq_by_id_not_found(): def test_pkgreq_by_id_not_found():
with pytest.raises(HTTPException): with pytest.raises(HTTPException):
util.get_pkgreq_by_id(0) util.get_pkgreq_by_id(0)
def test_source_uri_file(package: Package):
FILE = "test_file"
with db.begin():
pkgsrc = db.create(PackageSource, Source=FILE,
Package=package, SourceArch="x86_64")
source_file_uri = config.get("options", "source_file_uri")
file, uri = util.source_uri(pkgsrc)
expected = source_file_uri % (pkgsrc.Source, package.PackageBase.Name)
assert (file, uri) == (FILE, expected)
def test_source_uri_named_uri(package: Package):
FILE = "test"
URL = "https://test.xyz"
with db.begin():
pkgsrc = db.create(PackageSource, Source=f"{FILE}::{URL}",
Package=package, SourceArch="x86_64")
file, uri = util.source_uri(pkgsrc)
assert (file, uri) == (FILE, URL)
def test_source_uri_unnamed_uri(package: Package):
URL = "https://test.xyz"
with db.begin():
pkgsrc = db.create(PackageSource, Source=f"{URL}",
Package=package, SourceArch="x86_64")
file, uri = util.source_uri(pkgsrc)
assert (file, uri) == (URL, URL)