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

Set status to imported_via_csv for CSV Imported submissions #2077

Merged
merged 4 commits into from
May 17, 2021
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
4 changes: 3 additions & 1 deletion onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ class Instance(models.Model, InstanceBaseClass):

# ODK keeps track of three statuses for an instance:
# incomplete, submitted, complete
# we add a fourth status: submitted_via_web
# we add the following additional statuses:
# - submitted_via_web
# - imported_via_csv
status = models.CharField(max_length=20,
default=u'submitted_via_web')
uuid = models.CharField(max_length=249, default=u'', db_index=True)
Expand Down
17 changes: 17 additions & 0 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ def test_csv_with_multiple_select_in_one_column(self):
self.assertEqual(Instance.objects.count(), 1,
'submit_csv edits #1 test Failed!')

def test_csv_imports_are_tracked(self):
"""
Test that submissions created via CSV Import are tracked
"""
self.xls_file_path = os.path.join(self.fixtures_dir,
'form_with_multiple_select.xlsx')
self._publish_xls_file(self.xls_file_path)
self.xform = XForm.objects.get()

good_csv = open(
os.path.join(self.fixtures_dir,
'csv_import_with_multiple_select.csv'),
'rb')
csv_import.submit_csv(self.user.username, self.xform, good_csv)
self.assertEqual(Instance.objects.count(), 1)
self.assertEqual(Instance.objects.first().status, 'imported_via_csv')

def test_csv_with_repeats_import(self):
self.xls_file_path = os.path.join(self.this_directory, 'fixtures',
'csv_export',
Expand Down
4 changes: 3 additions & 1 deletion onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ def submit_csv(username, xform, csv_file, overwrite=False):

try:
error, instance = safe_create_instance(
username, xml_file, [], xform.uuid, None)
username, xml_file, [], xform.uuid, None,
instance_status='imported_via_csv'
)
except ValueError as e:
error = e

Expand Down
7 changes: 5 additions & 2 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,9 @@ def create_instance(username,


@use_master
def safe_create_instance(username, xml_file, media_files, uuid, request):
def safe_create_instance(
username, xml_file, media_files, uuid, request,
instance_status: str = 'submitted_via_web'):
"""Create an instance and catch exceptions.

:returns: A list [error, instance] where error is None if there was no
Expand All @@ -520,7 +522,8 @@ def safe_create_instance(username, xml_file, media_files, uuid, request):

try:
instance = create_instance(
username, xml_file, media_files, uuid=uuid, request=request)
username, xml_file, media_files, uuid=uuid, request=request,
status=instance_status)
except InstanceInvalidUserError:
error = OpenRosaResponseBadRequest(_(u"Username or ID required."))
except InstanceEmptyError:
Expand Down