-
Notifications
You must be signed in to change notification settings - Fork 621
[ENG-830] feat:support for diagnsotic reports and medication prescriptions at Patient OTP level #3720
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
Open
nandkishorr
wants to merge
14
commits into
develop
Choose a base branch
from
ENG-830-support-for-medication-prescriptions-and-diagnsotic-report-at-patient-otp-level
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[ENG-830] feat:support for diagnsotic reports and medication prescriptions at Patient OTP level #3720
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
907250e
feat:support for diagnsotic reports and medication prescriptions at p…
nandkishorr fe2e25a
feat:added filters
nandkishorr 8680078
cleanup
nandkishorr 1fb3bec
filter updates
nandkishorr 45b25cd
add encounter filter
nandkishorr 9030278
added appointment associated encounter
nandkishorr 4bce27b
feat:added default filter for otp based viewsets
nandkishorr 211e46c
feat:added queryset mixin based on env enabling
nandkishorr 31fb098
feat:added default filter setup and cleanup
nandkishorr f141b87
added testcases
nandkishorr bff3fbf
review change
nandkishorr 5a3a5d5
Merge branch 'develop' into ENG-830-support-for-medication-prescripti…
nandkishorr d474991
Merge branch 'develop' into ENG-830-support-for-medication-prescripti…
nandkishorr 2c87cd8
review changes added
nandkishorr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import json | ||
| from enum import Enum | ||
| from os import getenv | ||
|
|
||
| from django.conf import settings | ||
| from django.core.exceptions import FieldError | ||
| from django_filters.constants import EMPTY_VALUES | ||
| from rest_framework.exceptions import ValidationError | ||
|
|
||
| from care.emr.api.viewsets.base import EMRBaseViewSet | ||
| from config.patient_otp_authentication import ( | ||
| JWTTokenPatientAuthentication, | ||
| OTPAuthenticatedPermission, | ||
| ) | ||
|
|
||
|
|
||
| class OTPResourceType(str, Enum): | ||
| diagnostic_report = "diagnostic_report" | ||
| medication_request_prescription = "medication_request_prescription" | ||
|
|
||
|
|
||
| class OTPBaseViewset(EMRBaseViewSet): | ||
| """ | ||
| Base viewset for OTP authenticated endpoints. | ||
| """ | ||
|
|
||
| authentication_classes = [JWTTokenPatientAuthentication] | ||
| permission_classes = [OTPAuthenticatedPermission] | ||
|
|
||
|
|
||
| class QuerysetEnablerMixin: | ||
| """ | ||
| Mixin to enable queryset filtering based on the presence of a filterset_class attribute. | ||
| """ | ||
|
|
||
| resource_type = OTPResourceType | ||
|
|
||
| def config_key(self): | ||
| return f"OTP_{self.resource_type.value.upper()}_FILTERS" | ||
|
|
||
|
nandkishorr marked this conversation as resolved.
|
||
| def get_env_value(self, key): | ||
| config = getenv(key) | ||
| if not config: | ||
| return {} | ||
| try: | ||
| return json.loads(config) | ||
| except json.JSONDecodeError as e: | ||
| raise ValidationError( | ||
| {key: "Invalid JSON in default filter configuration."} | ||
| ) from e | ||
|
|
||
| def get_read_filters(self): | ||
| return self.get_env_value(self.config_key()) | ||
|
|
||
| def apply_default_filters(self, queryset): | ||
| read_filters = self.get_read_filters() | ||
| if not read_filters: | ||
| return queryset.none() | ||
| query_params = self.request.query_params | ||
| allowed_filters = set(getattr(self.filterset_class, "base_filters", {})) | ||
| for filter_config in read_filters: | ||
| filter_name = filter_config.get("name") | ||
| properties = filter_config.get("properties", {}) | ||
| if not filter_name or filter_name in query_params: | ||
| continue | ||
| if filter_name not in allowed_filters: | ||
| raise ValidationError({filter_name: "Invalid filter"}) | ||
| value = properties.get("value") | ||
| if value in EMPTY_VALUES: | ||
| continue | ||
|
|
||
| field_name = properties.get("field_name", filter_name) | ||
| lookup_expr = properties.get("lookup_expr", "exact") | ||
| try: | ||
| queryset = queryset.filter(**{f"{field_name}__{lookup_expr}": value}) | ||
| except FieldError as e: | ||
| raise ValidationError( | ||
| {filter_name: "Invalid default filter configuration"} | ||
| ) from e | ||
| return queryset | ||
|
|
||
| def get_queryset(self): | ||
| if not settings.OTP_QUERYSET_ENABLED: | ||
| return self.database_model.objects.none() | ||
| queryset = super().get_queryset() | ||
| if self.action == "list": | ||
| return self.apply_default_filters(queryset) | ||
| return queryset | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from django_filters import rest_framework as filters | ||
|
|
||
| from care.emr.api.otp_viewsets.base import ( | ||
| OTPBaseViewset, | ||
| OTPResourceType, | ||
| QuerysetEnablerMixin, | ||
| ) | ||
| from care.emr.api.viewsets.base import EMRListMixin, EMRRetrieveMixin | ||
| from care.emr.models.diagnostic_report import DiagnosticReport | ||
| from care.emr.resources.diagnostic_report.spec import ( | ||
| DiagnosticReportListSpec, | ||
| DiagnosticReportRetrieveSpec, | ||
| ) | ||
| from care.utils.filters.multiselect import MultiSelectFilter | ||
|
|
||
|
|
||
| class OTPDiagnosticReportFilters(filters.FilterSet): | ||
| facility = filters.UUIDFilter(field_name="facility__external_id") | ||
| status = MultiSelectFilter(field_name="status") | ||
| encounter = filters.UUIDFilter(field_name="encounter__external_id") | ||
| patient = filters.UUIDFilter(field_name="patient__external_id") | ||
| created_date = filters.DateTimeFromToRangeFilter(field_name="created_date") | ||
|
|
||
|
|
||
| class OTPDiagnosticReportViewSet( | ||
| QuerysetEnablerMixin, EMRRetrieveMixin, OTPBaseViewset, EMRListMixin | ||
| ): | ||
| database_model = DiagnosticReport | ||
| pydantic_read_model = DiagnosticReportListSpec | ||
| pydantic_retrieve_model = DiagnosticReportRetrieveSpec | ||
| filterset_class = OTPDiagnosticReportFilters | ||
| filter_backends = [filters.DjangoFilterBackend] | ||
| resource_type = OTPResourceType.diagnostic_report | ||
|
|
||
| def get_queryset(self): | ||
| return ( | ||
| super() | ||
| .get_queryset() | ||
| .filter(patient__phone_number=self.request.user.phone_number) | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
nandkishorr marked this conversation as resolved.
|
||
43 changes: 43 additions & 0 deletions
43
care/emr/api/otp_viewsets/medication_request_prescription.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from django_filters import rest_framework as filters | ||
|
|
||
| from care.emr.api.otp_viewsets.base import ( | ||
| OTPBaseViewset, | ||
| OTPResourceType, | ||
| QuerysetEnablerMixin, | ||
| ) | ||
| from care.emr.api.viewsets.base import EMRListMixin, EMRRetrieveMixin | ||
| from care.emr.models.medication_request import MedicationRequestPrescription | ||
| from care.emr.resources.medication.request_prescription.spec import ( | ||
| MedicationRequestPrescriptionReadSpec, | ||
| MedicationRequestPrescriptionRetrieveMedicationsSpec, | ||
| ) | ||
| from care.utils.filters.multiselect import MultiSelectFilter | ||
|
|
||
|
|
||
| class OTPMedicationRequestPrescriptionFilters(filters.FilterSet): | ||
| facility = filters.UUIDFilter(field_name="encounter__facility__external_id") | ||
| status = MultiSelectFilter(field_name="status") | ||
| encounter = filters.UUIDFilter(field_name="encounter__external_id") | ||
| patient = filters.UUIDFilter(field_name="patient__external_id") | ||
| created_date = filters.DateTimeFromToRangeFilter(field_name="created_date") | ||
|
|
||
|
|
||
| class OTPMedicationRequestPrescriptionViewSet( | ||
| QuerysetEnablerMixin, | ||
| EMRRetrieveMixin, | ||
| OTPBaseViewset, | ||
| EMRListMixin, | ||
| ): | ||
| database_model = MedicationRequestPrescription | ||
| pydantic_read_model = MedicationRequestPrescriptionReadSpec | ||
| pydantic_retrieve_model = MedicationRequestPrescriptionRetrieveMedicationsSpec | ||
| filterset_class = OTPMedicationRequestPrescriptionFilters | ||
| filter_backends = [filters.DjangoFilterBackend] | ||
| resource_type = OTPResourceType.medication_request_prescription | ||
|
|
||
| def get_queryset(self): | ||
| return ( | ||
| super() | ||
| .get_queryset() | ||
| .filter(patient__phone_number=self.request.user.phone_number) | ||
| ) | ||
|
nandkishorr marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.