Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update queryset references in views to avoid cached results #2582

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading