Skip to content

Commit

Permalink
Update queryset references in views to avoid cached results (#2582)
Browse files Browse the repository at this point in the history
* update queryset references in views to avoid cached results

* fix
  • Loading branch information
sainak authored Nov 6, 2024
1 parent 5cba13c commit d6d069e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/mixins/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class UserAccessMixin:
def get_queryset(self):
queryset = self.queryset
queryset = super().get_queryset()
model = self.queryset.__dict__["model"]

if not self.request.user.is_superuser:
Expand Down
13 changes: 6 additions & 7 deletions care/facility/api/viewsets/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ def get_permissions(self):
return super().get_permissions()

def get_queryset(self):
queryset = super().get_queryset()
if self.serializer_class == PatientConsultationSerializer:
self.queryset = self.queryset.prefetch_related(
queryset = queryset.prefetch_related(
"assigned_to",
Prefetch(
"assigned_to__skills",
Expand All @@ -95,13 +96,11 @@ def get_queryset(self):
"current_bed__assets__current_location",
)
if self.request.user.is_superuser:
return self.queryset
return queryset
if self.request.user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
return self.queryset.filter(
patient__facility__state=self.request.user.state
)
return queryset.filter(patient__facility__state=self.request.user.state)
if self.request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]:
return self.queryset.filter(
return queryset.filter(
patient__facility__district=self.request.user.district
)
allowed_facilities = get_accessible_facilities(self.request.user)
Expand All @@ -111,7 +110,7 @@ def get_queryset(self):
)
# A user should be able to see all consultations part of their home facility
applied_filters |= Q(facility=self.request.user.home_facility)
return self.queryset.filter(applied_filters)
return queryset.filter(applied_filters)

@transaction.non_atomic_requests
def create(self, request, *args, **kwargs) -> Response:
Expand Down
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/patient_external_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PatientExternalTestViewSet(
parser_classes = (MultiPartParser, FormParser, JSONParser)

def get_queryset(self):
queryset = self.queryset
queryset = super().get_queryset()
if not self.request.user.is_superuser:
if self.request.user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
queryset = queryset.filter(district__state=self.request.user.state)
Expand Down
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/patient_otp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OTPPatientDataViewSet(RetrieveModelMixin, ListModelMixin, GenericViewSet):

def get_queryset(self):
is_otp_login = getattr(self.request.user, "is_alternative_login", False)
queryset = self.queryset
queryset = super().get_queryset()
if is_otp_login:
queryset = queryset.filter(phone_number=self.request.user.phone_number)
else:
Expand Down
7 changes: 4 additions & 3 deletions care/facility/api/viewsets/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ class ShiftingViewSet(
filterset_class = ShiftingFilterSet

def get_queryset(self) -> QuerySet:
queryset = super().get_queryset()
if self.action == "list":
self.queryset = self.queryset.select_related(
queryset = queryset.select_related(
"origin_facility",
"shifting_approving_facility",
"assigned_facility",
"patient",
)

else:
self.queryset = self.queryset.select_related(
queryset = queryset.select_related(
"origin_facility",
"origin_facility__ward",
"origin_facility__local_body",
Expand Down Expand Up @@ -148,7 +149,7 @@ def get_queryset(self) -> QuerySet:
"created_by",
"last_edited_by",
)
return self.queryset
return queryset

def get_serializer_class(self):
serializer_class = self.serializer_class
Expand Down
4 changes: 2 additions & 2 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class UserViewSet(

def get_queryset(self):
if self.request.user.is_superuser:
return self.queryset
return super().get_queryset()
query = Q(id=self.request.user.id)
if self.request.user.user_type >= User.TYPE_VALUE_MAP["StateReadOnlyAdmin"]:
query |= Q(
Expand Down Expand Up @@ -178,7 +178,7 @@ def getcurrentuser(self, request):
)

def destroy(self, request, *args, **kwargs):
queryset = self.queryset
queryset = self.get_queryset()
username = kwargs["username"]
if request.user.is_superuser:
pass
Expand Down

0 comments on commit d6d069e

Please sign in to comment.