Skip to content

Commit 9cc718b

Browse files
authored
Use count() instead of len() where possible. (#4760)
Improves performance a bit by using `.count()` directly instead of loading everything and then use `len()` on it. SQL is always faster than PHP.
1 parent 945afb7 commit 9cc718b

5 files changed

Lines changed: 7 additions & 7 deletions

File tree

hypha/apply/dashboard/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def get_paf_for_review(user, is_paf_approval_sequential):
99
paf_approvals = PAFApprovals.objects.annotate(
1010
roles_count=Count("paf_reviewer_role__user_roles")
1111
).filter(
12-
roles_count=len(list(user.groups.all())),
12+
roles_count=user.groups.count(),
1313
approved=False,
1414
)
1515

hypha/apply/projects/templatetags/project_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def user_next_step_on_project(project, user, request=None):
171171
else:
172172
matched_roles = PAFReviewersRole.objects.annotate(
173173
roles_count=Count("user_roles")
174-
).filter(roles_count=len(user.groups.all()))
174+
).filter(roles_count=user.groups.count())
175175
for group in user.groups.all():
176176
matched_roles = matched_roles.filter(user_roles__id=group.id)
177177
if not matched_roles:

hypha/apply/projects/views/payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def form_valid(self, form):
319319
related=self.object,
320320
)
321321

322-
if len(self.project.invoices.all()) == 1:
322+
if self.project.invoices.count() == 1:
323323
# remove Project waiting invoices task for applicant on first invoice
324324
remove_tasks_for_user(
325325
code=PROJECT_WAITING_INVOICE,

hypha/apply/todo/services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def validate_user_uniqueness(code, user, related_obj):
2323
# if same task is already assigned to user's user_group
2424
user_group_matching_tasks = matching_tasks.annotate(
2525
group_count=Count("user_group")
26-
).filter(group_count=len(user.groups.all()))
26+
).filter(group_count=user.groups.count())
2727
for group in user.groups.all():
2828
user_group_matching_tasks = user_group_matching_tasks.filter(
2929
user_group__id=group.id
@@ -45,7 +45,7 @@ def validate_user_groups_uniqueness(code, user_groups, related_obj):
4545
)
4646
user_group_matching_tasks = matching_tasks.annotate(
4747
group_count=Count("user_group")
48-
).filter(group_count=len(user_groups))
48+
).filter(group_count=user_groups.count())
4949
for group in user_groups:
5050
user_group_matching_tasks = user_group_matching_tasks.filter(
5151
user_group__id=group.id

hypha/apply/todo/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def remove_tasks_for_user_group(code, user_group, related_obj):
147147
)
148148
user_group_matching_tasks = matching_tasks.annotate(
149149
group_count=Count("user_group")
150-
).filter(group_count=len(user_group.all()))
150+
).filter(group_count=user_group.count())
151151
for group in user_group.all():
152152
user_group_matching_tasks = user_group_matching_tasks.filter(
153153
user_group__id=group.id
@@ -190,7 +190,7 @@ def get_tasks_for_user(user):
190190
group_count=Count("user_group")
191191
)
192192
user_group_tasks = Task.objects.annotate(group_count=Count("user_group")).filter(
193-
group_count=len(user.groups.all())
193+
group_count=user.groups.count()
194194
)
195195
for group in user.groups.all():
196196
user_group_tasks = user_group_tasks.filter(user_group__id=group.id)

0 commit comments

Comments
 (0)