Skip to content

Commit

Permalink
Merge pull request #2757 from onaio/fix-2752
Browse files Browse the repository at this point in the history
Add GeoJSON attachment params to EXPORT_OPTIONS
  • Loading branch information
FrankApiyo authored Jan 13, 2025
2 parents 908a810 + 99fcab4 commit 03436fa
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ecr-image-build-w-arm-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on: # yamllint disable-line rule:truthy
- "*-rc"
tags:
- "v*"
workflow_dispatch:

jobs:
build:
Expand Down
4 changes: 4 additions & 0 deletions onadata/apps/viewer/models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class ExportBaseModel(models.Model):
EXPORT_OPTION_FIELDS = [
"binary_select_multiples",
"dataview_pk",
"title",
"fields",
"geo_fields",
"simple_style",
"group_delimiter",
"include_images",
"include_labels",
Expand Down
103 changes: 100 additions & 3 deletions onadata/libs/tests/utils/test_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
generate_geojson_export,
generate_kml_export,
generate_osm_export,
get_query_params_from_metadata,
get_repeat_index_tags,
kml_export_data,
parse_request_export_options,
Expand Down Expand Up @@ -261,7 +262,7 @@ def test_should_create_export_when_submission_deleted(self):

self.assertTrue(will_create_new_export)

def test_should_not_create_new_export_when_old_exists(self):
def test_should_not_create_new_export_fn(self):
export_type = "csv"
self._publish_transportation_form_and_submit_instance()
options = {
Expand All @@ -277,6 +278,53 @@ def test_should_not_create_new_export_when_old_exists(self):

self.assertFalse(will_create_new_export)

def test_get_query_params_from_metadata_fn(self):
self._publish_transportation_form_and_submit_instance()
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(XForm),
data_type="media",
data_value=f"xform_geojson {self.xform.id} testgeojson2",
extra_data={
"data_title": "start",
"data_fields": "",
"data_geo_field": "qn09",
},
object_id=self.xform.id,
)
self.assertEqual(
{
"title": "start",
"fields": "",
"geo_field": "qn09",
},
get_query_params_from_metadata(metadata),
)

metadata.extra_data = {
"data_title": "start",
"data_fields": "one,two",
"data_geo_field": "qn09",
}
self.assertEqual(
{
"title": "start",
"fields": "one,two",
"geo_field": "qn09",
},
get_query_params_from_metadata(metadata),
)

metadata.extra_data = {
"data_title": "start",
"data_fields": "",
"data_geo_field": "qn09",
"data_simple_style": True,
}
self.assertEqual(
{"title": "start", "fields": "", "geo_field": "qn09", "simple_style": True},
get_query_params_from_metadata(metadata),
)

def test_should_not_create_new_export_when_old_exists(self):
export_type = "geojson"
self._publish_transportation_form_and_submit_instance()
Expand All @@ -296,7 +344,7 @@ def test_should_not_create_new_export_when_old_exists(self):
},
object_id=self.xform.id,
)
_response = custom_response_handler(
custom_response_handler(
request,
self.xform,
{},
Expand All @@ -310,6 +358,9 @@ def test_should_not_create_new_export_when_old_exists(self):
self.assertEqual(
{
"dataview_pk": False,
"title": "start",
"fields": "",
"simple_style": True,
"include_hxl": True,
"include_images": True,
"include_labels": False,
Expand All @@ -322,7 +373,7 @@ def test_should_not_create_new_export_when_old_exists(self):
},
Export.objects.get(xform=self.xform).options,
)
_response = custom_response_handler(
custom_response_handler(
request,
self.xform,
{},
Expand All @@ -336,6 +387,9 @@ def test_should_not_create_new_export_when_old_exists(self):
self.assertEqual(
{
"dataview_pk": False,
"title": "start",
"fields": "",
"simple_style": True,
"include_hxl": True,
"include_images": True,
"include_labels": False,
Expand All @@ -349,6 +403,49 @@ def test_should_not_create_new_export_when_old_exists(self):
Export.objects.get(xform=self.xform).options,
)

# New metadata will yield a new export
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(XForm),
data_type="media",
data_value=f"xform_geojson {self.xform.id} testgeojson2",
extra_data={
"data_title": "end",
"data_fields": "",
"data_geo_field": "qn09",
"data_simple_style": True,
},
object_id=self.xform.id,
)
custom_response_handler(
request,
self.xform,
{},
export_type,
filename="testgeojson2",
dataview=False,
metadata=metadata,
)
# we generated a new export since the extra_data has been updated
self.assertEqual(2, Export.objects.filter(xform=self.xform).count())
self.assertEqual(
{
"dataview_pk": False,
"title": "end",
"fields": "",
"simple_style": True,
"include_hxl": True,
"include_images": True,
"include_labels": False,
"win_excel_utf8": False,
"group_delimiter": "/",
"include_reviews": False,
"remove_group_name": False,
"include_labels_only": False,
"split_select_multiples": True,
},
Export.objects.filter(xform=self.xform).last().options,
)

def test_should_create_new_export_when_filter_defined(self):
export_type = "csv"
options = {
Expand Down
8 changes: 7 additions & 1 deletion onadata/libs/utils/api_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from onadata.libs.utils.export_tools import (
check_pending_export,
get_latest_generic_export,
get_query_params_from_metadata,
generate_attachments_zip_export,
generate_entity_list_export,
generate_export,
Expand Down Expand Up @@ -151,7 +152,12 @@ def custom_response_handler( # noqa: C0901
):
export_type = Export.EXTERNAL_EXPORT

options = parse_request_export_options(request.query_params)
metadata_query_params = get_query_params_from_metadata(metadata) or {}

options = {
**parse_request_export_options(request.query_params),
**metadata_query_params,
}

dataview_pk = hasattr(dataview, "pk") and dataview.pk
options["dataview_pk"] = dataview_pk
Expand Down
31 changes: 23 additions & 8 deletions onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,28 @@ def generate_kml_export(
return export


def get_query_params_from_metadata(metadata):
"""
Build out query params to be used in GeoJsonSerializer
"""
if metadata is None or metadata.extra_data is None:
return None

extra_data = metadata.extra_data
keys_mapping = {
"data_geo_field": "geo_field",
"data_simple_style": "simple_style",
"data_title": "title",
"data_fields": "fields",
}

return {
mapped_key: extra_data[original_key]
for original_key, mapped_key in keys_mapping.items()
if original_key in extra_data
}


def generate_geojson_export(
export_type,
username,
Expand All @@ -665,14 +687,7 @@ def generate_geojson_export(
if xform is None:
xform = XForm.objects.get(user__username=username, id_string=id_string)
request = HttpRequest()
extra_data = metadata.extra_data
# build out query params to be used in GeoJsonSerializer
request.query_params = {
"geo_field": extra_data.get("data_geo_field"),
"simple_style": extra_data.get("data_simple_style"),
"title": extra_data.get("data_title"),
"fields": extra_data.get("data_fields"),
}
request.query_params = get_query_params_from_metadata(metadata)
_context = {}
_context["request"] = request
# filter out deleted submissions
Expand Down

0 comments on commit 03436fa

Please sign in to comment.