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
|
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:
|
class AnonymousUser:
|
||||||
# Stub attributes used to mimic a real user.
|
# Stub attributes used to mimic a real user.
|
||||||
ID = 0
|
ID = 0
|
||||||
|
@ -28,6 +39,10 @@ class AnonymousUser:
|
||||||
# A stub ssh_pub_key relationship.
|
# A stub ssh_pub_key relationship.
|
||||||
ssh_pub_key = None
|
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__.
|
# A nonce attribute, needed for all browser sessions; set in __init__.
|
||||||
nonce = None
|
nonce = None
|
||||||
|
|
||||||
|
|
|
@ -69,4 +69,4 @@ class PackageDependency(Base):
|
||||||
pkg = db.query(Package, Package.Name == self.DepName)
|
pkg = db.query(Package, Package.Name == self.DepName)
|
||||||
official = db.query(OfficialProvider,
|
official = db.query(OfficialProvider,
|
||||||
OfficialProvider.Name == self.DepName)
|
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")
|
@register_filter("pkgname_link")
|
||||||
def pkgname_link(pkgname: str) -> str:
|
def pkgname_link(pkgname: str) -> str:
|
||||||
base = "/".join([OFFICIAL_BASE, "packages"])
|
base = "/".join([OFFICIAL_BASE, "packages"])
|
||||||
pkg = db.query(Package).filter(Package.Name == pkgname)
|
|
||||||
official = db.query(OfficialProvider).filter(
|
official = db.query(OfficialProvider).filter(
|
||||||
OfficialProvider.Name == pkgname)
|
OfficialProvider.Name == pkgname)
|
||||||
if not pkg.count() or official.count():
|
if official.scalar():
|
||||||
return f"{base}/?q={pkgname}"
|
return f"{base}/?q={pkgname}"
|
||||||
return f"/packages/{pkgname}"
|
return f"/packages/{pkgname}"
|
||||||
|
|
||||||
|
@ -67,7 +66,7 @@ def package_link(package: Package) -> str:
|
||||||
base = "/".join([OFFICIAL_BASE, "packages"])
|
base = "/".join([OFFICIAL_BASE, "packages"])
|
||||||
official = db.query(OfficialProvider).filter(
|
official = db.query(OfficialProvider).filter(
|
||||||
OfficialProvider.Name == package.Name)
|
OfficialProvider.Name == package.Name)
|
||||||
if official.count():
|
if official.scalar():
|
||||||
return f"{base}/?q={package.Name}"
|
return f"{base}/?q={package.Name}"
|
||||||
return f"/packages/{package.Name}"
|
return f"/packages/{package.Name}"
|
||||||
|
|
||||||
|
@ -82,19 +81,14 @@ def provides_list(package: Package, depname: str) -> list:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
string = str()
|
string = ", ".join([
|
||||||
has_providers = providers.count() > 0
|
|
||||||
|
|
||||||
if has_providers:
|
|
||||||
string += "<em>("
|
|
||||||
|
|
||||||
string += ", ".join([
|
|
||||||
f'<a href="{package_link(pkg)}">{pkg.Name}</a>'
|
f'<a href="{package_link(pkg)}">{pkg.Name}</a>'
|
||||||
for pkg in providers
|
for pkg in providers
|
||||||
])
|
])
|
||||||
|
|
||||||
if has_providers:
|
if string:
|
||||||
string += ")</em>"
|
# If we actually constructed a string, wrap it.
|
||||||
|
string = f"<em>({string})</em>"
|
||||||
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,9 @@ from aurweb.models.package_notification import PackageNotification
|
||||||
from aurweb.models.package_relation import PackageRelation
|
from aurweb.models.package_relation import PackageRelation
|
||||||
from aurweb.models.package_source import PackageSource
|
from aurweb.models.package_source import PackageSource
|
||||||
from aurweb.models.package_vote import PackageVote
|
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.packages.util import get_pkgbase
|
||||||
from aurweb.templates import make_variable_context, render_template
|
from aurweb.templates import make_context, render_template
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ async def make_single_context(request: Request,
|
||||||
:param pkgbase: PackageBase instance
|
:param pkgbase: PackageBase instance
|
||||||
:return: A pkgbase context without specific differences
|
: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",
|
context["git_clone_uri_anon"] = aurweb.config.get("options",
|
||||||
"git_clone_uri_anon")
|
"git_clone_uri_anon")
|
||||||
context["git_clone_uri_priv"] = aurweb.config.get("options",
|
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["keywords"] = pkgbase.keywords
|
||||||
context["comments"] = pkgbase.comments
|
context["comments"] = pkgbase.comments
|
||||||
context["is_maintainer"] = (request.user.is_authenticated()
|
context["is_maintainer"] = (request.user.is_authenticated()
|
||||||
and request.user == pkgbase.Maintainer)
|
and request.user.ID == pkgbase.MaintainerUID)
|
||||||
context["notified"] = db.query(
|
context["notified"] = request.user.package_notifications.filter(
|
||||||
PackageNotification).join(PackageBase).filter(
|
PackageNotification.PackageBaseID == pkgbase.ID
|
||||||
and_(PackageBase.ID == pkgbase.ID,
|
).scalar()
|
||||||
PackageNotification.UserID == request.user.ID)).count() > 0
|
|
||||||
|
|
||||||
context["out_of_date"] = bool(pkgbase.OutOfDateTS)
|
context["out_of_date"] = bool(pkgbase.OutOfDateTS)
|
||||||
|
|
||||||
context["voted"] = pkgbase.package_votes.filter(
|
context["voted"] = request.user.package_votes.filter(
|
||||||
PackageVote.UsersID == request.user.ID).count() > 0
|
PackageVote.PackageBaseID == pkgbase.ID).scalar()
|
||||||
|
|
||||||
context["notifications_enabled"] = db.query(
|
|
||||||
PackageNotification).join(PackageBase).filter(
|
|
||||||
PackageBase.ID == pkgbase.ID).count() > 0
|
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
@ -71,13 +66,12 @@ async def package(request: Request, name: str) -> Response:
|
||||||
context = await make_single_context(request, pkgbase)
|
context = await make_single_context(request, pkgbase)
|
||||||
|
|
||||||
# Package sources.
|
# Package sources.
|
||||||
sources = db.query(PackageSource).join(Package).filter(
|
context["sources"] = db.query(PackageSource).join(Package).join(
|
||||||
Package.PackageBaseID == pkgbase.ID)
|
PackageBase).filter(PackageBase.ID == pkgbase.ID)
|
||||||
context["sources"] = sources
|
|
||||||
|
|
||||||
# Package dependencies.
|
# Package dependencies.
|
||||||
dependencies = db.query(PackageDependency).join(Package).filter(
|
dependencies = db.query(PackageDependency).join(Package).join(
|
||||||
Package.PackageBaseID == pkgbase.ID)
|
PackageBase).filter(PackageBase.ID == pkgbase.ID)
|
||||||
context["dependencies"] = dependencies
|
context["dependencies"] = dependencies
|
||||||
|
|
||||||
# Package requirements (other packages depend on this one).
|
# Package requirements (other packages depend on this one).
|
||||||
|
@ -86,13 +80,15 @@ async def package(request: Request, name: str) -> Response:
|
||||||
Package.Name.asc())
|
Package.Name.asc())
|
||||||
context["required_by"] = required_by
|
context["required_by"] = required_by
|
||||||
|
|
||||||
licenses = db.query(License).join(PackageLicense).join(Package).filter(
|
licenses = db.query(License).join(PackageLicense).join(Package).join(
|
||||||
PackageLicense.PackageID == pkgbase.packages.first().ID)
|
PackageBase).filter(PackageBase.ID == pkgbase.ID)
|
||||||
context["licenses"] = licenses
|
context["licenses"] = licenses
|
||||||
|
|
||||||
conflicts = db.query(PackageRelation).join(RelationType).join(Package).join(PackageBase).filter(
|
conflicts = db.query(PackageRelation).join(Package).join(
|
||||||
and_(RelationType.ID == CONFLICTS_ID,
|
PackageBase).filter(
|
||||||
PackageBase.ID == pkgbase.ID))
|
and_(PackageRelation.RelTypeID == CONFLICTS_ID,
|
||||||
|
PackageBase.ID == pkgbase.ID)
|
||||||
|
)
|
||||||
context["conflicts"] = conflicts
|
context["conflicts"] = conflicts
|
||||||
|
|
||||||
return render_template(request, "packages/show.html", context)
|
return render_template(request, "packages/show.html", context)
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if comments.count() %}
|
{% if comments.scalar() %}
|
||||||
<div class="comments package-comments">
|
<div class="comments package-comments">
|
||||||
<div class="comments-header">
|
<div class="comments-header">
|
||||||
<h3>
|
<h3>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
{% set pkg = pkgbase.packages.first() %}
|
||||||
<table id="pkginfo">
|
<table id="pkginfo">
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Git Clone URL" | tr }}:</th>
|
<th>{{ "Git Clone URL" | tr }}:</th>
|
||||||
|
@ -19,12 +20,11 @@
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Description" | tr }}:</th>
|
<th>{{ "Description" | tr }}:</th>
|
||||||
<td class="wrap">{{ pkgbase.packages.first().Description }}</td>
|
<td class="wrap">{{ pkg.Description }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Upstream URL" | tr }}:</th>
|
<th>{{ "Upstream URL" | tr }}:</th>
|
||||||
<td class="wrap">
|
<td class="wrap">
|
||||||
{% set pkg = pkgbase.packages.first() %}
|
|
||||||
{% if pkg.URL %}
|
{% if pkg.URL %}
|
||||||
<a href="{{ pkg.URL }}">{{ pkg.URL }}</a>
|
<a href="{{ pkg.URL }}">{{ pkg.URL }}</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -33,7 +33,7 @@
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if pkgbase.keywords.count() %}
|
{% if pkgbase.keywords.scalar() %}
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Keywords" | tr }}:</th>
|
<th>{{ "Keywords" | tr }}:</th>
|
||||||
{% if is_maintainer %}
|
{% if is_maintainer %}
|
||||||
|
@ -63,13 +63,13 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if licenses and licenses.count() and show_package_details | default(False) %}
|
{% if licenses and licenses.scalar() and show_package_details %}
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Licenses" | tr }}:</th>
|
<th>{{ "Licenses" | tr }}:</th>
|
||||||
<td>{{ licenses | join(', ', attribute='Name') | default('None' | tr) }} </td>
|
<td>{{ licenses | join(', ', attribute='Name') | default('None' | tr) }} </td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if show_package_details | default(False) %}
|
{% if show_package_details %}
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ "Conflicts" | tr }}:</th>
|
<th>{{ "Conflicts" | tr }}:</th>
|
||||||
<td class="wrap">
|
<td class="wrap">
|
||||||
|
|
Loading…
Add table
Reference in a new issue