Exemple #1
0
    def get_queryset(self):
        if self.action != "list":
            return super(PatientSearchViewSet, self).get_queryset()
        else:
            facility_id = self.request.query_params.get("facility_id")
            serializer = PatientSearchSerializer(
                data=self.request.query_params, partial=True)
            serializer.is_valid(raise_exception=True)

            search_keys = [
                "year_of_birth", "phone_number", "name", "age", "facility_id"
            ]
            search_fields = {
                key: serializer.validated_data[key]
                for key in search_keys if serializer.validated_data.get(key)
            }
            if not search_fields and not facility_id:
                raise serializers.ValidationError({
                    "detail": [
                        f"None of the search keys provided. Available: {', '.join(search_keys)}"
                    ]
                })

            if not self.request.user.is_superuser:
                search_fields["state_id"] = self.request.user.state_id

            if "age" in search_fields:
                age = search_fields.pop("age")
                year_of_birth = datetime.datetime.now().year - age
                search_fields["age__gte"] = year_of_birth - 5
                search_fields["age__lte"] = year_of_birth + 5
            if facility_id:
                facility = Facility.objects.filter(id=facility_id)
                if not facility:
                    raise exceptions.ValidationError(
                        {"facility_id": ["Invalid facility_id"]})
                patients = PatientRegistration.objects.filter(
                    facility=facility[0]).values_list("patient_search_id",
                                                      flat=True)
                if patients:
                    search_fields["pk__in"] = list(patients)
            name = search_fields.pop("name", None)
            queryset = []
            if search_fields:
                queryset = self.queryset.filter(**search_fields)

            if name:
                queryset = (queryset.annotate(
                    similarity=TrigramSimilarity("name", name)).filter(
                        similarity__gt=0.2).order_by("-similarity"))

            return queryset
Exemple #2
0
    def get_queryset(self):
        if self.action != "list":
            return super(PatientSearchViewSet, self).get_queryset()
        else:
            serializer = PatientSearchSerializer(
                data=self.request.query_params, partial=True)
            serializer.is_valid(raise_exception=True)
            if self.request.user.user_type >= User.TYPE_VALUE_MAP[
                    "DistrictLabAdmin"]:
                search_keys = [
                    "date_of_birth",
                    "year_of_birth",
                    "phone_number",
                    "name",
                    "age",
                ]
            else:
                search_keys = [
                    "date_of_birth", "year_of_birth", "phone_number", "age"
                ]
            search_fields = {
                key: serializer.validated_data[key]
                for key in search_keys if serializer.validated_data.get(key)
            }
            if not search_fields:
                raise serializers.ValidationError({
                    "detail": [
                        f"None of the search keys provided. Available: {', '.join(search_keys)}"
                    ]
                })

            # if not self.request.user.is_superuser:
            #     search_fields["state_id"] = self.request.user.state_id

            if "age" in search_fields:
                age = search_fields.pop("age")
                year_of_birth = datetime.datetime.now().year - age
                search_fields["age__gte"] = year_of_birth - 5
                search_fields["age__lte"] = year_of_birth + 5

            name = search_fields.pop("name", None)

            queryset = self.queryset.filter(**search_fields)

            if name:
                queryset = (queryset.annotate(
                    similarity=TrigramSimilarity("name", name)).filter(
                        similarity__gt=0.2).order_by("-similarity"))

            return queryset
Exemple #3
0
    def get_queryset(self):
        if self.action != "list":
            return super(PatientSearchViewSet, self).get_queryset()
        else:
            serializer = PatientSearchSerializer(
                data=self.request.query_params, partial=True)
            serializer.is_valid(raise_exception=True)

            search_keys = ["date_of_birth", "year_of_birth", "phone_number"]
            search_fields = {
                key: serializer.validated_data[key]
                for key in search_keys if serializer.validated_data.get(key)
            }
            if not search_fields:
                raise serializers.ValidationError({
                    "detail": [
                        f"None of the search keys provided. Available: {', '.join(search_keys)}"
                    ]
                })

            if not self.request.user.is_superuser:
                search_fields["state_id"] = self.request.user.state_id
            return self.queryset.filter(**search_fields)