-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add field readmission + custom migration (#1572)
* add field readmission + custom migration * rebase migration * fix rebase migration
- Loading branch information
1 parent
cbc0603
commit fe28fc1
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
48 changes: 48 additions & 0 deletions
48
care/facility/migrations/0385_patientconsultation_is_readmission.py
This file contains 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,48 @@ | ||
# Generated by Django 4.2.2 on 2023-09-01 16:22 | ||
|
||
from datetime import timedelta | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0384_patientconsultation_verified_by"), | ||
] | ||
|
||
def fill_is_readmission(apps, schema_editor): | ||
PatientConsultation = apps.get_model("facility", "PatientConsultation") | ||
|
||
bulk_update_list = [] | ||
qs = PatientConsultation.objects.filter( | ||
suggestion="A", admission_date__isnull=False | ||
) | ||
|
||
def is_readmission(consultation): | ||
# readmission=True if last_consultation.discharge_date is within 30 days of admission_date | ||
return qs.filter( | ||
patient=consultation.patient, | ||
created_date__lt=consultation.created_date, | ||
discharge_date__isnull=False, | ||
discharge_date__gte=consultation.admission_date - timedelta(days=30), | ||
).exists() | ||
|
||
for consultation in qs: | ||
if is_readmission(consultation): | ||
consultation.is_readmission = True | ||
bulk_update_list.append(consultation) | ||
|
||
PatientConsultation.objects.bulk_update( | ||
bulk_update_list, ["is_readmission"], batch_size=1000 | ||
) | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="patientconsultation", | ||
name="is_readmission", | ||
field=models.BooleanField(default=False), | ||
), | ||
migrations.RunPython( | ||
fill_is_readmission, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains 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