diff --git a/iaso/api/enketo.py b/iaso/api/enketo.py index ec959089ff..06a7810f13 100644 --- a/iaso/api/enketo.py +++ b/iaso/api/enketo.py @@ -364,6 +364,7 @@ def post(self, request, format=None): instance.file = main_file instance.name = instance.form.name instance.json = {} + instance.source_updated_at = timezone.now() instance.save() # copy-pasted from the "create" code diff --git a/iaso/api/instances.py b/iaso/api/instances.py index c78a7ae626..e1ffdc612f 100644 --- a/iaso/api/instances.py +++ b/iaso/api/instances.py @@ -235,16 +235,8 @@ def list_file_export(filters: Dict[str, Any], queryset: "QuerySet[Instance]", fi filename = "%s-%s" % (filename, strftime("%Y-%m-%d-%H-%M", gmtime())) def get_row(instance, **kwargs): - created_at_timestamp = ( - instance.source_created_at.timestamp() - if instance.source_created_at - else instance.created_at.timestamp() - ) - updated_at_timestamp = ( - instance.source_updated_at.timestamp() - if instance.source_updated_at - else instance.updated_at.timestamp() - ) + created_at_timestamp = instance.source_created_at_with_fallback.timestamp() + updated_at_timestamp = instance.source_updated_at_with_fallback.timestamp() org_unit = instance.org_unit file_content = instance.get_and_save_json_of_xml() diff --git a/iaso/api/mobile/entity.py b/iaso/api/mobile/entity.py index 0c76afe65d..07404a793c 100644 --- a/iaso/api/mobile/entity.py +++ b/iaso/api/mobile/entity.py @@ -74,9 +74,8 @@ class Meta: id = serializers.CharField(read_only=True, source="uuid") org_unit_id = serializers.CharField(read_only=True, source="org_unit.id") form_version_id = serializers.SerializerMethodField() - - created_at = TimestampField() - updated_at = TimestampField() + created_at = TimestampField(read_only=True, source="source_created_at_with_fallback") + updated_at = TimestampField(read_only=True, source="source_updated_at_with_fallback") def get_form_version_id(self, obj): if obj.json is None: diff --git a/iaso/api/mobile/org_units.py b/iaso/api/mobile/org_units.py index dae90256b3..06aa486b37 100644 --- a/iaso/api/mobile/org_units.py +++ b/iaso/api/mobile/org_units.py @@ -43,8 +43,8 @@ class Meta: class ReferenceInstancesSerializer(serializers.ModelSerializer): - created_at = TimestampField() - updated_at = TimestampField() + created_at = TimestampField(read_only=True, source="source_created_at_with_fallback") + updated_at = TimestampField(read_only=True, source="source_updated_at_with_fallback") class Meta: model = Instance diff --git a/iaso/dhis2/datavalue_exporter.py b/iaso/dhis2/datavalue_exporter.py index 8292336034..60e831da3c 100644 --- a/iaso/dhis2/datavalue_exporter.py +++ b/iaso/dhis2/datavalue_exporter.py @@ -32,8 +32,7 @@ def get_event_date(instance, form_mapping): event_date_source = MappingVersion.get_event_date_source(form_mapping) if event_date_source == MappingVersion.EVENT_DATE_SOURCE_FROM_SUBMISSION_CREATED_AT: - created_at = instance.source_created_at if instance.source_created_at else instance.created_at - return created_at.strftime("%Y-%m-%d") + return instance.source_created_at_with_fallback.strftime("%Y-%m-%d") if event_date_source == MappingVersion.EVENT_DATE_SOURCE_FROM_SUBMISSION_PERIOD: dhis2_period = Period.from_string(instance.period) start = dhis2_period.start_date().strftime("%Y-%m-%d") diff --git a/iaso/models/base.py b/iaso/models/base.py index 6d96833b80..a4f8e923e1 100644 --- a/iaso/models/base.py +++ b/iaso/models/base.py @@ -989,6 +989,14 @@ def is_reference_instance(self) -> bool: return False return self.org_unit.reference_instances.filter(orgunitreferenceinstance__instance=self).exists() + @property + def source_created_at_with_fallback(self): + return self.source_created_at if self.source_created_at else self.created_at + + @property + def source_updated_at_with_fallback(self): + return self.source_updated_at if self.source_updated_at else self.updated_at + def flag_reference_instance(self, org_unit: "OrgUnit") -> "OrgUnitReferenceInstance": if not self.form: raise ValidationError(_("The Instance must be linked to a Form.")) @@ -1150,8 +1158,8 @@ def as_dict(self): "id": self.id, "form_id": self.form_id, "form_name": self.form.name if self.form else None, - "created_at": self.source_created_at.timestamp() if self.source_created_at else self.created_at.timestamp(), - "updated_at": self.source_updated_at.timestamp() if self.source_updated_at else self.updated_at.timestamp(), + "created_at": self.source_created_at_with_fallback.timestamp(), + "updated_at": self.source_updated_at_with_fallback.timestamp(), "org_unit": self.org_unit.as_dict() if self.org_unit else None, "latitude": self.location.y if self.location else None, "longitude": self.location.x if self.location else None, @@ -1188,8 +1196,8 @@ def as_dict_with_parents(self): "file_url": self.file.url if self.file else None, "id": self.id, "form_id": self.form_id, - "created_at": self.source_created_at.timestamp() if self.source_created_at else self.created_at.timestamp(), - "updated_at": self.source_updated_at.timestamp() if self.source_updated_at else self.updated_at.timestamp(), + "created_at": self.source_created_at_with_fallback.timestamp(), + "updated_at": self.source_updated_at_with_fallback.timestamp(), "created_by": get_creator_name(self.created_by) if self.created_by else None, "org_unit": self.org_unit.as_dict_with_parents() if self.org_unit else None, "latitude": self.location.y if self.location else None, @@ -1221,8 +1229,8 @@ def as_full_model(self, with_entity=False): "form_version_id": self.form_version.id if self.form_version else None, "form_name": self.form.name, "form_descriptor": form_version.get_or_save_form_descriptor() if form_version is not None else None, - "created_at": self.source_created_at.timestamp() if self.source_created_at else self.created_at.timestamp(), - "updated_at": self.source_updated_at.timestamp() if self.source_updated_at else self.updated_at.timestamp(), + "created_at": self.source_created_at_with_fallback.timestamp(), + "updated_at": self.source_updated_at_with_fallback.timestamp(), "org_unit": self.org_unit.as_dict_with_parents(light=False, light_parents=False) if self.org_unit else None, "latitude": self.location.y if self.location else None, "longitude": self.location.x if self.location else None, @@ -1281,8 +1289,8 @@ def as_small_dict(self): return { "id": self.id, "file_url": self.file.url if self.file else None, - "created_at": self.source_created_at.timestamp() if self.source_created_at else self.created_at.timestamp(), - "updated_at": self.source_updated_at.timestamp() if self.source_updated_at else self.updated_at.timestamp(), + "created_at": self.source_created_at_with_fallback.timestamp(), + "updated_at": self.source_updated_at_with_fallback.timestamp(), "period": self.period, "latitude": self.location.y if self.location else None, "longitude": self.location.x if self.location else None,