Skip to content

Commit

Permalink
Use current() only for choice_filters
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Dec 2, 2018
1 parent 452509d commit 3c31570
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def xml_control(self):
if self["query"]:
choice_filter = self.get("choice_filter")
query = "instance('" + self["query"] + "')/root/item"
choice_filter = survey.insert_xpaths(choice_filter, self)
choice_filter = survey.insert_xpaths(choice_filter, self, True)
if choice_filter:
query += "[" + choice_filter + "]"
result.setAttribute("query", query)
Expand Down Expand Up @@ -149,7 +149,7 @@ def xml_control(self):
itemset = self["itemset"]
itemset_label_ref = "jr:itext(itextId)"
nodeset = "instance('" + itemset + "')/root/item"
choice_filter = survey.insert_xpaths(choice_filter, self)
choice_filter = survey.insert_xpaths(choice_filter, self, True)
if choice_filter:
nodeset += "[" + choice_filter + "]"

Expand Down
9 changes: 5 additions & 4 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _setup_xpath_dictionary(self):
else:
self._xpath[element.name] = element.get_xpath()

def _var_repl_function(self, matchobj, context):
def _var_repl_function(self, matchobj, context, use_current=False):
"""
Given a dictionary of xpaths, return a function we can use to
replace ${varname} with the xpath to varname.
Expand All @@ -723,17 +723,18 @@ def _var_repl_function(self, matchobj, context):
context.get_xpath())
if steps:
ref_path = ref_path if ref_path.endswith(name) else "/%s" % name
return " " + "/".join([".."] * steps) + ref_path + " "
prefix = " current()/" if use_current else " "
return prefix + "/".join([".."] * steps) + ref_path + " "

return " " + self._xpath[name] + " "

def insert_xpaths(self, text, context):
def insert_xpaths(self, text, context, use_current=False):
"""
Replace all instances of ${var} with the xpath to var.
"""
bracketed_tag = r"\$\{(.*?)\}"
def _var_repl_function(matchobj):
return self._var_repl_function(matchobj, context)
return self._var_repl_function(matchobj, context, use_current)

return re.sub(bracketed_tag, _var_repl_function, unicode(text))

Expand Down
3 changes: 2 additions & 1 deletion pyxform/tests/test_output/yes_or_no_question.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
}
],
"type": "select one",
"name": "good_day",
"name": "good_day",
"parameters": {},
"label": {
"english": "have you had a good day today?"
}
Expand Down
4 changes: 2 additions & 2 deletions pyxform/tests_v1/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_choice_filter_relative_path(self): # pylint: disable=invalid-name
| | crop_list | kale | Kale | |
""", # noqa pylint: disable=line-too-long
xml__contains=[
"""<itemset nodeset="instance('crop_list')/root/item[name = ../crop ]">""", # noqa pylint: disable=line-too-long
"""<itemset nodeset="instance('crop_list')/root/item[name = ../../crop ]">""", # noqa pylint: disable=line-too-long
"""<itemset nodeset="instance('crop_list')/root/item[name = current()/../crop ]">""", # noqa pylint: disable=line-too-long
"""<itemset nodeset="instance('crop_list')/root/item[name = current()/../../crop ]">""", # noqa pylint: disable=line-too-long
],
)

0 comments on commit 3c31570

Please sign in to comment.