-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use signals to update patient and bed count of facilities
- Loading branch information
Showing
7 changed files
with
122 additions
and
19 deletions.
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
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
47 changes: 47 additions & 0 deletions
47
care/facility/migrations/0361_set_patient_and_bed_count.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,47 @@ | ||
# Generated by Django 2.2.11 on 2023-06-08 16:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def set_patient_and_bed_count(apps, schema_editor): | ||
Facility = apps.get_model("facility", "Facility") | ||
|
||
facilities = Facility.objects.filter(deleted=False, is_active=True).annotate( | ||
annotated_patient_count=models.Count( | ||
"patientregistration", | ||
filter=models.Q( | ||
patientregistration__is_active=True, patientregistration__deleted=False | ||
), | ||
), | ||
annotated_bed_count=models.Count("bed", filter=models.Q(bed__deleted=False)), | ||
) | ||
|
||
updated_facilities = [] | ||
for facility in facilities: | ||
facility.patient_count = facility.annotated_patient_count | ||
facility.bed_count = facility.annotated_bed_count | ||
updated_facilities.append(facility) | ||
|
||
Facility.objects.bulk_update(updated_facilities, ["patient_count", "bed_count"]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0360_auto_20230608_1750"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="facility", | ||
name="bed_count", | ||
field=models.IntegerField(default=0), | ||
), | ||
migrations.AddField( | ||
model_name="facility", | ||
name="patient_count", | ||
field=models.IntegerField(default=0), | ||
), | ||
migrations.RunPython( | ||
set_patient_and_bed_count, 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
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 @@ | ||
from .facility_related_count import * # noqa |
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,59 @@ | ||
from django.db.models import Q | ||
from django.db.models.signals import post_delete, post_save | ||
from django.dispatch import receiver | ||
|
||
from care.facility.models.bed import Bed | ||
from care.facility.models.facility import Facility | ||
from care.facility.models.patient import PatientRegistration | ||
from care.utils.models.aggregators import update_related_count_for_single_object | ||
|
||
|
||
def update_patient_count(facility: Facility): | ||
update_related_count_for_single_object( | ||
facility, | ||
{ | ||
"patient_count": ( | ||
"patientregistration_set", | ||
Q( | ||
is_active=True, | ||
deleted=False, | ||
), | ||
) | ||
}, | ||
) | ||
|
||
|
||
def update_bed_count(facility: Facility): | ||
update_related_count_for_single_object( | ||
facility, | ||
{ | ||
"bed_count": ("bed_set", Q(is_active=True, deleted=False)), | ||
}, | ||
) | ||
|
||
|
||
@receiver(post_save, sender=PatientRegistration) | ||
def patient_post_save(sender, instance, created, raw, using, update_fields, **kwargs): | ||
if raw: | ||
return | ||
if ( | ||
created or (update_fields is not None and "is_active" in update_fields) | ||
) and instance.facility is not None: | ||
update_patient_count(instance.facility) | ||
|
||
|
||
@receiver(post_delete, sender=PatientRegistration) | ||
def patient_post_delete(sender, instance, **kwargs): | ||
if instance.facility is not None: | ||
update_patient_count(instance.facility) | ||
|
||
|
||
@receiver(post_save, sender=Bed) | ||
def bed_post_save(sender, instance, created, raw, using, update_fields, **kwargs): | ||
if created: | ||
update_bed_count(instance.facility) | ||
|
||
|
||
@receiver(post_delete, sender=Bed) | ||
def bed_post_delete(sender, instance, **kwargs): | ||
update_bed_count(instance.facility) |
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