Skip to content

Commit

Permalink
(optimization): Prefetch related org objects
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <arapgodsmack@gmail.com>
  • Loading branch information
KipSigei committed Oct 2, 2023
1 parent 0625ede commit 6c63699
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
34 changes: 31 additions & 3 deletions onadata/apps/api/viewsets/organization_profile_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from django.conf import settings
from django.core.cache import cache
from django.db.models import Prefetch
from django.utils.module_loading import import_string

from rest_framework import status
Expand All @@ -16,7 +17,7 @@
from rest_framework.viewsets import ModelViewSet

from onadata.apps.api import permissions
from onadata.apps.api.models.organization_profile import OrganizationProfile
from onadata.apps.api.models.organization_profile import OrganizationProfile, OrgProfileUserObjectPermission, OrgProfileGroupObjectPermission
from onadata.apps.api.tools import get_baseviewset_class
from onadata.libs.filters import (
OrganizationPermissionFilter,
Expand Down Expand Up @@ -56,8 +57,35 @@ class OrganizationProfileViewSet(
"""
List, Retrieve, Update, Create/Register Organizations.
"""

queryset = OrganizationProfile.objects.filter(user__is_active=True)
queryset = (
OrganizationProfile.objects.select_related("user__profile")
.prefetch_related(
Prefetch(
"organizationprofile",
queryset=OrganizationProfile.objects.filter(user__is_active=True)
.select_related("user")
.prefetch_related("user")
.prefetch_related("userprofileuserobjectpermission_set")
.prefetch_related("orgprofileuserobjectpermission_set")
),
)
.prefetch_related(
Prefetch(
"orgprofileuserobjectpermission_set",
queryset=OrgProfileUserObjectPermission.objects.select_related(
"user__profile__organizationprofile", "permission"
),
)
)
.prefetch_related(
Prefetch(
"orgprofilegroupobjectpermission_set",
queryset=OrgProfileGroupObjectPermission.objects.select_related(
"group", "permission"
),
)
)
)
serializer_class = serializer_from_settings()
lookup_field = "user"
permission_classes = [permissions.OrganizationProfilePermissions]
Expand Down
2 changes: 2 additions & 0 deletions onadata/libs/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ def is_organization(obj):
return False


@lru_cache(maxsize=None)
def get_role(permissions, obj):
"""
Return the user role for the given obj permissions.
Expand All @@ -449,6 +450,7 @@ def get_role(permissions, obj):
return None


@lru_cache(maxsize=None)
def get_role_in_org(user, organization):
"""
Return the user role in the organization.
Expand Down
4 changes: 4 additions & 0 deletions onadata/libs/serializers/organization_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Organization Serializer
"""

from functools import lru_cache

from django.contrib.auth import get_user_model
from django.db.models.query import QuerySet
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -118,11 +120,13 @@ def validate_org(self, value):

raise serializers.ValidationError(_(f"Organization {org} already exists."))

@lru_cache(maxsize=None)
def get_users(self, obj):
"""
Return organization members.
"""

@lru_cache(maxsize=None)
def _create_user_list(user_list):
users_list = []
for u in user_list:
Expand Down

0 comments on commit 6c63699

Please sign in to comment.