Skip to content

Commit

Permalink
Merge pull request #131 from openimis/feature/fix_performance
Browse files Browse the repository at this point in the history
Fixed executing query too early
  • Loading branch information
hirensoni913 committed Mar 4, 2024
2 parents 0e352d9 + a9fc683 commit 29a2e1a
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions location/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def parents(self, location_id, loc_type=None):
)
return self.get_location_from_ids((parents), loc_type) if loc_type else parents

def allowed(self, user_id, loc_types=['R', 'D', 'W', 'V'], strict=True, qs = False):
def allowed(self, user_id, loc_types=['R', 'D', 'W', 'V'], strict=True, qs=False):
query = f"""
WITH {"" if settings.MSSQL else "RECURSIVE"} USER_LOC AS (SELECT l."LocationId", l."ParentLocationId" FROM "tblUsersDistricts" ud JOIN "tblLocations" l ON ud."LocationId" = l."LocationId" WHERE ud."ValidityTo" is Null AND "UserID" = %s ),
CTE_PARENTS AS (
Expand All @@ -72,20 +72,20 @@ def allowed(self, user_id, loc_types=['R', 'D', 'W', 'V'], strict=True, qs = Fal
)
SELECT DISTINCT "LocationId" FROM CTE_PARENTS WHERE "LocationType" in ('{"','".join(loc_types)}')
"""
if qs:
#location_allowed = Location.objects.filter( id__in =[obj.id for obj in Location.objects.raw( query,(user_id,))])
if settings.MSSQL: # MSSQL don't support WITH in subqueries

if qs is not None:
# location_allowed = Location.objects.filter( id__in =[obj.id for obj in Location.objects.raw( query,(user_id,))])
if settings.MSSQL: # MSSQL don't support WITH in subqueries

with connection.cursor() as cursor:
cursor.execute(query, (user_id,))
ids = cursor.fetchall()
location_allowed = Location.objects.filter( id__in =[x for x, in ids])
else:
location_allowed = Location.objects.filter( id__in =RawSQL( query,(user_id,)))
location_allowed = Location.objects.filter(id__in=[x for x, in ids])
else:
location_allowed = Location.objects.filter(id__in=RawSQL(query, (user_id,)))

else:
location_allowed = Location.objects.raw( query,(user_id,))
location_allowed = Location.objects.raw(query, (user_id,))

return location_allowed

Expand Down Expand Up @@ -124,19 +124,19 @@ def build_user_location_filter_query(self, user: core_models.InteractiveUser, pr
q_allowed_location = None
if not isinstance(user, core_models.InteractiveUser):
logger.warning(f"Access without filter for user {user.id} ")
if queryset:
if queryset is not None:
return queryset
else:
return Q()
elif not user.is_imis_admin:
q_allowed_location = Q((f"{prefix}__in", self.allowed(user.id, loc_types))) | Q((f"{prefix}__isnull", True))

if queryset:
if queryset is not None:
return queryset.filter(q_allowed_location)
else:
return q_allowed_location
else:
if queryset:
if queryset is not None:
return queryset
else:
return Q()
Expand Down Expand Up @@ -205,7 +205,7 @@ def get_queryset(cls, queryset, user):
elif user.is_imis_admin:
return Location.objects
else:
return cls.objects.allowed(user.i_user_id, qs = True)
return cls.objects.allowed(user.i_user_id, qs=True)
return queryset

@staticmethod
Expand Down Expand Up @@ -368,16 +368,16 @@ def get_user_districts(cls, user):
if user.is_superuser is True or (hasattr(user, "is_imis_admin") and user.is_imis_admin):
all_districts = Location.objects.filter(type='D', *filter_validity())
districts = []
idx = 0
for d in all_districts:
idx = 0
for d in all_districts:
districts.append(
UserDistrict(
id=idx,
user=user,
location=d
)
)

return districts

elif not isinstance(user, core_models.InteractiveUser):
Expand All @@ -387,7 +387,7 @@ def get_user_districts(cls, user):
return UserDistrict.objects.none()
else:
return (
UserDistrict.objects
UserDistrict.objects
.filter(location__type='D')
.filter(*filter_validity())
.filter(*filter_validity(prefix='location__'))
Expand All @@ -396,7 +396,7 @@ def get_user_districts(cls, user):
.prefetch_related("location__parent")
.order_by("location__parent__code")
.order_by("location__code")
)
)

@classmethod
def get_user_locations(cls, user):
Expand Down

0 comments on commit 29a2e1a

Please sign in to comment.