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

Add GeoJSON attachment params to EXPORT_OPTIONS #2757

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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
98 changes: 97 additions & 1 deletion 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,51 @@ 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} testgeojson",
extra_data={
"data_title": "start",
"data_fields": "",
"data_geo_field": "qn09",
"data_simple_style": True,
},
object_id=self.xform.id,
)
self.assertEqual(
{
"title": "start",
"fields": "",
"geo_field": "qn09",
"simple_style": True,
},
get_query_params_from_metadata(metadata),
)

metadata.delete()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrankApiyo Instead of deleting and creating a new MetaData object, we can update metadata.extra_data then do metadata.refresh_from_db()

metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(XForm),
data_type="media",
data_value=f"xform_geojson {self.xform.id} testgeojson",
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),
)

def test_should_not_create_new_export_when_old_exists(self):
export_type = "geojson"
self._publish_transportation_form_and_submit_instance()
Expand Down Expand Up @@ -310,6 +356,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 @@ -336,6 +385,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 +401,50 @@ 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.delete()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrankApiyo Will updating metadata.extra_data suffice instead of deleting and creating a new one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever you create a new linked dataset, a new metadata object is created; So creating a new metadata object more closely simulates what happens in production. (as compared to editing an existing metadata object)

Copy link
Contributor

@kelvin-muchiri kelvin-muchiri Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrankApiyo Noted. Though l believe since this is a unit test and not an integration test, we should only be interested in the metadata passed to custom_response_handler and not the larger context of how the MetaData object is used

metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(XForm),
data_type="media",
data_value=f"xform_geojson {self.xform.id} testgeojson",
extra_data={
"data_title": "end",
"data_fields": "",
"data_geo_field": "qn09",
"data_simple_style": True,
},
object_id=self.xform.id,
)
_response = custom_response_handler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the variable _response is unused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This _response variables might provide some implicit documentation by telling you that this custom_reponse_handler returns a response object

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
34 changes: 26 additions & 8 deletions onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,31 @@ 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:
return None

extra_data = metadata.extra_data
if extra_data is None:
FrankApiyo marked this conversation as resolved.
Show resolved Hide resolved
return None

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 +690,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
Loading