Skip to content

Commit

Permalink
fix: Prefetch Landscape membership list data
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Nov 1, 2023
1 parent ce65277 commit 2f6d852
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
6 changes: 6 additions & 0 deletions terraso_backend/apps/collaboration/graphql/memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ def resolve_account_membership(self, info):
user = info.context.user
if user.is_anonymous:
return None
if hasattr(self, "account_memberships"):
if len(self.account_memberships) > 0:
return self.account_memberships[0]
return None
return self.memberships.filter(user=user).first()

def resolve_memberships_count(self, info):
if hasattr(self, "memberships_count"):
return self.memberships_count
user = info.context.user
if user.is_anonymous:
return 0
Expand Down
30 changes: 9 additions & 21 deletions terraso_backend/apps/graphql/schema/landscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from apps.collaboration.graphql import CollaborationMembershipNode
from apps.collaboration.models import Membership as CollaborationMembership
from apps.collaboration.models import MembershipList
from apps.core import landscape_collaboration_roles
from apps.core.gis.utils import m2_to_hectares
from apps.core.models import (
Expand Down Expand Up @@ -99,19 +100,19 @@ def get_queryset(cls, queryset, info):
is_anonymous = info.context.user.is_anonymous

try:
# Prefetch default landscape group, account membership and count of members
group_queryset = (
Group.objects.prefetch_related(
# Prefetch account membership and count of members
membership_list_queryset = (
MembershipList.objects.prefetch_related(
Prefetch(
"memberships",
to_attr="account_memberships",
queryset=Membership.objects.filter(
queryset=CollaborationMembership.objects.filter(
user=info.context.user,
),
),
)
if not is_anonymous
else Group.objects.all()
else MembershipList.objects.all()
).annotate(
memberships_count=Count(
"memberships__user",
Expand All @@ -120,20 +121,14 @@ def get_queryset(cls, queryset, info):
& Q(memberships__membership_status=Membership.APPROVED),
)
)
landscape_group_queryset = LandscapeGroup.objects.prefetch_related(
Prefetch(
"group",
queryset=group_queryset,
),
).filter(is_default_landscape_group=True)

# Fetch all fields from Landscape, except for area_polygon
result = (
queryset.defer("area_polygon")
.prefetch_related(
Prefetch(
"associated_groups",
to_attr="default_landscape_groups",
queryset=landscape_group_queryset,
"membership_list",
queryset=membership_list_queryset,
)
)
.all()
Expand All @@ -147,13 +142,6 @@ def resolve_area_scalar_ha(self, info):
area = self.area_scalar_m2
return None if area is None else round(m2_to_hectares(area), 3)

def resolve_default_group(self, info):
if hasattr(self, "default_landscape_groups"):
if len(self.default_landscape_groups) > 0:
return self.default_landscape_groups[0].group
return None
return self.get_default_group()


class LandscapeDevelopmentStrategyNode(DjangoObjectType):
id = graphene.ID(source="pk", required=True)
Expand Down

0 comments on commit 2f6d852

Please sign in to comment.