Skip to content

Commit 55220c0

Browse files
committed
Use cheaper filter
1 parent bb3e46f commit 55220c0

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

pgcommitfest/commitfest/lookups.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.contrib.auth.models import User
2+
from django.db import connection
23
from django.db.models import Q
34
from django.http import Http404, HttpResponse, HttpResponseForbidden
45

@@ -20,23 +21,33 @@ def userlookup(request):
2021
| Q(last_name__icontains=query),
2122
)
2223

23-
if not request.user.is_authenticated:
24-
return HttpResponseForbidden("Login required when not filtering by commitfest")
25-
_ = cf
2624
# If no commitfest filter is provided, require login
27-
# if not cf:
28-
# if not request.user.is_authenticated:
29-
# return HttpResponseForbidden(
30-
# "Login required when not filtering by commitfest"
31-
# )
32-
# else:
33-
# # Filter users to only those who have participated in the specified commitfest
34-
# # This includes authors, reviewers, and committers of patches in that commitfest
35-
# users = users.filter(
36-
# Q(patch_author__commitfests__id=cf)
37-
# | Q(patch_reviewer__commitfests__id=cf)
38-
# | Q(committer__patch__commitfests__id=cf)
39-
# ).distinct()
25+
if not cf:
26+
if not request.user.is_authenticated:
27+
return HttpResponseForbidden(
28+
"Login required when not filtering by commitfest"
29+
)
30+
else:
31+
# Filter users to only those who have participated in the specified commitfest.
32+
with connection.cursor() as cursor:
33+
cursor.execute(
34+
"""
35+
SELECT cpa.user_id FROM commitfest_patch_authors cpa
36+
INNER JOIN commitfest_patchoncommitfest poc ON poc.patch_id = cpa.patch_id
37+
WHERE poc.commitfest_id = %(cf)s
38+
UNION
39+
SELECT cpr.user_id FROM commitfest_patch_reviewers cpr
40+
INNER JOIN commitfest_patchoncommitfest poc ON poc.patch_id = cpr.patch_id
41+
WHERE poc.commitfest_id = %(cf)s
42+
UNION
43+
SELECT p.committer_id FROM commitfest_patch p
44+
INNER JOIN commitfest_patchoncommitfest poc ON poc.patch_id = p.id
45+
WHERE poc.commitfest_id = %(cf)s AND p.committer_id IS NOT NULL
46+
""",
47+
{"cf": cf},
48+
)
49+
user_ids = [row[0] for row in cursor.fetchall()]
50+
users = users.filter(id__in=user_ids)
4051

4152
return HttpResponse(
4253
json.dumps(

0 commit comments

Comments
 (0)