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 @@