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

Add support for choice from previous repeat answers #381

Closed
Closed
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
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]"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that string-length(./" + name + ") > 0 is not intended to be hardcoded. It should just be whatever the contents of the choice_filter column is.

With the following definition:

type name label choice_filter
begin repeat rep
text food Enter food
end repeat
select_one ${food} choice Choose

There should be no predicate at all.

With the following definition:

type name label choice_filter
begin repeat rep
text food Enter food
end repeat
select_one ${food} choice Choose starts-with(., "b")

The predicate should be starts-with(., "b").

Note that the choice filter/predicate could possibly include a reference to another node (starts-with(., ${favorite_letter}).

)
itemset_children = [node("value", ref=name), node("label", ref=name)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #370 because the values will be user-generated and could contain spaces. If this turns out to be a popular feature, we may want to allow users to specify value and label separately. I don't think this needs to be done now but wanted to call it out in case that changes things for anyone.

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 choices 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]">'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be a predicate.

],
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