Skip to content

Commit

Permalink
add taken at field for events
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Jul 26, 2024
1 parent dc62e3f commit 81c1d58
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions care/facility/api/serializers/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def create(self, validated_data):
daily_round_obj,
daily_round_obj.created_by_id,
daily_round_obj.created_date,
taken_at=daily_round_obj.taken_at,
)
return daily_round_obj

Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def update(self, instance, validated_data):
consultation,
self.context["request"].user.id,
consultation.modified_date,
old_instance,
old=old_instance,
)

if "assigned_to" in validated_data:
Expand Down Expand Up @@ -819,7 +819,7 @@ def update(self, instance: PatientConsultation, validated_data):
instance,
self.context["request"].user.id,
instance.modified_date,
old_instance,
old=old_instance,
)

return instance
Expand Down
11 changes: 7 additions & 4 deletions care/facility/api/viewsets/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def roots(self, request):
class PatientConsultationEventFilterSet(filters.FilterSet):
class Meta:
model = PatientConsultationEvent
fields = {
"event_type": ["exact"],
}
fields = [
"event_type",
"caused_by",
"is_latest",
]


class PatientConsultationEventViewSet(ReadOnlyModelViewSet):
Expand All @@ -60,8 +62,9 @@ class PatientConsultationEventViewSet(ReadOnlyModelViewSet):
"event_type", "caused_by"
)
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter)
filterset_class = PatientConsultationEventFilterSet
ordering_fields = ("created_date", "taken_at")
# lookup_field = "external_id"
# lookup_url_kwarg = "external_id"

Expand Down
10 changes: 9 additions & 1 deletion care/facility/events/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create_consultation_event_entry(
object_instance: Model,
caused_by: int,
created_date: datetime,
taken_at: datetime,
old_instance: Model | None = None,
fields_to_store: set[str] | None = None,
):
Expand Down Expand Up @@ -49,7 +50,7 @@ def create_consultation_event_entry(
is_latest=True,
object_model=object_instance.__class__.__name__,
object_id=object_instance.id,
created_date__lt=created_date,
taken_at__lt=taken_at,
).update(is_latest=False)
batch.append(
PatientConsultationEvent(
Expand All @@ -58,6 +59,7 @@ def create_consultation_event_entry(
event_type_id=group_id,
is_latest=True,
created_date=created_date,
taken_at=taken_at,
object_model=object_instance.__class__.__name__,
object_id=object_instance.id,
value=value,
Expand All @@ -78,12 +80,16 @@ def create_consultation_events(
objects: list | QuerySet | Model,
caused_by: int,
created_date: datetime = None,
taken_at: datetime = None,
old: Model | None = None,
fields_to_store: list[str] | set[str] | None = None,
):
if created_date is None:
created_date = now()

if taken_at is None:
taken_at = created_date

with transaction.atomic():
if isinstance(objects, (QuerySet, list, tuple)):
if old is not None:
Expand All @@ -96,6 +102,7 @@ def create_consultation_events(
obj,
caused_by,
created_date,
taken_at,
fields_to_store=set(fields_to_store) if fields_to_store else None,
)
else:
Expand All @@ -104,6 +111,7 @@ def create_consultation_events(
objects,
caused_by,
created_date,
taken_at,
old,
fields_to_store=set(fields_to_store) if fields_to_store else None,
)
30 changes: 30 additions & 0 deletions care/facility/migrations/0447_patientconsultationevent_taken_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.10 on 2024-07-02 10:44

from django.db import migrations, models


def backfill_taken_at(apps, schema_editor):
PatientConsultationEvent = apps.get_model("facility", "PatientConsultationEvent")
PatientConsultationEvent.objects.filter(taken_at__isnull=True).update(
taken_at=models.F("created_date")
)


class Migration(migrations.Migration):
dependencies = [
("facility", "0446_alter_notification_event"),
]

operations = [
migrations.AddField(
model_name="patientconsultationevent",
name="taken_at",
field=models.DateTimeField(db_index=True, null=True),
),
migrations.RunPython(backfill_taken_at, reverse_code=migrations.RunPython.noop),
migrations.AlterField(
model_name="patientconsultationevent",
name="taken_at",
field=models.DateTimeField(db_index=True),
),
]
1 change: 1 addition & 0 deletions care/facility/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PatientConsultationEvent(models.Model):
)
caused_by = models.ForeignKey(User, on_delete=models.PROTECT)
created_date = models.DateTimeField(db_index=True)
taken_at = models.DateTimeField(db_index=True)
object_model = models.CharField(
max_length=50, db_index=True, null=False, blank=False
)
Expand Down

0 comments on commit 81c1d58

Please sign in to comment.