Skip to content

Commit

Permalink
added created_by_local_user field
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhavik-ag committed Jul 17, 2023
1 parent 097380b commit fb8424f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 8 additions & 1 deletion care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def save(self, **kwargs):
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)

def validate_empty_values(self, data):
if not data.get("note", "").strip():
Expand All @@ -463,5 +464,11 @@ def validate_empty_values(self, data):

class Meta:
model = PatientNotes
fields = ("note", "facility", "created_by_object", "created_date")
fields = (
"note",
"facility",
"created_by_object",
"created_by_local_user",
"created_date",
)
read_only_fields = ("created_date",)
13 changes: 12 additions & 1 deletion care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.contrib.postgres.search import TrigramSimilarity
from django.db import models
from django.db.models import Case, When
from django.db.models import BooleanField, Case, F, Value, When
from django.db.models.query_utils import Q
from django_filters import rest_framework as filters
from djqscsv import render_to_csv_response
Expand Down Expand Up @@ -595,6 +595,16 @@ class PatientNotesViewSet(
queryset = (
PatientNotes.objects.all()
.select_related("facility", "patient", "created_by")
.annotate(
created_by_local_user=Case(
When(
created_by__home_facility__external_id=F("facility__external_id"),
then=Value(True),
),
default=Value(False),
output_field=BooleanField(),
)
)
.order_by("-created_date")
)
serializer_class = PatientNotesSerializer
Expand All @@ -617,6 +627,7 @@ def get_queryset(self):
q_filters |= Q(patient__last_consultation__assigned_to=user)
q_filters |= Q(patient__assigned_to=user)
queryset = queryset.filter(q_filters)

return queryset

def perform_create(self, serializer):
Expand Down
2 changes: 0 additions & 2 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def validate(self, attrs):

class UserBaseMinimumSerializer(serializers.ModelSerializer):
user_type = ChoiceField(choices=User.TYPE_CHOICES, read_only=True)
home_facility = ExternalIdSerializerField(queryset=Facility.objects.all())

class Meta:
model = User
Expand All @@ -345,7 +344,6 @@ class Meta:
"last_name",
"user_type",
"last_login",
"home_facility",
)


Expand Down

0 comments on commit fb8424f

Please sign in to comment.