Skip to content

Commit

Permalink
Remove 5 pointless queries when viewing users
Browse files Browse the repository at this point in the history
We presently do one SQL query per every activity chair category in the
database (there are 6 activities with chairs at present). We can
drastically simplify this by just looking up a users' groups *first*,
then reporting any which have them specified as an activity chair.
  • Loading branch information
DavidCain committed Sep 4, 2023
1 parent 90885d4 commit 1f84ab1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ws/utils/perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ def chair_activities(
allow_superusers: bool = False,
) -> list[enums.Activity]:
"""All activities for which the user is the chair."""
return [
activity_enum
for activity_enum in enums.Activity
if is_chair(user, activity_enum, allow_superusers)
]
chair_group_to_activity: dict[str, enums.Activity] = {
chair_group(activity_enum): activity_enum for activity_enum in enums.Activity
}
groups = Group.objects if allow_superusers and user.is_superuser else user.groups

return sorted(
(
chair_group_to_activity[g.name]
for g in groups.filter(name__in=chair_group_to_activity).order_by('name')
),
key=lambda activity_enum: activity_enum.value,
)

0 comments on commit 1f84ab1

Please sign in to comment.