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

feat(patient notes): add edit window validation and update endpoint #1221

Merged
merged 45 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e378637
feat(patient notes): add edit window validation and update endpoint
Ashesh3 Mar 18, 2023
0e5ef10
Merge branch 'master' into doc-note-edit
Ashesh3 Apr 18, 2023
b68cdb4
Use editable_until logic
Ashesh3 Apr 18, 2023
d6a3574
dummy empty commit
Ashesh3 Apr 18, 2023
aa780a5
Merge branch 'master' into doc-note-edit
Ashesh3 Jul 19, 2023
bcf1ffa
Merge branch 'master' into doc-note-edit
Ashesh3 Jul 19, 2023
18dc937
Merge migrations
Ashesh3 Jul 19, 2023
11b91df
format files
Ashesh3 Jul 19, 2023
3d85cc4
Merge migrations
Ashesh3 Jul 19, 2023
85531fe
fix tests
Ashesh3 Jul 19, 2023
aec33b5
remove editable_until
Ashesh3 Jul 21, 2023
38b50b1
Merge branch 'master' into doc-note-edit
Ashesh3 Jul 21, 2023
551d487
bug fixes
Ashesh3 Jul 21, 2023
548b11e
Move update validation to viewset
Ashesh3 Jul 21, 2023
ecfef45
Merge branch 'master' into doc-note-edit
Ashesh3 Jul 21, 2023
c415185
Fix tests
Ashesh3 Jul 21, 2023
4dd8d12
Remove PATIENT_NOTE_EDIT_WINDOW
Ashesh3 Aug 8, 2023
8e61d90
Merge branch 'master' into doc-note-edit
Ashesh3 Aug 8, 2023
2e90292
fix lint
Ashesh3 Aug 8, 2023
44d32f0
Merge branch 'master' into doc-note-edit
Ashesh3 Aug 28, 2023
067d147
edit history for patient notes
Ashesh3 Aug 28, 2023
4eaab8b
Fix tests
Ashesh3 Aug 29, 2023
3587a87
Create initial edit record for existing notes
Ashesh3 Aug 29, 2023
dc899d0
optimize migration
sainak Aug 29, 2023
7e44d7a
Update care/facility/models/mixins/permissions/patient.py
Ashesh3 Aug 29, 2023
5adcd17
Merge branch 'master' into doc-note-edit
Ashesh3 Dec 20, 2023
f291055
Update migrations
Ashesh3 Dec 20, 2023
c42e923
Merge branch 'master' into doc-note-edit
Ashesh3 Dec 27, 2023
0117da4
update migrations
Ashesh3 Dec 27, 2023
2d9bf40
Update care/facility/api/serializers/patient.py
Ashesh3 Dec 27, 2023
d8050c2
Update care/facility/models/patient.py
Ashesh3 Dec 27, 2023
1e50d34
Update care/facility/migrations/0405_patientnotesedit.py
sainak Dec 27, 2023
c7d09ea
Update care/facility/migrations/0405_patientnotesedit.py
sainak Dec 27, 2023
c61d3e9
edited_date
Ashesh3 Dec 27, 2023
ef69e7e
Update care/facility/migrations/0405_patientnotesedit.py
sainak Dec 27, 2023
559d50b
Merge branch 'doc-note-edit' of https://github.com/ashesh3/care into …
Ashesh3 Dec 27, 2023
a54e2c3
Update care/facility/models/patient.py
sainak Dec 27, 2023
eaf8f68
Apply suggestions from code review
sainak Dec 27, 2023
26cc98d
Merge branch 'master' into doc-note-edit
Ashesh3 Jan 2, 2024
73750d7
Merge branch 'master' into doc-note-edit
Ashesh3 Jan 9, 2024
d3bcb79
Merge branch 'master' into doc-note-edit
Ashesh3 Jan 17, 2024
59a7b0e
Fix N+1, update migrations, seperate endpoint for edits
Ashesh3 Jan 18, 2024
6f9039c
Merge branch 'master' into doc-note-edit
Ashesh3 Jan 22, 2024
716368e
Update migrations
Ashesh3 Jan 22, 2024
3ecf5b6
Fix tests
Ashesh3 Jan 23, 2024
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
14 changes: 12 additions & 2 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
PatientRegistration,
)
from care.facility.models.notification import Notification
from care.facility.models.patient import PATIENT_NOTE_EDIT_WINDOW
from care.facility.models.patient_base import (
BLOOD_GROUP_CHOICES,
DISEASE_STATUS_CHOICES,
Expand Down Expand Up @@ -456,6 +457,10 @@ class PatientNotesSerializer(serializers.ModelSerializer):
facility = FacilityBasicInfoSerializer(read_only=True)
created_by_object = UserBaseMinimumSerializer(source="created_by", read_only=True)
created_by_local_user = serializers.BooleanField(read_only=True)
edit_window_seconds = serializers.SerializerMethodField(read_only=True)

def get_edit_window_seconds(self, obj):
return PATIENT_NOTE_EDIT_WINDOW
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved

def validate_empty_values(self, data):
if not data.get("note", "").strip():
Expand All @@ -465,10 +470,15 @@ def validate_empty_values(self, data):
class Meta:
model = PatientNotes
fields = (
"id",
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
"note",
"facility",
"created_by_object",
"created_by_local_user",
"created_date",
"edit_window_seconds",
sainak marked this conversation as resolved.
Show resolved Hide resolved
)
read_only_fields = (
"id",
"created_by_local_user",
"edit_window_seconds",
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
)
read_only_fields = ("created_date",)
15 changes: 12 additions & 3 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from rest_framework.exceptions import ValidationError
from rest_framework.filters import BaseFilterBackend
from rest_framework.generics import get_object_or_404
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin
from rest_framework.mixins import (
CreateModelMixin,
ListModelMixin,
RetrieveModelMixin,
UpdateModelMixin,
)
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -590,7 +595,11 @@ def list(self, request, *args, **kwargs):


class PatientNotesViewSet(
ListModelMixin, RetrieveModelMixin, CreateModelMixin, GenericViewSet
ListModelMixin,
RetrieveModelMixin,
CreateModelMixin,
UpdateModelMixin,
GenericViewSet,
):
queryset = (
PatientNotes.objects.all()
Expand Down Expand Up @@ -638,7 +647,7 @@ def perform_create(self, serializer):
)
if not patient.is_active:
raise ValidationError(
{"patient": "Only active patients data can be updated"}
{"patient": "Updating patient data is only allowed for active patients"}
)
return serializer.save(
facility=patient.facility,
Expand Down
9 changes: 5 additions & 4 deletions care/facility/models/mixins/permissions/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ def has_object_read_permission(self, request):
and request.user in self.patient.facility.users.all()
)
or (
self.assigned_to == request.user
or request.user == self.patient.assigned_to
getattr(self, "assigned_to", None)
and getattr(self, "assigned_to", None) == request.user
)
or request.user == getattr(self.patient, "assigned_to", None)
or (
request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]
and (
Expand Down Expand Up @@ -206,8 +207,8 @@ def has_object_update_permission(self, request):
and self.patient.facility == request.user.home_facility
)
or (
self.assigned_to == request.user
or request.user == self.patient.assigned_to
getattr(self, "assigned_to", None)
and getattr(self, "assigned_to", None) == request.user
)
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
or (
request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]
Expand Down
19 changes: 19 additions & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import datetime
import enum

from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import JSONField
from django.utils.timezone import now
from simple_history.models import HistoricalRecords

from care.facility.models import (
Expand Down Expand Up @@ -41,6 +43,8 @@
)
from care.utils.models.base import BaseManager, BaseModel

PATIENT_NOTE_EDIT_WINDOW = 30 * 60 # 30 minutes


class PatientRegistration(PatientBaseModel, PatientPermissionMixin):
# fields in the PatientSearch model
Expand Down Expand Up @@ -683,3 +687,18 @@ class PatientNotes(FacilityBaseModel, PatientRelatedPermissionMixin):
null=True,
)
note = models.TextField(default="", blank=True)

def save(self, *args, **kwargs):
if self.pk: # Updating an existing note
if not self.patient.is_active:
raise ValidationError(
{
"patient": "Updating patient data is only allowed for active patients"
}
)
if now() > self.created_date + datetime.timedelta(
seconds=PATIENT_NOTE_EDIT_WINDOW
):
raise ValidationError({"note": "Note is not editable anymore"})
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved

super().save(*args, **kwargs)
2 changes: 2 additions & 0 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@


class ExpectedPatientNoteKeys(Enum):
ID = "id"
NOTE = "note"
FACILITY = "facility"
CREATED_BY_OBJECT = "created_by_object"
CREATED_DATE = "created_date"
MODIFIED_DATE = "modified_date"
CREATED_BY_LOCAL_USER = "created_by_local_user"


Expand Down
2 changes: 1 addition & 1 deletion care/utils/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def create_consultation(
return PatientConsultation.objects.create(**data)

def create_patient_note(
self, patient=None, facility=None, note="Patient is doing find", **kwargs
self, patient=None, facility=None, note="Patient is doing fine", **kwargs
):
data = {
"patient": patient or self.patient,
Expand Down
Loading