Skip to content

Commit

Permalink
Merge pull request #2734 from onaio/fix-geojson-process-creation-bug
Browse files Browse the repository at this point in the history
Ensure geojson exports creation process is idempotent
  • Loading branch information
FrankApiyo authored Nov 15, 2024
2 parents a4f8a62 + bcb4c4d commit 03c51fd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
76 changes: 76 additions & 0 deletions onadata/libs/tests/utils/test_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.core.files.storage import default_storage
from django.core.files.temp import NamedTemporaryFile
from django.test.utils import override_settings
from django.test import RequestFactory
from django.utils import timezone

from pyxform.builder import create_survey_from_xls
Expand All @@ -23,8 +24,11 @@
from savReaderWriter import SavWriter

from onadata.apps.api import tests as api_tests
from onadata.apps.main.models import MetaData
from django.contrib.contenttypes.models import ContentType
from onadata.apps.api.tests.viewsets.test_abstract_viewset import TestAbstractViewSet
from onadata.apps.api.viewsets.data_viewset import DataViewSet
from onadata.libs.utils.api_export_tools import custom_response_handler
from onadata.apps.logger.models import Attachment, Instance, XForm, Entity, EntityList
from onadata.apps.viewer.models.export import Export, GenericExport
from onadata.apps.viewer.models.parsed_instance import query_fields_data
Expand Down Expand Up @@ -273,6 +277,78 @@ def test_should_not_create_new_export_when_old_exists(self):

self.assertFalse(will_create_new_export)

def test_should_not_create_new_export_when_old_exists(self):
export_type = "geojson"
self._publish_transportation_form_and_submit_instance()

request = RequestFactory().get("/")
request.user = self.user
request.query_params = options = {}
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,
)
_response = custom_response_handler(
request,
self.xform,
{},
export_type,
filename="testgeojson",
dataview=False,
metadata=metadata,
)

self.assertEqual(1, Export.objects.filter(xform=self.xform).count())
self.assertEqual(
{
"dataview_pk": False,
"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.get(xform=self.xform).options,
)
_response = custom_response_handler(
request,
self.xform,
{},
export_type,
filename="testgeojson",
dataview=False,
metadata=metadata,
)
# we still have only one export, we didn't generate another
self.assertEqual(1, Export.objects.filter(xform=self.xform).count())
self.assertEqual(
{
"dataview_pk": False,
"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.get(xform=self.xform).options,
)

def test_should_create_new_export_when_filter_defined(self):
export_type = "csv"
options = {
Expand Down
11 changes: 10 additions & 1 deletion onadata/libs/utils/api_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def _new_export():
export_type,
dataview_pk=dataview_pk,
metadata=metadata,
export_options=options,
)

if should_create_new_export(xform, export_type, options, request=request):
Expand Down Expand Up @@ -245,7 +246,13 @@ def _new_export():


def _generate_new_export( # noqa: C0901
request, xform, query, export_type, dataview_pk=False, metadata=None
request,
xform,
query,
export_type,
dataview_pk=False,
metadata=None,
export_options=None,
):
query = _set_start_end_params(request, query)
extension = _get_extension_from_export_type(export_type)
Expand All @@ -257,6 +264,8 @@ def _generate_new_export( # noqa: C0901
"host": request.get_host(),
"sort": request.query_params.get("sort"),
}
if isinstance(export_options, dict):
options.update(export_options)
if query:
options["query"] = query

Expand Down

0 comments on commit 03c51fd

Please sign in to comment.