perf(aurweb/pkgbase): only relevant queries when logged in

Don't query for notify, requests and vote information when the user is
not logged in as this information is not shown.
This commit is contained in:
Jelle van der Waa 2024-08-09 12:15:35 +02:00
parent 640ebd114e
commit 92f5c9b85b
No known key found for this signature in database
GPG key ID: C06086337C50773E

View file

@ -26,6 +26,8 @@ def make_context(
if not context: if not context:
context = _make_context(request, pkgbase.Name) context = _make_context(request, pkgbase.Name)
is_authenticated = request.user.is_authenticated
# Per page and offset. # Per page and offset.
offset, per_page = util.sanitize_params( offset, per_page = util.sanitize_params(
request.query_params.get("O", defaults.O), request.query_params.get("O", defaults.O),
@ -42,8 +44,11 @@ def make_context(
PackageComaintainer.Priority.asc() PackageComaintainer.Priority.asc()
).all() ).all()
] ]
if is_authenticated:
context["unflaggers"] = context["comaintainers"].copy() context["unflaggers"] = context["comaintainers"].copy()
context["unflaggers"].extend([pkgbase.Maintainer, pkgbase.Flagger]) context["unflaggers"].extend([pkgbase.Maintainer, pkgbase.Flagger])
else:
context["unflaggers"] = []
context["packages_count"] = pkgbase.packages.count() context["packages_count"] = pkgbase.packages.count()
context["keywords"] = pkgbase.keywords context["keywords"] = pkgbase.keywords
@ -60,17 +65,26 @@ def make_context(
).order_by(PackageComment.CommentTS.desc()) ).order_by(PackageComment.CommentTS.desc())
context["is_maintainer"] = bool(request.user == pkgbase.Maintainer) context["is_maintainer"] = bool(request.user == pkgbase.Maintainer)
if is_authenticated:
context["notified"] = request.user.notified(pkgbase) context["notified"] = request.user.notified(pkgbase)
else:
context["notified"] = False
context["out_of_date"] = bool(pkgbase.OutOfDateTS) context["out_of_date"] = bool(pkgbase.OutOfDateTS)
if is_authenticated:
context["voted"] = request.user.package_votes.filter( context["voted"] = request.user.package_votes.filter(
PackageVote.PackageBaseID == pkgbase.ID PackageVote.PackageBaseID == pkgbase.ID
).scalar() ).scalar()
else:
context["voted"] = False
if is_authenticated:
context["requests"] = pkgbase.requests.filter( context["requests"] = pkgbase.requests.filter(
and_(PackageRequest.Status == PENDING_ID, PackageRequest.ClosedTS.is_(None)) and_(PackageRequest.Status == PENDING_ID, PackageRequest.ClosedTS.is_(None))
).count() ).count()
else:
context["requests"] = []
context["popularity"] = popularity(pkgbase, time.utcnow()) context["popularity"] = popularity(pkgbase, time.utcnow())