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

Csv import overwrite enabled #2166

Merged
merged 5 commits into from
Nov 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
5 changes: 4 additions & 1 deletion docs/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,10 @@ Response
Import CSV data to existing form
---------------------------------

- `csv_file` a valid csv file with exported data (instance/submission per row)
- `csv_file` a valid csv file with exported data (instance/submission per row).

Use the `overwrite` query parameter to clear all previous submissions while importing submissions.
Note: Overwrites are not reversible & the `uuid` column is ignored during the process and is always regenerated.

.. raw:: html

Expand Down
2 changes: 2 additions & 0 deletions onadata/libs/tests/utils/fixtures/same_uuid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,age,gender,photo,date,location,test_location.latitude,test_location.longitude,test_location.altitude,test_location.precision,test_location2.latitude,test_location2.longitude,test_location2.altitude,test_location2.precision,_location_latitude,_location_longitude,_location_altitude,_location_precision,pizza_fan,pizza_type,favorite_toppings/cheese,favorite_toppings/pepperoni,favorite_toppings/sausauge,favorite_toppings/green_peppers,favorite_toppings/mushrooms,favorite_toppings/anchovies,a_group/grouped,a_group/a_text,start_time,end_time,today,imei,phonenumber,meta/instanceID,_uuid,_submission_time,_tags,_notes,_submitted_by,_version,_duration
Name_1,10,male,NA,n/a,83.3595 -32.8601 0 1,83.3595,-32.8601,0,1,21.22474,-10.5601,50000,200,83.3595,-32.8601,0,1,no,n/a,n/a,n/a,n/a,n/a,n/a,n/a,n/a,n/a,2014-09-04T15:06:01.000+03:00,2014-09-04T15:07:17.000+03:00,2014-09-04,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:182b3388-77c7-4f59-8499-becdbf264645,182b3388-77c7-4f59-8499-becdbf264645,2014-09-04T12:08:04,,,,,
28 changes: 28 additions & 0 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,31 @@ def test_enforces_data_type_and_rollback(self):
expected_error)
# Assert all created instances were rolled back
self.assertEqual(count, Instance.objects.count())

def test_csv_import_with_overwrite(self):
self._publish_xls_file(self.xls_file_path)

surveys = ['uuid1']

paths = [os.path.join(
self.fixtures_dir, 'tutorial', 'instances', s, 'submission.xml')
for s in surveys]

for path in paths:
self._make_submission(path)

self.xform = XForm.objects.last()
count = self.xform.instances.all().count()

self.assertEqual(count, 1)

single_csv = open(os.path.join(self.fixtures_dir, 'same_uuid.csv'),
'rb')

csv_import.submit_csv(self.user.username, self.xform, single_csv,
overwrite=True)
self.xform.refresh_from_db()
count = self.xform.instances.filter(deleted_at=None).count()

self.assertEqual(count, 1)
self.assertEqual(self.xform.num_of_submissions, 1)
5 changes: 4 additions & 1 deletion onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ def submit_csv(username, xform, csv_file, overwrite=False):
xform.instances.filter(deleted_at__isnull=True)\
.update(deleted_at=timezone.now(),
deleted_by=User.objects.get(username=username))
# updates the form count
xform.submission_count(True)
# send message
send_message(
instance_id=instance_ids, target_id=xform.id,
Expand Down Expand Up @@ -381,7 +383,7 @@ def submit_csv(username, xform, csv_file, overwrite=False):

submission_time = datetime.utcnow().isoformat()
row_uuid = row.get('meta/instanceID') or 'uuid:{}'.format(
row.get(UUID)) if row.get(UUID) else None
row.get(UUID)) if row.get(UUID) and not overwrite else None
submitted_by = row.get('_submitted_by')
submission_date = row.get('_submission_time', submission_time)

Expand Down Expand Up @@ -476,6 +478,7 @@ def submit_csv(username, xform, csv_file, overwrite=False):
errors) if errors else ''
)
else:
xform.submission_count(True)
added_submissions = additions - inserts
event_by = User.objects.get(username=username)
event_name = None
Expand Down
1 change: 1 addition & 0 deletions onadata/settings/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
ENKETO_API_INSTANCE_PATH = '/api_v1/instance'
ENKETO_SINGLE_SUBMIT_PATH = '/api/v2/survey/single/once'
ENKETO_API_INSTANCE_IFRAME_URL = ENKETO_URL + "api_v1/instance/iframe"
NOTIFICATION_BACKENDS = {}
else:
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/') # noqa

Expand Down