Skip to content

Commit

Permalink
Merge pull request #155 from taigaio/performance-patch
Browse files Browse the repository at this point in the history
Tweak membership queries
  • Loading branch information
migonzalvar authored Apr 19, 2024
2 parents 5d7d323 + 5990da2 commit 8851435
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
40 changes: 20 additions & 20 deletions taiga/projects/services/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ def check_if_project_privacy_can_be_changed(project):
user_id__isnull=False) |
Q(project_id=project.pk, # current project members
user_id__isnull=False))
.order_by("user_id")
.distinct("user_id")
.values("user_id")
.distinct()
.count()) # Just confirmed members

current_memberships += (Membership.objects.filter(Q(project__is_private=not project.is_private, # public/private members
project__owner_id=project.owner_id,
user_id__isnull=True) |
Q(project_id=project.pk, # current project members
user_id__isnull=True))
.order_by("email")
.distinct("email")
.values("email")
.distinct()
.count()) # Just pending members

if project.is_private:
Expand Down Expand Up @@ -90,14 +90,14 @@ def check_if_project_can_be_created_or_updated(project):
current_memberships = (Membership.objects.filter(project__is_private=project.is_private,
project__owner_id=project.owner_id,
user_id__isnull=False)
.order_by("user_id")
.distinct("user_id")
.values("user_id")
.distinct()
.count()) # Just confirmed members
current_memberships += (Membership.objects.filter(project__is_private=project.is_private,
project__owner_id=project.owner_id,
user_id__isnull=True)
.order_by("email")
.distinct("email")
.values("email")
.distinct()
.count()) # Pending members

if project.is_private:
Expand Down Expand Up @@ -138,16 +138,16 @@ def check_if_project_can_be_transfered(project, new_owner):
user_id__isnull=False) |
Q(project_id=project.pk, # current project members
user_id__isnull=False))
.order_by("user_id")
.distinct("user_id")
.values("user_id")
.distinct()
.count()) # Just confirmed members
current_memberships += (Membership.objects.filter(Q(project__is_private=project.is_private, # public/private members
project__owner_id=new_owner.id,
user_id__isnull=True) |
Q(project_id=project.pk, # current project members
user_id__isnull=True))
.order_by("email")
.distinct("email")
.values("email")
.distinct()
.count()) # Pending members

if project.is_private:
Expand Down Expand Up @@ -185,14 +185,14 @@ def check_if_project_can_be_duplicate(project, new_owner, new_is_private, new_us
actual_user_id_members = (Membership.objects.filter(project__is_private=new_is_private,
project__owner_id=new_owner.id,
user_id__isnull=False)
.order_by("user_id")
.distinct("user_id")
.values("user_id")
.distinct()
.values_list("user__id", flat=True))
total_pending_members = (Membership.objects.filter(project__is_private=new_is_private,
project__owner_id=new_owner.id,
user_id__isnull=True)
.order_by("email")
.distinct("email")
.values("email")
.distinct()
.count())

current_memberships = len(
Expand Down Expand Up @@ -237,14 +237,14 @@ def check_if_project_is_out_of_owner_limits(project):
current_memberships = (Membership.objects.filter(project__is_private=project.is_private,
project__owner_id=project.owner_id,
user_id__isnull=False)
.order_by("user_id")
.distinct("user_id")
.values("user_id")
.distinct()
.count()) # Current confirmed members
current_memberships += (Membership.objects.filter(project__is_private=project.is_private,
project__owner_id=project.owner_id,
user_id__isnull=True)
.order_by("email")
.distinct("email")
.values("email")
.distinct()
.count()) # Pending members

if project.is_private:
Expand Down
16 changes: 8 additions & 8 deletions taiga/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ def total_memberships_private_projects(self, obj):
accepted = (Membership.objects.filter(project__is_private=True,
project__owner_id=obj.id,
user_id__isnull=False)
.order_by("user_id")
.distinct("user_id").count())
.values("user_id")
.distinct().count())
pending = (Membership.objects.filter(project__is_private=True,
project__owner_id=obj.id,
user_id__isnull=True)
.order_by("email")
.distinct("email").count())
.values("email")
.distinct().count())
total = pending + accepted
return mark_safe(f"{total}{SEPARATOR}<i>{accepted} accepted</i>{SEPARATOR}<i>{pending} pending</i>")

Expand All @@ -203,13 +203,13 @@ def total_memberships_public_projects(self, obj):
accepted = (Membership.objects.filter(project__is_private=False,
project__owner_id=obj.id,
user_id__isnull=False)
.order_by("user_id")
.distinct("user_id").count())
.values("user_id")
.distinct().count())
pending = (Membership.objects.filter(project__is_private=False,
project__owner_id=obj.id,
user_id__isnull=True)
.order_by("email")
.distinct("email").count())
.values("email")
.distinct().count())
total = pending + accepted
return mark_safe(f"{total}{SEPARATOR}<i>{accepted} accepted</i>{SEPARATOR}<i>{pending} pending</i>")

Expand Down

0 comments on commit 8851435

Please sign in to comment.