Skip to content

Commit

Permalink
Add support for choices from previous repeat answers with choice filter
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Sep 24, 2020
1 parent 4ca43f3 commit ba03680
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
XForm Survey element classes for different question types.
"""
import os.path
import re

from pyxform.errors import PyXFormError
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
Expand Down Expand Up @@ -196,6 +197,7 @@ def build_xml(self):
choice_filter = self.get("choice_filter")
itemset, file_extension = os.path.splitext(self["itemset"])
has_media = False
is_previous_question = bool(re.match(r"^\${.*}$", self.get("itemset")))

if choices.get(itemset):
has_media = bool(choices[itemset][0].get("media"))
Expand All @@ -210,9 +212,17 @@ def build_xml(self):
else:
itemset = self["itemset"]
itemset_label_ref = "jr:itext(itextId)"
nodeset = "instance('" + itemset + "')/root/item"

choice_filter = survey.insert_xpaths(choice_filter, self, True)
if is_previous_question:
path = survey.insert_xpaths(self["itemset"], self).strip().split("/")
nodeset = "/".join(path[:-1])
itemset_label_ref = path[-1]
else:
nodeset = "instance('" + itemset + "')/root/item"

if choice_filter:
choice_filter = choice_filter.replace(nodeset, ".")
nodeset += "[" + choice_filter + "]"

if self["parameters"]:
Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests_v1/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def test_choice_from_previous_repeat_answers_with_choice_filter(self):
md=xlsform_md,
xml__contains=[
'<itemset nodeset="/data/rep[starts-with(./name, &quot;b&quot;)]">',
'<itemset nodeset="/data/rep[./demographics/age &gt; 18]">',
'<itemset nodeset="/data/rep[ ./demographics/age &gt; 18]">',
],
run_odk_validate=True,
)

0 comments on commit ba03680

Please sign in to comment.