mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
routers.packages: Simplify some existence checks
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
bace345da4
commit
4ade8b0539
6 changed files with 48 additions and 43 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 += "<em>("
|
||||
|
||||
string += ", ".join([
|
||||
string = ", ".join([
|
||||
f'<a href="{package_link(pkg)}">{pkg.Name}</a>'
|
||||
for pkg in providers
|
||||
])
|
||||
|
||||
if has_providers:
|
||||
string += ")</em>"
|
||||
if string:
|
||||
# If we actually constructed a string, wrap it.
|
||||
string = f"<em>({string})</em>"
|
||||
|
||||
return string
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if comments.count() %}
|
||||
{% if comments.scalar() %}
|
||||
<div class="comments package-comments">
|
||||
<div class="comments-header">
|
||||
<h3>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{% set pkg = pkgbase.packages.first() %}
|
||||
<table id="pkginfo">
|
||||
<tr>
|
||||
<th>{{ "Git Clone URL" | tr }}:</th>
|
||||
|
@ -19,12 +20,11 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<th>{{ "Description" | tr }}:</th>
|
||||
<td class="wrap">{{ pkgbase.packages.first().Description }}</td>
|
||||
<td class="wrap">{{ pkg.Description }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ "Upstream URL" | tr }}:</th>
|
||||
<td class="wrap">
|
||||
{% set pkg = pkgbase.packages.first() %}
|
||||
{% if pkg.URL %}
|
||||
<a href="{{ pkg.URL }}">{{ pkg.URL }}</a>
|
||||
{% else %}
|
||||
|
@ -33,7 +33,7 @@
|
|||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if pkgbase.keywords.count() %}
|
||||
{% if pkgbase.keywords.scalar() %}
|
||||
<tr>
|
||||
<th>{{ "Keywords" | tr }}:</th>
|
||||
{% if is_maintainer %}
|
||||
|
@ -63,13 +63,13 @@
|
|||
{% endif %}
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if licenses and licenses.count() and show_package_details | default(False) %}
|
||||
{% if licenses and licenses.scalar() and show_package_details %}
|
||||
<tr>
|
||||
<th>{{ "Licenses" | tr }}:</th>
|
||||
<td>{{ licenses | join(', ', attribute='Name') | default('None' | tr) }} </td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if show_package_details | default(False) %}
|
||||
{% if show_package_details %}
|
||||
<tr>
|
||||
<th>{{ "Conflicts" | tr }}:</th>
|
||||
<td class="wrap">
|
||||
|
|
Loading…
Add table
Reference in a new issue