From 3501703faaa43840e876a4072a4a9db3b014e559 Mon Sep 17 00:00:00 2001 From: Cal Ellowitz Date: Sun, 15 Dec 2024 14:36:03 -0500 Subject: [PATCH] add duration to visit model and export --- commcare_connect/opportunity/models.py | 13 +++++++++++++ commcare_connect/opportunity/tables.py | 1 + 2 files changed, 14 insertions(+) diff --git a/commcare_connect/opportunity/models.py b/commcare_connect/opportunity/models.py index 6214d3fc..5d8722e9 100644 --- a/commcare_connect/opportunity/models.py +++ b/commcare_connect/opportunity/models.py @@ -5,6 +5,7 @@ from django.conf import settings from django.db import models from django.db.models import Count, F, Max, Q, Sum +from django.utils.dateparse import parse_datetime from django.utils.functional import cached_property from django.utils.timezone import now from django.utils.translation import gettext @@ -586,6 +587,18 @@ def __setattr__(self, name, value): def images(self): return BlobMeta.objects.filter(parent_id=self.xform_id, content_type__startswith="image/") + @property + def duration(self): + duration = None + start = self.form_json["metadata"].get("timeStart") + end = self.form_json["metatdata"].get("timeEnd") + if start and end: + try: + duration = parse_datetime(end) - parse_datetime(start) + except (TypeError, ValueError): + pass + return duration + class OpportunityClaim(models.Model): opportunity_access = models.OneToOneField(OpportunityAccess, on_delete=models.CASCADE) diff --git a/commcare_connect/opportunity/tables.py b/commcare_connect/opportunity/tables.py index c4f19f2e..e4e8c46e 100644 --- a/commcare_connect/opportunity/tables.py +++ b/commcare_connect/opportunity/tables.py @@ -62,6 +62,7 @@ class UserVisitTable(OrgContextTable): ) reason = columns.Column("Rejected Reason", accessor="reason", visible=False) justification = columns.Column("Justification", accessor="justification", visible=False) + duration = columns.Column("Duration", accessor="duration", visible=False) deliver_unit = columns.Column("Unit Name", accessor="deliver_unit__name") entity_id = columns.Column("Entity ID", accessor="entity_id", visible=False)