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

[WC2-478] Using source_created_at and source_updated_at instead of their server… #1581

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions iaso/api/enketo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions iaso/api/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions iaso/api/mobile/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions iaso/api/mobile/org_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions iaso/dhis2/datavalue_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 16 additions & 8 deletions iaso/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading