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

Fix queryset filters for asset daily rounds #1879

Merged
merged 1 commit into from
Feb 8, 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
32 changes: 11 additions & 21 deletions care/facility/api/serializers/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.utils.timezone import localtime, now
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404

# from care.facility.api.serializers.bed import BedSerializer
from care.facility.models import (
Expand All @@ -22,9 +21,9 @@
SYMPTOM_CHOICES,
SuggestionChoices,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.utils.notification_handler import NotificationGenerator
from care.utils.queryset.consultation import get_consultation_queryset
from care.utils.queryset.facility import get_home_facility_queryset
from config.serializers import ChoiceField

Expand Down Expand Up @@ -105,6 +104,7 @@
"glasgow_total_calculated",
"total_intake_calculated",
"total_output_calculated",
"consultation",
)
exclude = ("deleted",)

Expand Down Expand Up @@ -165,30 +165,19 @@
).generate()

def create(self, validated_data):
consultation: PatientConsultation = validated_data["consultation"]
# Authorisation Checks

# Skip check for asset user
if self.context["request"].user.asset_id is None:
allowed_facilities = get_home_facility_queryset(
self.context["request"].user
if (
not get_home_facility_queryset(self.context["request"].user)
.filter(id=consultation.facility_id)
.exists()
):
raise ValidationError(

Check warning on line 175 in care/facility/api/serializers/daily_round.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/daily_round.py#L175

Added line #L175 was not covered by tests
{"facility": "Daily Round creates are only allowed in home facility"}
)
if not allowed_facilities.filter(
id=self.validated_data["consultation"].facility.id
).exists():
raise ValidationError(
{
"facility": "Daily Round creates are only allowed in home facility"
}
)

# Authorisation Checks End

with transaction.atomic():
consultation = get_object_or_404(
get_consultation_queryset(self.context["request"].user).filter(
id=validated_data["consultation"].id
)
)
if (
validated_data.get("rounds_type")
== DailyRound.RoundsType.TELEMEDICINE.value
Expand Down Expand Up @@ -307,6 +296,7 @@

def validate(self, attrs):
validated = super().validate(attrs)
validated["consultation"] = self.context["consultation"]

if validated["consultation"].discharge_date:
raise ValidationError(
Expand Down
22 changes: 13 additions & 9 deletions care/facility/api/viewsets/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from care.facility.api.serializers.daily_round import DailyRoundSerializer
from care.facility.api.viewsets.mixins.access import AssetUserAccessMixin
from care.facility.models.daily_round import DailyRound
from care.facility.models.patient_consultation import PatientConsultation
from care.utils.queryset.consultation import get_consultation_queryset

DailyRoundAttributes = [f.name for f in DailyRound._meta.get_fields()]
Expand Down Expand Up @@ -57,16 +56,21 @@
PAGE_SIZE = 36 # One Round Per Hour

def get_queryset(self):
return self.queryset.filter(
consultation__external_id=self.kwargs["consultation_external_id"]
).order_by("-taken_at")
consultation = get_object_or_404(

Check warning on line 59 in care/facility/api/viewsets/daily_round.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/daily_round.py#L59

Added line #L59 was not covered by tests
get_consultation_queryset(self.request.user).filter(
external_id=self.kwargs["consultation_external_id"]
)
)
return self.queryset.filter(consultation=consultation).order_by("-taken_at")

Check warning on line 64 in care/facility/api/viewsets/daily_round.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/daily_round.py#L64

Added line #L64 was not covered by tests

def get_serializer(self, *args, **kwargs):
if "data" in kwargs:
kwargs["data"]["consultation"] = PatientConsultation.objects.get(
def get_serializer_context(self):
context = super().get_serializer_context()
context["consultation"] = get_object_or_404(
get_consultation_queryset(self.request.user).filter(
external_id=self.kwargs["consultation_external_id"]
).id
return super().get_serializer(*args, **kwargs)
)
)
return context

@extend_schema(tags=["daily_rounds"])
@action(methods=["POST"], detail=False)
Expand Down
6 changes: 4 additions & 2 deletions care/utils/queryset/consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
def get_consultation_queryset(user):
queryset = PatientConsultation.objects.all()
if user.is_superuser:
return queryset
if user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
pass

Check warning on line 11 in care/utils/queryset/consultation.py

View check run for this annotation

Codecov / codecov/patch

care/utils/queryset/consultation.py#L11

Added line #L11 was not covered by tests
elif hasattr(user, "asset") and user.asset is not None:
queryset = queryset.filter(facility=user.asset.current_location.facility_id)

Check warning on line 13 in care/utils/queryset/consultation.py

View check run for this annotation

Codecov / codecov/patch

care/utils/queryset/consultation.py#L13

Added line #L13 was not covered by tests
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
q_filters = Q(facility__state=user.state)
q_filters |= Q(patient__facility__state=user.state)
queryset = queryset.filter(q_filters)
Expand Down
4 changes: 4 additions & 0 deletions care/utils/queryset/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
queryset = Facility.objects.all()
if user.is_superuser:
pass
elif hasattr(user, "asset") and user.asset is not None:
queryset = queryset.filter(id=user.asset.current_location.facility_id)

Check warning on line 11 in care/utils/queryset/facility.py

View check run for this annotation

Codecov / codecov/patch

care/utils/queryset/facility.py#L11

Added line #L11 was not covered by tests
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
queryset = queryset.filter(state=user.state)
elif user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]:
Expand All @@ -21,6 +23,8 @@
queryset = Facility.objects.all()
if user.is_superuser:
pass
elif hasattr(user, "asset") and user.asset is not None:
queryset = queryset.filter(id=user.asset.current_location.facility_id)

Check warning on line 27 in care/utils/queryset/facility.py

View check run for this annotation

Codecov / codecov/patch

care/utils/queryset/facility.py#L27

Added line #L27 was not covered by tests
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
queryset = queryset.filter(state=user.state)
elif user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]:
Expand Down
Loading