From 4ade8b053992663242df361477ca26282cadfc20 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 6 Aug 2021 00:59:38 -0700 Subject: [PATCH] routers.packages: Simplify some existence checks Signed-off-by: Kevin Morris --- aurweb/auth.py | 15 ++++++++ aurweb/models/package_dependency.py | 2 +- aurweb/packages/util.py | 18 ++++------ aurweb/routers/packages.py | 44 +++++++++++------------ templates/partials/packages/comments.html | 2 +- templates/partials/packages/details.html | 10 +++--- 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/aurweb/auth.py b/aurweb/auth.py index 316e7293..26e4073d 100644 --- a/aurweb/auth.py +++ b/aurweb/auth.py @@ -19,6 +19,17 @@ from aurweb.models.user import User from aurweb.templates import make_variable_context, render_template +class StubQuery: + """ Acts as a stubbed version of an orm.Query. Typically used + to masquerade fake records for an AnonymousUser. """ + + def filter(self, *args): + return StubQuery() + + def scalar(self): + return 0 + + class AnonymousUser: # Stub attributes used to mimic a real user. ID = 0 @@ -28,6 +39,10 @@ class AnonymousUser: # A stub ssh_pub_key relationship. ssh_pub_key = None + # Add stubbed relationship backrefs. + package_notifications = StubQuery() + package_votes = StubQuery() + # A nonce attribute, needed for all browser sessions; set in __init__. nonce = None diff --git a/aurweb/models/package_dependency.py b/aurweb/models/package_dependency.py index 0e5b028b..9ce0b019 100644 --- a/aurweb/models/package_dependency.py +++ b/aurweb/models/package_dependency.py @@ -69,4 +69,4 @@ class PackageDependency(Base): pkg = db.query(Package, Package.Name == self.DepName) official = db.query(OfficialProvider, OfficialProvider.Name == self.DepName) - return pkg.count() > 0 or official.count() > 0 + return pkg.scalar() or official.scalar() diff --git a/aurweb/packages/util.py b/aurweb/packages/util.py index 698ae1af..60db2962 100644 --- a/aurweb/packages/util.py +++ b/aurweb/packages/util.py @@ -54,10 +54,9 @@ def dep_extra_desc(dep: PackageDependency) -> str: @register_filter("pkgname_link") def pkgname_link(pkgname: str) -> str: base = "/".join([OFFICIAL_BASE, "packages"]) - pkg = db.query(Package).filter(Package.Name == pkgname) official = db.query(OfficialProvider).filter( OfficialProvider.Name == pkgname) - if not pkg.count() or official.count(): + if official.scalar(): return f"{base}/?q={pkgname}" return f"/packages/{pkgname}" @@ -67,7 +66,7 @@ def package_link(package: Package) -> str: base = "/".join([OFFICIAL_BASE, "packages"]) official = db.query(OfficialProvider).filter( OfficialProvider.Name == package.Name) - if official.count(): + if official.scalar(): return f"{base}/?q={package.Name}" return f"/packages/{package.Name}" @@ -82,19 +81,14 @@ def provides_list(package: Package, depname: str) -> list: ) ) - string = str() - has_providers = providers.count() > 0 - - if has_providers: - string += "(" - - string += ", ".join([ + string = ", ".join([ f'{pkg.Name}' for pkg in providers ]) - if has_providers: - string += ")" + if string: + # If we actually constructed a string, wrap it. + string = f"({string})" return string diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index cb7f4a18..0ffcbfb9 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -19,9 +19,9 @@ from aurweb.models.package_notification import PackageNotification from aurweb.models.package_relation import PackageRelation from aurweb.models.package_source import PackageSource from aurweb.models.package_vote import PackageVote -from aurweb.models.relation_type import CONFLICTS_ID, RelationType +from aurweb.models.relation_type import CONFLICTS_ID from aurweb.packages.util import get_pkgbase -from aurweb.templates import make_variable_context, render_template +from aurweb.templates import make_context, render_template router = APIRouter() @@ -34,7 +34,7 @@ async def make_single_context(request: Request, :param pkgbase: PackageBase instance :return: A pkgbase context without specific differences """ - context = await make_variable_context(request, pkgbase.Name) + context = make_context(request, pkgbase.Name) context["git_clone_uri_anon"] = aurweb.config.get("options", "git_clone_uri_anon") context["git_clone_uri_priv"] = aurweb.config.get("options", @@ -44,20 +44,15 @@ async def make_single_context(request: Request, context["keywords"] = pkgbase.keywords context["comments"] = pkgbase.comments context["is_maintainer"] = (request.user.is_authenticated() - and request.user == pkgbase.Maintainer) - context["notified"] = db.query( - PackageNotification).join(PackageBase).filter( - and_(PackageBase.ID == pkgbase.ID, - PackageNotification.UserID == request.user.ID)).count() > 0 + and request.user.ID == pkgbase.MaintainerUID) + context["notified"] = request.user.package_notifications.filter( + PackageNotification.PackageBaseID == pkgbase.ID + ).scalar() context["out_of_date"] = bool(pkgbase.OutOfDateTS) - context["voted"] = pkgbase.package_votes.filter( - PackageVote.UsersID == request.user.ID).count() > 0 - - context["notifications_enabled"] = db.query( - PackageNotification).join(PackageBase).filter( - PackageBase.ID == pkgbase.ID).count() > 0 + context["voted"] = request.user.package_votes.filter( + PackageVote.PackageBaseID == pkgbase.ID).scalar() return context @@ -71,13 +66,12 @@ async def package(request: Request, name: str) -> Response: context = await make_single_context(request, pkgbase) # Package sources. - sources = db.query(PackageSource).join(Package).filter( - Package.PackageBaseID == pkgbase.ID) - context["sources"] = sources + context["sources"] = db.query(PackageSource).join(Package).join( + PackageBase).filter(PackageBase.ID == pkgbase.ID) # Package dependencies. - dependencies = db.query(PackageDependency).join(Package).filter( - Package.PackageBaseID == pkgbase.ID) + dependencies = db.query(PackageDependency).join(Package).join( + PackageBase).filter(PackageBase.ID == pkgbase.ID) context["dependencies"] = dependencies # Package requirements (other packages depend on this one). @@ -86,13 +80,15 @@ async def package(request: Request, name: str) -> Response: Package.Name.asc()) context["required_by"] = required_by - licenses = db.query(License).join(PackageLicense).join(Package).filter( - PackageLicense.PackageID == pkgbase.packages.first().ID) + licenses = db.query(License).join(PackageLicense).join(Package).join( + PackageBase).filter(PackageBase.ID == pkgbase.ID) context["licenses"] = licenses - conflicts = db.query(PackageRelation).join(RelationType).join(Package).join(PackageBase).filter( - and_(RelationType.ID == CONFLICTS_ID, - PackageBase.ID == pkgbase.ID)) + conflicts = db.query(PackageRelation).join(Package).join( + PackageBase).filter( + and_(PackageRelation.RelTypeID == CONFLICTS_ID, + PackageBase.ID == pkgbase.ID) + ) context["conflicts"] = conflicts return render_template(request, "packages/show.html", context) diff --git a/templates/partials/packages/comments.html b/templates/partials/packages/comments.html index f1bc020d..051849b0 100644 --- a/templates/partials/packages/comments.html +++ b/templates/partials/packages/comments.html @@ -49,7 +49,7 @@ {% endif %} -{% if comments.count() %} +{% if comments.scalar() %}

diff --git a/templates/partials/packages/details.html b/templates/partials/packages/details.html index a25e9c9e..83c6d53b 100644 --- a/templates/partials/packages/details.html +++ b/templates/partials/packages/details.html @@ -1,3 +1,4 @@ +{% set pkg = pkgbase.packages.first() %} @@ -19,12 +20,11 @@ - + {% endif %} - {% if pkgbase.keywords.count() %} + {% if pkgbase.keywords.scalar() %} {% if is_maintainer %} @@ -63,13 +63,13 @@ {% endif %} {% endif %} - {% if licenses and licenses.count() and show_package_details | default(False) %} + {% if licenses and licenses.scalar() and show_package_details %} {% endif %} - {% if show_package_details | default(False) %} + {% if show_package_details %}
{{ "Git Clone URL" | tr }}:
{{ "Description" | tr }}:{{ pkgbase.packages.first().Description }}{{ pkg.Description }}
{{ "Upstream URL" | tr }}: - {% set pkg = pkgbase.packages.first() %} {% if pkg.URL %} {{ pkg.URL }} {% else %} @@ -33,7 +33,7 @@
{{ "Keywords" | tr }}:
{{ "Licenses" | tr }}: {{ licenses | join(', ', attribute='Name') | default('None' | tr) }}
{{ "Conflicts" | tr }}: