From 53f18cb0e2a19c9bd03fc59d3a8ba927c0fc2804 Mon Sep 17 00:00:00 2001 From: Uday Sagar Date: Thu, 24 Oct 2024 21:01:22 +0000 Subject: [PATCH] adds parent_note field to PatientNotes model --- care/facility/api/serializers/patient.py | 17 ++++++----------- .../0467_patientnotes_parent_note.py | 19 +++++++++++++++++++ care/facility/models/patient.py | 6 ++++++ 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 care/facility/migrations/0467_patientnotes_parent_note.py diff --git a/care/facility/api/serializers/patient.py b/care/facility/api/serializers/patient.py index 679ad986bf..bc7467f68e 100644 --- a/care/facility/api/serializers/patient.py +++ b/care/facility/api/serializers/patient.py @@ -501,19 +501,11 @@ class PatientNotesSerializer(serializers.ModelSerializer): reply_to_object = ReplyToPatientNoteSerializer(source="reply_to", read_only=True) files = serializers.SerializerMethodField() replies = ReplyToPatientNoteSerializer(many=True, read_only=True) - parent_note_object = serializers.SerializerMethodField() + parent_note_object = ReplyToPatientNoteSerializer( + source="parent_note", read_only=True + ) mentioned_users = UserBaseMinimumSerializer(many=True, read_only=True) - def get_parent_note_object(self, obj): - parent_note = obj - while parent_note.reply_to is not None: - parent_note = parent_note.reply_to - return ( - ReplyToPatientNoteSerializer(parent_note).data - if parent_note != obj - else None - ) - def get_files(self, obj): from care.facility.api.serializers.file_upload import FileUploadListSerializer @@ -560,6 +552,9 @@ def create(self, validated_data): msg = "Reply to note should be in the same consultation" raise serializers.ValidationError(msg) + # Set the parent_note to the parent of the reply_to_note if it exists else set it to the reply_to_note + validated_data["parent_note"] = reply_to_note.parent_note or reply_to_note + user = self.context["request"].user note = validated_data.get("note") with transaction.atomic(): diff --git a/care/facility/migrations/0467_patientnotes_parent_note.py b/care/facility/migrations/0467_patientnotes_parent_note.py new file mode 100644 index 0000000000..38ddbdbf3d --- /dev/null +++ b/care/facility/migrations/0467_patientnotes_parent_note.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1.1 on 2024-10-24 20:56 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('facility', '0466_alter_fileupload_file_type_alter_notification_event'), + ] + + operations = [ + migrations.AddField( + model_name='patientnotes', + name='parent_note', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='facility.patientnotes'), + ), + ] diff --git a/care/facility/models/patient.py b/care/facility/models/patient.py index fda9901a7d..bd22175736 100644 --- a/care/facility/models/patient.py +++ b/care/facility/models/patient.py @@ -807,6 +807,12 @@ class PatientNotes(FacilityBaseModel, ConsultationRelatedPermissionMixin): related_name="replies", ) note = models.TextField(default="", blank=True) + parent_note = models.ForeignKey( + "self", + on_delete=models.SET_NULL, + null=True, + blank=True, + ) def get_related_consultation(self): # This is a temporary hack! this model does not have `assigned_to` field