Skip to content

Commit

Permalink
Add support for choice from previous repeat answers
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Oct 2, 2019
1 parent 2e032f6 commit c8041a0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,21 @@ def xml_control(self):
node("label", ref=itemset_label_ref),
]
result.appendChild(node("itemset", *itemset_children, nodeset=nodeset))
elif not self["children"] and self["list_name"]:
list_name = survey.insert_xpaths(self["list_name"], self).strip()
path = list_name.split("/")
depth = len(path)
if depth > 2:
name = path[-1]
nodeset = "/".join(
path[:-2] + [path[-2] + "[string-length(./" + name + ") > 0]"]
)
itemset_children = [node("value", ref=name), node("label", ref=name)]
result.appendChild(node("itemset", *itemset_children, nodeset=nodeset))
else:
for n in [o.xml() for o in self.children]:
result.appendChild(n)
for child in self.children:
result.appendChild(child.xml())

return result


Expand Down
23 changes: 23 additions & 0 deletions pyxform/tests_v1/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,26 @@ def test_hints_are_present_within_groups(self):
</group>""" # noqa

self.assertPyxformXform(md=md, xml__contains=[expected], run_odk_validate=True)

def test_choice_from_previous_repeat_answers(self):
"""Select one choics from previous repeat answers."""
xlsform_md = """
| survey | | | |
| | type | name | label |
| | begin repeat | rep | Repeat |
| | text | name | Enter name |
| | end repeat | | |
| | select one fruits | fruit | Choose a fruit |
| | select one ${name} | choice | Choose name |
| choices | | | |
| | list name | name | label |
| | fruits | banana | Banana |
| | fruits | mango | Mango |
"""
self.assertPyxformXform(
md=xlsform_md,
xml__contains=[
'<itemset nodeset="/pyxform_autotestname/rep[string-length(./name) &gt; 0]">'
],
run_odk_validate=True,
)
3 changes: 3 additions & 0 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ def replace_prefix(d, prefix):
list_name not in choices
and select_type != "select one external"
and file_extension not in [".csv", ".xml"]
and not re.match(r"\$\{(.*?)\}", list_name)
):
if not choices:
raise PyXFormError(
Expand Down Expand Up @@ -1133,6 +1134,8 @@ def replace_prefix(d, prefix):
json_dict["choices"] = choices
elif file_extension in [".csv", ".xml"]:
new_json_dict["itemset"] = list_name
elif re.match(r"\$\{(.*?)\}", list_name):
new_json_dict["list_name"] = list_name
else:
new_json_dict["list_name"] = list_name
new_json_dict[constants.CHOICES] = choices[list_name]
Expand Down

0 comments on commit c8041a0

Please sign in to comment.