directly work on group ids

This commit is contained in:
imperosol
2026-03-05 13:57:27 +01:00
parent 09a98db786
commit 30663d87a4
3 changed files with 12 additions and 15 deletions

View File

@@ -307,6 +307,7 @@ class PermissionOrClubBoardRequiredMixin(PermissionRequiredMixin):
return False return False
if super().has_permission(): if super().has_permission():
return True return True
return self.club is not None and any( return (
g.id == self.club.board_group_id for g in self.request.user.all_groups self.club is not None
and self.club.board_group_id in self.request.user.all_groups
) )

View File

@@ -356,10 +356,10 @@ class User(AbstractUser):
) )
if group_id is None: if group_id is None:
return False return False
return any(g.id == group_id for g in self.all_groups) return group_id in self.all_groups
@cached_property @cached_property
def all_groups(self) -> list[Group]: def all_groups(self) -> dict[int, Group]:
"""Get the list of groups this user is in.""" """Get the list of groups this user is in."""
additional_groups = [] additional_groups = []
if self.is_subscribed: if self.is_subscribed:
@@ -372,14 +372,11 @@ class User(AbstractUser):
# a UNION rather than a OR (in average, 0.25ms vs 14ms). # a UNION rather than a OR (in average, 0.25ms vs 14ms).
# For the why, cf. https://dba.stackexchange.com/questions/293836/why-is-an-or-statement-slower-than-union # For the why, cf. https://dba.stackexchange.com/questions/293836/why-is-an-or-statement-slower-than-union
qs = qs.union(Group.objects.filter(id__in=additional_groups)) qs = qs.union(Group.objects.filter(id__in=additional_groups))
return list(qs) return {g.id: g for g in qs}
@cached_property @cached_property
def is_root(self) -> bool: def is_root(self) -> bool:
if self.is_superuser: return self.is_superuser or settings.SITH_GROUP_ROOT_ID in self.all_groups
return True
root_id = settings.SITH_GROUP_ROOT_ID
return any(g.id == root_id for g in self.all_groups)
@cached_property @cached_property
def is_board_member(self) -> bool: def is_board_member(self) -> bool:
@@ -1106,8 +1103,7 @@ class PageQuerySet(models.QuerySet):
return self.filter(view_groups=settings.SITH_GROUP_PUBLIC_ID) return self.filter(view_groups=settings.SITH_GROUP_PUBLIC_ID)
if user.has_perm("core.view_page"): if user.has_perm("core.view_page"):
return self.all() return self.all()
groups_ids = [g.id for g in user.all_groups] return self.filter(view_groups__in=user.all_groups)
return self.filter(view_groups__in=groups_ids)
# This function prevents generating migration upon settings change # This function prevents generating migration upon settings change
@@ -1381,7 +1377,7 @@ class PageRev(models.Model):
return self.page.can_be_edited_by(user) return self.page.can_be_edited_by(user)
def is_owned_by(self, user: User) -> bool: def is_owned_by(self, user: User) -> bool:
return any(g.id == self.page.owner_group_id for g in user.all_groups) return self.page.owner_group_id in user.all_groups
def similarity_ratio(self, text: str) -> float: def similarity_ratio(self, text: str) -> float:
"""Similarity ratio between this revision's content and the given text. """Similarity ratio between this revision's content and the given text.

View File

@@ -115,7 +115,7 @@ class VoteFormView(LoginRequiredMixin, UserPassesTestMixin, FormView):
if not self.election.can_vote(self.request.user): if not self.election.can_vote(self.request.user):
return False return False
return self.election.vote_groups.filter( return self.election.vote_groups.filter(
id__in=[g.id for g in self.request.user.all_groups] id__in=self.request.user.all_groups
).exists() ).exists()
def vote(self, election_data): def vote(self, election_data):
@@ -231,7 +231,7 @@ class RoleCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
if self.request.user.has_perm("election.add_role"): if self.request.user.has_perm("election.add_role"):
return True return True
return self.election.edit_groups.filter( return self.election.edit_groups.filter(
id__in=[g.id for g in self.request.user.all_groups] id__in=self.request.user.all_groups
).exists() ).exists()
def get_initial(self): def get_initial(self):
@@ -265,7 +265,7 @@ class ElectionListCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView
.union(self.election.edit_groups.values("id")) .union(self.election.edit_groups.values("id"))
.values_list("id", flat=True) .values_list("id", flat=True)
) )
return not groups.isdisjoint({g.id for g in self.request.user.all_groups}) return not groups.isdisjoint(self.request.user.all_groups.keys())
def get_initial(self): def get_initial(self):
return {"election": self.election} return {"election": self.election}