Skip to content

Commit

Permalink
events module: serialize only specific fields of related objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Jul 25, 2024
1 parent 32bf01b commit 7f50553
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 44 deletions.
46 changes: 15 additions & 31 deletions care/facility/events/handler.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
from contextlib import suppress
from datetime import datetime

from django.core.exceptions import FieldDoesNotExist
from django.db import transaction
from django.db.models import Field, Model
from django.db.models import Model
from django.db.models.query import QuerySet
from django.utils.timezone import now

from care.facility.models.events import ChangeType, EventType, PatientConsultationEvent
from care.utils.event_utils import get_changed_fields, serialize_field


def transform(
object_instance: Model,
old_instance: Model,
fields_to_store: set[str] | None = None,
) -> dict[str, any]:
fields: set[Field] = set()
if old_instance:
changed_fields = get_changed_fields(old_instance, object_instance)
fields = {
field
for field in object_instance._meta.fields
if field.name in changed_fields
}
else:
fields = set(object_instance._meta.fields)

if fields_to_store:
fields = {field for field in fields if field.name in fields_to_store}

return {field.name: serialize_field(object_instance, field) for field in fields}


def create_consultation_event_entry(
consultation_id: int,
object_instance: Model,
Expand All @@ -41,24 +21,28 @@ def create_consultation_event_entry(
):
change_type = ChangeType.UPDATED if old_instance else ChangeType.CREATED

data = transform(object_instance, old_instance, fields_to_store)
fields_to_store = fields_to_store or set(data.keys())
fields: set[str] = (
get_changed_fields(old_instance, object_instance)
if old_instance
else {field.name for field in object_instance._meta.fields}
)

fields_to_store = fields_to_store & fields if fields_to_store else fields

batch = []
groups = EventType.objects.filter(
model=object_instance.__class__.__name__, fields__len__gt=0, is_active=True
).values_list("id", "fields")
for group_id, group_fields in groups:
if set(group_fields) & fields_to_store:
if fields_to_store & {field.split("__", 1)[0] for field in group_fields}:
value = {}
for field in group_fields:
try:
value[field] = data[field]
except KeyError:
value[field] = getattr(object_instance, field, None)
# if all values in the group are Falsy, skip creating the event for this group
with suppress(FieldDoesNotExist):
value[field] = serialize_field(object_instance, field)

Check warning on line 41 in care/facility/events/handler.py

View check run for this annotation

Codecov / codecov/patch

care/facility/events/handler.py#L41

Added line #L41 was not covered by tests

if all(not v for v in value.values()):
continue

PatientConsultationEvent.objects.select_for_update().filter(
consultation_id=consultation_id,
event_type=group_id,
Expand Down
15 changes: 8 additions & 7 deletions care/facility/management/commands/load_event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ class Command(BaseCommand):
"name": "INVESTIGATION",
"fields": ("investigation",),
},
# disabling until we have a better way to serialize user objects
# {
# "name": "TREATING_PHYSICIAN",
# "fields": ("treating_physician",),
# },
{
"name": "TREATING_PHYSICIAN",
"fields": (
"treating_physician__username",
"treating_physician__full_name",
),
},
),
},
{
Expand Down Expand Up @@ -225,7 +227,7 @@ class Command(BaseCommand):
{
"name": "DIAGNOSIS",
"model": "ConsultationDiagnosis",
"fields": ("diagnosis", "verification_status", "is_principal"),
"fields": ("diagnosis__label", "verification_status", "is_principal"),
},
{
"name": "SYMPTOMS",
Expand All @@ -246,7 +248,6 @@ class Command(BaseCommand):
"VENTILATOR_MODES",
"SYMPTOMS",
"ROUND_SYMPTOMS",
"TREATING_PHYSICIAN",
)

def create_objects(
Expand Down
4 changes: 4 additions & 0 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ class User(AbstractUser):

CSV_MAKE_PRETTY = {"user_type": (lambda x: User.REVERSE_TYPE_MAP[x])}

@property
def full_name(self):
return self.get_full_name()

Check warning on line 316 in care/users/models.py

View check run for this annotation

Codecov / codecov/patch

care/users/models.py#L316

Added line #L316 was not covered by tests

@staticmethod
def has_read_permission(request):
return True
Expand Down
24 changes: 18 additions & 6 deletions care/utils/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from json import JSONEncoder
from logging import getLogger

from django.core.serializers import serialize
from django.core.exceptions import FieldDoesNotExist
from django.db.models import Field, Model
from multiselectfield.db.fields import MSFList, MultiSelectField

Expand All @@ -27,11 +27,23 @@ def get_changed_fields(old: Model, new: Model) -> set[str]:
return changed_fields


def serialize_field(object: Model, field: Field):
value = getattr(object, field.name)
if isinstance(value, Model):
# serialize the fields of the related model
return serialize("python", [value])[0]["fields"]
def serialize_field(object: Model, field_name: str):
if "__" in field_name:
field_name, sub_field = field_name.split("__", 1)
related_object = getattr(object, field_name)
return serialize_field(related_object, sub_field)

Check warning on line 34 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L32-L34

Added lines #L32 - L34 were not covered by tests

field = None
try:
field = object._meta.get_field(field_name)
except FieldDoesNotExist as e:
try:

Check warning on line 40 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L36-L40

Added lines #L36 - L40 were not covered by tests
# try to get property field
return getattr(object, field_name)
except AttributeError:
raise e

Check warning on line 44 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L42-L44

Added lines #L42 - L44 were not covered by tests

value = getattr(object, field.name, None)

Check warning on line 46 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L46

Added line #L46 was not covered by tests
if issubclass(field.__class__, Field) and field.choices:
# serialize choice fields with display value
return getattr(object, f"get_{field.name}_display", lambda: value)()
Expand Down
1 change: 1 addition & 0 deletions scripts/celery_beat-ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ done

python manage.py migrate --noinput
python manage.py load_redis_index
python manage.py load_event_types

touch /tmp/healthy

Expand Down
1 change: 1 addition & 0 deletions scripts/celery_beat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ done

python manage.py migrate --noinput
python manage.py load_redis_index
python manage.py load_event_types

touch /tmp/healthy

Expand Down

0 comments on commit 7f50553

Please sign in to comment.