Skip to content

Commit

Permalink
flatten select multiples in repeating sections
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Apr 8, 2024
1 parent 1c5ad83 commit 7909095
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,age,grp1/grp2/browser_use[1]/grp3/grp4/grp5/year,grp1/grp2/browser_use[1]/grp3/grp4/grp5/browsers/firefox,grp1/grp2/browser_use[1]/grp3/grp4/grp5/browsers/chrome,grp1/grp2/browser_use[1]/grp3/grp4/grp5/browsers/ie,grp1/grp2/browser_use[1]/grp3/grp4/grp5/browsers/safari,grp1/grp2/browser_use[2]/grp3/grp4/grp5/year,grp1/grp2/browser_use[2]/grp3/grp4/grp5/browsers/firefox,grp1/grp2/browser_use[2]/grp3/grp4/grp5/browsers/chrome,grp1/grp2/browser_use[2]/grp3/grp4/grp5/browsers/ie,grp1/grp2/browser_use[2]/grp3/grp4/grp5/browsers/safari,meta/instanceID
Vic,30,2010,True,False,False,True,2011,True,True,False,False,uuid:13f5c2a8-0ba2-44e2-bd0a-9c6fa3991484
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,age,grp1/grp2/browser_use[1]/grp3/grp4/grp5/year,grp1/grp2/browser_use[1]/grp3/grp4/grp5/browsers,grp1/grp2/browser_use[2]/grp3/grp4/grp5/year,grp1/grp2/browser_use[2]/grp3/grp4/grp5/browsers,meta/instanceID
Vic,30,2010,firefox safari,2011,firefox chrome,uuid:13f5c2a8-0ba2-44e2-bd0a-9c6fa3991484
104 changes: 104 additions & 0 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,107 @@ def test_get_columns_by_type(self):
self.assertTrue(
submission.json["section_B/year_established"].startswith("1890")
)

def test_select_multiples_grouped_repeating_w_split(self):
"""Select multiple choices within group within repeat with split"""
md_xform = """
| survey | | | |
| | type | name | label |
| | text | name | Name |
| | integer | age | Age |
| | begin group | grp1 | Group 1 |
| | begin group | grp2 | Group 2 |
| | begin repeat | browser_use | Browser Use |
| | begin group | grp3 | Group 3 |
| | begin group | grp4 | Group 4 |
| | begin group | grp5 | Group 5 |
| | integer | year | Year |
| | select_multiple browsers | browsers | Browsers |
| | end group | | |
| | end group | | |
| | end group | | |
| | end repeat | | |
| | end group | | |
| | end group | | |
| choices | | | |
| | list_name | name | label |
| | browsers | firefox | Firefox |
| | browsers | chrome | Chrome |
| | browsers | ie | Internet Explorer |
| | browsers | safari | Safari |"""
xform = self._publish_markdown(md_xform, self.user, id_string="nested_split")

with open(
os.path.join(
self.fixtures_dir, "csv_import_multiple_split_group_repeat.csv"
),
"rb",
) as csv_file:
csv_import.submit_csv(self.user.username, xform, csv_file)
self.assertEqual(Instance.objects.count(), 1)
submission = Instance.objects.first()
self.assertEqual(
submission.json["grp1/grp2/browser_use"],
[
{
"grp1/grp2/browser_use/grp3/grp4/grp5/year": 2010,
"grp1/grp2/browser_use/grp3/grp4/grp5/browsers": "firefox safari",
},
{
"grp1/grp2/browser_use/grp3/grp4/grp5/year": 2011,
"grp1/grp2/browser_use/grp3/grp4/grp5/browsers": "firefox chrome",
},
],
)

def test_select_multiples_grouped_repeating_wo_split(self):
"""Select multiple choices within group within repeat without split"""
md_xform = """
| survey | | | |
| | type | name | label |
| | text | name | Name |
| | integer | age | Age |
| | begin group | grp1 | Group 1 |
| | begin group | grp2 | Group 2 |
| | begin repeat | browser_use | Browser Use |
| | begin group | grp3 | Group 3 |
| | begin group | grp4 | Group 4 |
| | begin group | grp5 | Group 5 |
| | integer | year | Year |
| | select_multiple browsers | browsers | Browsers |
| | end group | | |
| | end group | | |
| | end group | | |
| | end repeat | | |
| | end group | | |
| | end group | | |
| choices | | | |
| | list_name | name | label |
| | browsers | firefox | Firefox |
| | browsers | chrome | Chrome |
| | browsers | ie | Internet Explorer |
| | browsers | safari | Safari |"""
xform = self._publish_markdown(md_xform, self.user, id_string="nested_split")

with open(
os.path.join(
self.fixtures_dir, "csv_import_multiple_wo_split_group_repeat.csv"
),
"rb",
) as csv_file:
csv_import.submit_csv(self.user.username, xform, csv_file)
self.assertEqual(Instance.objects.count(), 1)
submission = Instance.objects.first()
self.assertEqual(
submission.json["grp1/grp2/browser_use"],
[
{
"grp1/grp2/browser_use/grp3/grp4/grp5/year": 2010,
"grp1/grp2/browser_use/grp3/grp4/grp5/browsers": "firefox safari",
},
{
"grp1/grp2/browser_use/grp3/grp4/grp5/year": 2011,
"grp1/grp2/browser_use/grp3/grp4/grp5/browsers": "firefox chrome",
},
],
)
14 changes: 13 additions & 1 deletion onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,26 @@ def flatten_split_select_multiples(
for key, value in row.items():
if key in select_multiples and isinstance(value, dict):
picked_choices = [
k for k, v in value.items() if v in ["1", "TRUE"] or v == k
k for k, v in value.items() if v.upper() in ["1", "TRUE"] or v == k
]
new_value = " ".join(picked_choices)
row.update({key: new_value})
elif isinstance(value, dict):
# Handle cases where select_multiples are within a group
new_value = flatten_split_select_multiples(value, select_multiples)
row.update({key: new_value})
elif isinstance(value, list):
# Handle case where we have repeat questions
new_value = []

for repeat_question in value:
flattened_question = flatten_split_select_multiples(
repeat_question, select_multiples
)
new_value.append(flattened_question)

row.update({key: new_value})

return row


Expand Down

0 comments on commit 7909095

Please sign in to comment.