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

refactor ambulance, asset, bed part tuples to choice classes #2578

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 5 additions & 5 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
class AssetLocationSerializer(ModelSerializer):
facility = FacilityBareMinimumSerializer(read_only=True)
id = UUIDField(source="external_id", read_only=True)
location_type = ChoiceField(choices=AssetLocation.RoomTypeChoices)
location_type = ChoiceField(choices=AssetLocation.RoomTypeChoices.choices)

def validate_middleware_address(self, value):
value = (value or "").strip()
Expand Down Expand Up @@ -142,8 +142,8 @@ class ResolvedMiddlewareField(serializers.JSONField):

class AssetSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
status = ChoiceField(choices=StatusChoices, read_only=True)
asset_type = ChoiceField(choices=AssetTypeChoices)
status = ChoiceField(choices=StatusChoices.choices, read_only=True)
asset_type = ChoiceField(choices=AssetTypeChoices.choices)
location_object = AssetLocationSerializer(source="current_location", read_only=True)
location = UUIDField(write_only=True, required=True)
last_service = AssetServiceSerializer(read_only=True)
Expand Down Expand Up @@ -313,8 +313,8 @@ def update(self, instance: Asset, validated_data):

class AssetPublicSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
status = ChoiceField(choices=StatusChoices, read_only=True)
asset_type = ChoiceField(choices=AssetTypeChoices)
status = ChoiceField(choices=StatusChoices.choices, read_only=True)
asset_type = ChoiceField(choices=AssetTypeChoices.choices)
location_object = AssetLocationSerializer(source="current_location", read_only=True)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class BedSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
name = CharField(max_length=1024, required=True)
bed_type = ChoiceField(choices=BedTypeChoices)
bed_type = ChoiceField(choices=BedTypeChoices.choices)

location_object = AssetLocationSerializer(source="location", read_only=True)
is_occupied = BooleanField(default=False, read_only=True)
Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/serializers/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from care.facility.events.handler import create_consultation_events
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
CategoryChoices,
PatientRegistration,
)
from care.facility.models.bed import Bed
Expand All @@ -31,7 +31,7 @@ class DailyRoundSerializer(serializers.ModelSerializer):
deprecated_covid_category = ChoiceField(
choices=COVID_CATEGORY_CHOICES, required=False
) # Deprecated
patient_category = ChoiceField(choices=CATEGORY_CHOICES, required=False)
patient_category = ChoiceField(choices=CategoryChoices.choices, required=False)

action = ChoiceField(
choices=PatientRegistration.ActionChoices, write_only=True, required=False
Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/serializers/hospital_doctor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rest_framework import serializers

from care.facility.api.serializers import TIMESTAMP_FIELDS
from care.facility.models import DOCTOR_TYPES, HospitalDoctors
from care.facility.models import DoctorType, HospitalDoctors
from care.utils.serializers.fields import ChoiceField


class HospitalDoctorSerializer(serializers.ModelSerializer):
area_text = ChoiceField(choices=DOCTOR_TYPES, read_only=True, source="area")
area_text = ChoiceField(choices=DoctorType.choices, read_only=True, source="area")
id = serializers.UUIDField(source="external_id", read_only=True)

class Meta:
Expand Down
21 changes: 11 additions & 10 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
PatientConsultationSerializer,
)
from care.facility.models import (
DISEASE_CHOICES,
GENDER_CHOICES,
Disease,
DiseaseChoices,
Facility,
FacilityPatientStatsHistory,
PatientContactDetails,
Expand All @@ -27,10 +27,9 @@
from care.facility.models.notification import Notification
from care.facility.models.patient import PatientNotesEdit
from care.facility.models.patient_base import (
BLOOD_GROUP_CHOICES,
DISEASE_STATUS_CHOICES,
DiseaseStatusEnum,
NewDischargeReasonEnum,
BloodGroupChoices,
DiseaseStatusChoices,
NewDischargeReasonChoices,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.facility.models.patient_external_test import PatientExternalTest
Expand Down Expand Up @@ -80,9 +79,10 @@ class PatientListSerializer(serializers.ModelSerializer):

last_consultation = PatientConsultationSerializer(read_only=True)

blood_group = ChoiceField(choices=BLOOD_GROUP_CHOICES, required=True)
blood_group = ChoiceField(choices=BloodGroupChoices.choices, required=True)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
choices=DiseaseStatusChoices.choices,
default=DiseaseStatusChoices.SUSPECTED.value,
)
source = ChoiceField(choices=PatientRegistration.SourceChoices)

Expand Down Expand Up @@ -129,7 +129,7 @@ def to_internal_value(self, data):

class PatientDetailSerializer(PatientListSerializer):
class MedicalHistorySerializer(serializers.Serializer):
disease = ChoiceField(choices=DISEASE_CHOICES)
disease = ChoiceField(choices=DiseaseChoices.choices)
details = serializers.CharField(required=False, allow_blank=True)

facility = ExternalIdSerializerField(
Expand All @@ -147,7 +147,8 @@ class MedicalHistorySerializer(serializers.Serializer):
default=PatientRegistration.SourceEnum.CARE.value,
)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
choices=DiseaseStatusChoices.choices,
default=DiseaseStatusChoices.SUSPECTED.value,
)

meta_info = PatientMetaInfoSerializer(required=False, allow_null=True)
Expand Down Expand Up @@ -434,7 +435,7 @@ def update(self, instance, validated_data):

if consultation:
consultation.discharge_date = now()
consultation.new_discharge_reason = NewDischargeReasonEnum.REFERRED
consultation.new_discharge_reason = NewDischargeReasonChoices.REFERRED
consultation.current_bed = None
consultation.save()

Expand Down
14 changes: 7 additions & 7 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from care.facility.api.serializers.facility import FacilityBasicInfoSerializer
from care.facility.events.handler import create_consultation_events
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
CategoryChoices,
Facility,
PatientRegistration,
Prescription,
Expand All @@ -48,7 +48,7 @@
)
from care.facility.models.notification import Notification
from care.facility.models.patient_base import (
NewDischargeReasonEnum,
NewDischargeReasonChoices,
RouteToFacility,
SuggestionChoices,
)
Expand Down Expand Up @@ -83,7 +83,7 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
deprecated_covid_category = ChoiceField(
choices=COVID_CATEGORY_CHOICES, required=False
)
category = ChoiceField(choices=CATEGORY_CHOICES, required=True)
category = ChoiceField(choices=CategoryChoices.choices, required=True)

referred_to_object = FacilityBasicInfoSerializer(
source="referred_to", read_only=True
Expand Down Expand Up @@ -134,7 +134,7 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
)

new_discharge_reason = serializers.ChoiceField(
choices=NewDischargeReasonEnum.choices, read_only=True, required=False
choices=NewDischargeReasonChoices.choices, read_only=True, required=False
)
discharge_notes = serializers.CharField(read_only=True)

Expand Down Expand Up @@ -693,7 +693,7 @@ def validate(self, attrs): # noqa: PLR0912

class PatientConsultationDischargeSerializer(serializers.ModelSerializer):
new_discharge_reason = serializers.ChoiceField(
choices=NewDischargeReasonEnum.choices, required=True
choices=NewDischargeReasonChoices.choices, required=True
)
discharge_notes = serializers.CharField(required=False, allow_blank=True)

Expand Down Expand Up @@ -756,11 +756,11 @@ def validate(self, attrs):
],
}
)
if attrs.get("new_discharge_reason") != NewDischargeReasonEnum.EXPIRED:
if attrs.get("new_discharge_reason") != NewDischargeReasonChoices.EXPIRED:
attrs.pop("death_datetime", None)
attrs.pop("death_confirmed_doctor", None)

if attrs.get("new_discharge_reason") == NewDischargeReasonEnum.EXPIRED:
if attrs.get("new_discharge_reason") == NewDischargeReasonChoices.EXPIRED:
if not attrs.get("death_datetime"):
raise ValidationError({"death_datetime": "This field is required"})
if attrs.get("death_datetime") > now():
Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/serializers/patient_icmr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from care.facility.models import DISEASE_CHOICES, SAMPLE_TYPE_CHOICES
from care.facility.models import SAMPLE_TYPE_CHOICES, DiseaseChoices
from care.facility.models.patient_icmr import (
PatientConsultationICMR,
PatientIcmr,
Expand Down Expand Up @@ -133,7 +133,7 @@ class ICMRMedicalConditionSerializer(serializers.ModelSerializer):
hospital_pincode = serializers.CharField(source="consultation.facility.pincode")

medical_conditions_list = serializers.ListSerializer(
child=ChoiceField(choices=DISEASE_CHOICES)
child=ChoiceField(choices=DiseaseChoices.choices)
)

class Meta:
Expand Down
14 changes: 7 additions & 7 deletions care/facility/api/serializers/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
)
from care.facility.models import (
BREATHLESSNESS_CHOICES,
CATEGORY_CHOICES,
FACILITY_TYPES,
SHIFTING_STATUS_CHOICES,
VEHICLE_CHOICES,
CategoryChoices,
Facility,
PatientRegistration,
ShiftingRequest,
Expand All @@ -25,9 +25,8 @@
from care.facility.models.bed import ConsultationBed
from care.facility.models.notification import Notification
from care.facility.models.patient_base import (
DISEASE_STATUS_CHOICES,
DiseaseStatusEnum,
NewDischargeReasonEnum,
DiseaseStatusChoices,
NewDischargeReasonChoices,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.api.serializers.lsg import StateSerializer
Expand Down Expand Up @@ -77,7 +76,7 @@ def discharge_patient(patient: PatientRegistration):
PatientConsultation.objects.filter(patient=patient).order_by("-id").first()
)
if last_consultation:
reason = NewDischargeReasonEnum.REFERRED
reason = NewDischargeReasonChoices.REFERRED
notes = "Patient Shifted to another facility"
last_consultation.new_discharge_reason = reason
last_consultation.discharge_notes = notes
Expand Down Expand Up @@ -225,7 +224,7 @@ class ShiftingSerializer(serializers.ModelSerializer):
last_edited_by_object = UserBaseMinimumSerializer(
source="last_edited_by", read_only=True
)
patient_category = ChoiceField(choices=CATEGORY_CHOICES, required=False)
patient_category = ChoiceField(choices=CategoryChoices.choices, required=False)
ambulance_driver_name = serializers.CharField(
required=False, allow_null=True, allow_blank=True
)
Expand Down Expand Up @@ -461,7 +460,8 @@ class PatientShiftingBareMinimumSerializer(serializers.ModelSerializer):
)
state_object = StateSerializer(source="state", read_only=True)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
choices=DiseaseStatusChoices.choices,
default=DiseaseStatusChoices.SUSPECTED.value,
)
age = serializers.SerializerMethodField()

Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
logger = logging.getLogger(__name__)


inverse_asset_type = inverse_choices(AssetTypeChoices)
inverse_asset_status = inverse_choices(StatusChoices)
inverse_asset_type = inverse_choices(AssetTypeChoices.choices)
inverse_asset_status = inverse_choices(StatusChoices.choices)


@receiver(post_save, sender=Asset)
Expand Down
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from care.utils.filters.choicefilter import CareChoiceFilter, inverse_choices
from care.utils.queryset.asset_bed import get_asset_bed_queryset, get_bed_queryset

inverse_bed_type = inverse_choices(BedTypeChoices)
inverse_bed_type = inverse_choices(BedTypeChoices.choices)


class BedFilter(filters.FilterSet):
Expand Down
12 changes: 6 additions & 6 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
from care.facility.api.viewsets.mixins.history import HistoryMixin
from care.facility.events.handler import create_consultation_events
from care.facility.models import (
CATEGORY_CHOICES,
COVID_CATEGORY_CHOICES,
DISCHARGE_REASON_CHOICES,
FACILITY_TYPES,
BedTypeChoices,
CategoryChoices,
DailyRound,
Facility,
FacilityPatientStatsHistory,
Expand All @@ -75,7 +75,7 @@
from care.facility.models.patient import PatientNotesEdit, RationCardCategory
from care.facility.models.patient_base import (
DISEASE_STATUS_DICT,
NewDischargeReasonEnum,
NewDischargeReasonChoices,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.models import User
Expand All @@ -91,7 +91,7 @@
)

REVERSE_FACILITY_TYPES = covert_choice_dict(FACILITY_TYPES)
REVERSE_BED_TYPES = covert_choice_dict(BedTypeChoices)
REVERSE_BED_TYPES = covert_choice_dict(BedTypeChoices.choices)
DISCHARGE_REASONS = [choice[0] for choice in DISCHARGE_REASON_CHOICES]


Expand Down Expand Up @@ -122,7 +122,7 @@ class PatientFilterSet(filters.FilterSet):
)
category = filters.ChoiceFilter(
method="filter_by_category",
choices=CATEGORY_CHOICES,
choices=CategoryChoices.choices,
)
ration_card_category = filters.ChoiceFilter(choices=RationCardCategory.choices)

Expand Down Expand Up @@ -210,7 +210,7 @@ def filter_by_bed_type(self, queryset, name, value):
)
last_consultation__new_discharge_reason = filters.ChoiceFilter(
field_name=f"{last_consultation_field}__new_discharge_reason",
choices=NewDischargeReasonEnum.choices,
choices=NewDischargeReasonChoices.choices,
)
last_consultation_assigned_to = filters.NumberFilter(
field_name=f"{last_consultation_field}__assigned_to"
Expand Down Expand Up @@ -357,7 +357,7 @@ def filter_queryset(self, request, queryset, view):
if ordering in ("category_severity", "-category_severity"):
category_ordering = {
category: index + 1
for index, (category, _) in enumerate(CATEGORY_CHOICES)
for index, (category, _) in enumerate(CategoryChoices.choices)
}
when_statements = [
When(last_consultation__category=cat, then=order)
Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/viewsets/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
from care.facility.models.patient_base import (
DISEASE_STATUS_DICT,
NewDischargeReasonEnum,
NewDischargeReasonChoices,
)
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities
from care.utils.filters.choicefilter import CareChoiceFilter
Expand Down Expand Up @@ -186,7 +186,7 @@ def transfer(self, request, *args, **kwargs):
patient=patient, discharge_date__isnull=True
).update(
discharge_date=localtime(now()),
new_discharge_reason=NewDischargeReasonEnum.REFERRED,
new_discharge_reason=NewDischargeReasonChoices.REFERRED,
)
ConsultationBed.objects.filter(
consultation=patient.last_consultation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import migrations

from care.facility.models.patient_base import DiseaseStatusEnum
from care.facility.models.patient_base import DiseaseStatusChoices


def change_recovery_to_recovered(apps, schema_editor):
PatientRegistration = apps.get_model("facility", "PatientRegistration")
PatientRegistration.objects.filter(
disease_status=DiseaseStatusEnum.RECOVERY.value
).update(disease_status=DiseaseStatusEnum.RECOVERED.value)
disease_status=DiseaseStatusChoices.RECOVERY.value
).update(disease_status=DiseaseStatusChoices.RECOVERED.value)


class Migration(migrations.Migration):
Expand Down
Loading