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

Removed redundant room types in FacilityCapacity #2417

Merged
merged 6 commits into from
Sep 23, 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
6 changes: 4 additions & 2 deletions care/facility/api/serializers/facility_capacity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from rest_framework import serializers

from care.facility.api.serializers import TIMESTAMP_FIELDS
from care.facility.models import ROOM_TYPES, FacilityCapacity
from care.facility.models import FacilityCapacity, RoomType
from config.serializers import ChoiceField


class FacilityCapacitySerializer(serializers.ModelSerializer):
room_type_text = ChoiceField(choices=ROOM_TYPES, read_only=True, source="room_type")
room_type_text = ChoiceField(
choices=RoomType.choices, read_only=True, source="room_type"
)
id = serializers.UUIDField(source="external_id", read_only=True)

def validate(self, data):
Expand Down Expand Up @@ -37,9 +39,9 @@


class FacilityCapacityHistorySerializer(serializers.ModelSerializer):
def __init__(self, model, *args, **kwargs):

Check failure on line 42 in care/facility/api/serializers/facility_capacity.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ARG002)

care/facility/api/serializers/facility_capacity.py:42:32: ARG002 Unused method argument: `args`

Check failure on line 42 in care/facility/api/serializers/facility_capacity.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ARG002)

care/facility/api/serializers/facility_capacity.py:42:40: ARG002 Unused method argument: `kwargs`
self.Meta.model = model
super().__init__()

class Meta:
exclude = TIMESTAMP_FIELDS + ("facility",)

Check failure on line 47 in care/facility/api/serializers/facility_capacity.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF005)

care/facility/api/serializers/facility_capacity.py:47:19: RUF005 Consider `(*TIMESTAMP_FIELDS, "facility")` instead of concatenation
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Generated by Django 4.2.2 on 2024-09-02 09:42

from django.db import migrations, models

from care.facility.models import RoomType


class Migration(migrations.Migration):
dependencies = [
("facility", "0463_patientnotes_reply_to"),
]

Check failure on line 11 in care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py:9:20: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

def migrate_room_type(apps, schema_editor):

Check failure on line 13 in care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (N805)

care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py:13:27: N805 First argument of a method should be named `self`

Check failure on line 13 in care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ARG002)

care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py:13:33: ARG002 Unused method argument: `schema_editor`
FacilityCapacity = apps.get_model("facility", "FacilityCapacity")

room_type_migration_map = {
1: RoomType.GENERAL_BED, # General Bed
10: RoomType.ICU_BED, # ICU
20: RoomType.ICU_BED, # Ventilator
30: RoomType.GENERAL_BED, # Covid Beds
100: RoomType.ICU_BED, # Covid Ventilators
110: RoomType.ICU_BED, # Covid ICU
120: RoomType.OXYGEN_BED, # Covid Oxygen beds
150: RoomType.OXYGEN_BED, # Oxygen beds
0: RoomType.OTHER, # Total
2: RoomType.OTHER, # Hostel
3: RoomType.ISOLATION_BED, # Single Room with Attached Bathroom
40: RoomType.GENERAL_BED, # KASP Beds
50: RoomType.ICU_BED, # KASP ICU beds
60: RoomType.OXYGEN_BED, # KASP Oxygen beds
70: RoomType.ICU_BED, # KASP Ventilator beds
}

merged_facility_capacities = {}

for old_type, new_type in room_type_migration_map.items():
facility_capacities = FacilityCapacity.objects.filter(room_type=old_type)

for facility_capacity in facility_capacities:
key = (facility_capacity.facility.external_id, new_type)

if key not in merged_facility_capacities:
merged_facility_capacities[key] = {
"facility": facility_capacity.facility,
"room_type": new_type,
"total_capacity": facility_capacity.total_capacity,
"current_capacity": facility_capacity.current_capacity,
}
else:
merged_facility_capacities[key]["total_capacity"] += (
facility_capacity.total_capacity
)
merged_facility_capacities[key]["current_capacity"] += (
facility_capacity.current_capacity
)

facility_capacity.delete()

for data in merged_facility_capacities.values():
FacilityCapacity.objects.create(**data)

operations = [
migrations.RunPython(migrate_room_type, migrations.RunPython.noop),
migrations.AlterField(
model_name="facilitycapacity",
name="room_type",
field=models.IntegerField(
choices=[
(100, "ICU Bed"),
(200, "Ordinary Bed"),
(300, "Oxygen Bed"),
(400, "Isolation Bed"),
(500, "Others"),
]
),
),
migrations.AlterField(
model_name="historicalfacilitycapacity",
name="room_type",
field=models.IntegerField(
choices=[
(100, "ICU Bed"),
(200, "Ordinary Bed"),
(300, "Oxygen Bed"),
(400, "Isolation Bed"),
(500, "Others"),
]
),
),
]

Check failure on line 90 in care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py:62:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
13 changes: 11 additions & 2 deletions care/facility/models/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
(70, "KASP Ventilator beds"),
]


class RoomType(models.IntegerChoices):
ICU_BED = 100, "ICU Bed"
GENERAL_BED = 200, "Ordinary Bed"
OXYGEN_BED = 300, "Oxygen Bed"
ISOLATION_BED = 400, "Isolation Bed"
OTHER = 500, "Others"


# to be removed in further PR
FEATURE_CHOICES = [
(1, "CT Scan Facility"),
Expand Down Expand Up @@ -81,15 +90,15 @@
(5, "Hotel"),
(6, "Lodge"),
(7, "TeleMedicine"),
# (8, "Govt Hospital"), # Change from "Govt Hospital" to "Govt Medical College Hospitals"

Check failure on line 93 in care/facility/models/facility.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ERA001)

care/facility/models/facility.py:93:5: ERA001 Found commented-out code
(9, "Govt Labs"),
(10, "Private Labs"),
# Use 8xx for Govt owned hospitals and health centres
(800, "Primary Health Centres"),
# (801, "24x7 Public Health Centres"), # Change from "24x7 Public Health Centres" to "Primary Health Centres"

Check failure on line 98 in care/facility/models/facility.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ERA001)

care/facility/models/facility.py:98:5: ERA001 Found commented-out code
(802, "Family Health Centres"),
(803, "Community Health Centres"),
# (820, "Urban Primary Health Center"), # Change from "Urban Primary Health Center" to "Primary Health Centres"

Check failure on line 101 in care/facility/models/facility.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ERA001)

care/facility/models/facility.py:101:5: ERA001 Found commented-out code
(830, "Taluk Hospitals"),
# (831, "Taluk Headquarters Hospitals"), # Change from "Taluk Headquarters Hospitals" to "Taluk Hospitals"
(840, "Women and Child Health Centres"),
Expand Down Expand Up @@ -426,7 +435,7 @@
facility = models.ForeignKey(
"Facility", on_delete=models.CASCADE, null=False, blank=False
)
room_type = models.IntegerField(choices=ROOM_TYPES)
room_type = models.IntegerField(choices=RoomType.choices)
total_capacity = models.IntegerField(default=0, validators=[MinValueValidator(0)])
current_capacity = models.IntegerField(default=0, validators=[MinValueValidator(0)])

Expand Down Expand Up @@ -462,7 +471,7 @@
return (
str(self.facility)
+ " "
+ REVERSE_ROOM_TYPES[self.room_type]
+ RoomType(self.room_type).label
+ " "
+ str(self.total_capacity)
)
Expand Down
6 changes: 3 additions & 3 deletions data/dummy/facility.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@
"modified_date": "2022-09-27T07:00:19.399Z",
"deleted": false,
"facility": 1,
"room_type": 150,
"room_type": 300,
"total_capacity": 1000,
"current_capacity": 20
}
Expand All @@ -806,7 +806,7 @@
"modified_date": "2022-09-27T07:16:52.525Z",
"deleted": false,
"facility": 2,
"room_type": 150,
"room_type": 300,
"total_capacity": 20,
"current_capacity": 1
}
Expand All @@ -820,7 +820,7 @@
"modified_date": "2023-09-15T06:12:31.548Z",
"deleted": false,
"facility": 4,
"room_type": 150,
"room_type": 300,
"total_capacity": 12,
"current_capacity": 2
}
Expand Down
Loading