From 8f8929f324657665545728df3305878a9f43a8db Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Thu, 30 Dec 2021 23:04:39 -0800 Subject: [PATCH] fix(routers.packages): handle package source display Signed-off-by: Kevin Morris --- aurweb/packages/util.py | 31 ++++++++++++++-- .../partials/packages/package_metadata.html | 3 +- test/test_packages_util.py | 36 ++++++++++++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/aurweb/packages/util.py b/aurweb/packages/util.py index 53af4446..987061c9 100644 --- a/aurweb/packages/util.py +++ b/aurweb/packages/util.py @@ -1,13 +1,13 @@ from collections import defaultdict from http import HTTPStatus -from typing import Dict, List, Union +from typing import Dict, List, Tuple, Union import orjson from fastapi import HTTPException, Request 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.official_provider import OFFICIAL_BASE, OfficialProvider 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) ).order_by(Package.Name.asc()).limit(limit) 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)) diff --git a/templates/partials/packages/package_metadata.html b/templates/partials/packages/package_metadata.html index 5f831711..6f58c2be 100644 --- a/templates/partials/packages/package_metadata.html +++ b/templates/partials/packages/package_metadata.html @@ -65,8 +65,9 @@
diff --git a/test/test_packages_util.py b/test/test_packages_util.py index 0e5e896b..3474f847 100644 --- a/test/test_packages_util.py +++ b/test/test_packages_util.py @@ -5,12 +5,13 @@ import pytest from fastapi import HTTPException 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.official_provider import OFFICIAL_BASE, OfficialProvider from aurweb.models.package import Package from aurweb.models.package_base import PackageBase from aurweb.models.package_notification import PackageNotification +from aurweb.models.package_source import PackageSource from aurweb.models.package_vote import PackageVote from aurweb.models.user import User from aurweb.packages import util @@ -100,3 +101,36 @@ def test_query_notified(maintainer: User, package: Package): def test_pkgreq_by_id_not_found(): with pytest.raises(HTTPException): 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)