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

Support ref in constraint and required messages with single language #487

Merged
merged 2 commits into from
Dec 7, 2020
Merged
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
10 changes: 5 additions & 5 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import codecs
import os
import re
from re import split
import tempfile
import xml.etree.ElementTree as ETree
from collections import defaultdict
from datetime import datetime

from pyxform import constants
from pyxform.utils import (
BRACKETED_TAG_REGEX,
LAST_SAVED_REGEX,
LAST_SAVED_INSTANCE_NAME,
)
from pyxform.errors import PyXFormError, ValidationError
from pyxform.external_instance import ExternalInstance
from pyxform.instance import SurveyInstance
Expand All @@ -36,10 +40,6 @@
except ImportError:
from functools32 import lru_cache

LAST_SAVED_INSTANCE_NAME = "__last-saved"
BRACKETED_TAG_REGEX = re.compile(r"\${(last-saved#)?(.*?)}")
LAST_SAVED_REGEX = re.compile(r"\${last-saved#(.*?)}")


def register_nsmap():
"""Function to register NSMAP namespaces with ETree"""
Expand Down
25 changes: 22 additions & 3 deletions pyxform/survey_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from pyxform.question_type_dictionary import QUESTION_TYPE_DICT
from pyxform.utils import (
INVALID_XFORM_TAG_REGEXP,
BRACKETED_TAG_REGEX,
is_valid_xml_tag,
node,
unicode,
basestring,
default_is_dynamic,
)
from pyxform.xls2json import print_pyobj_to_json
Expand Down Expand Up @@ -275,6 +275,14 @@ def get_translations(self, default_language):
"text": text,
"output_context": self,
}
elif constraint_msg and re.search(BRACKETED_TAG_REGEX, constraint_msg):
yield {
"path": self._translation_path("jr:constraintMsg"),
"lang": default_language,
"text": constraint_msg,
"output_context": self,
}

required_msg = bind_dict.get("jr:requiredMsg")
if type(required_msg) is dict:
for lang, text in required_msg.items():
Expand All @@ -284,6 +292,13 @@ def get_translations(self, default_language):
"text": text,
"output_context": self,
}
elif required_msg and re.search(BRACKETED_TAG_REGEX, required_msg):
yield {
"path": self._translation_path("jr:requiredMsg"),
"lang": default_language,
"text": required_msg,
"output_context": self,
}
no_app_error_string = bind_dict.get("jr:noAppErrorString")
if type(no_app_error_string) is dict:
for lang, text in no_app_error_string.items():
Expand Down Expand Up @@ -426,9 +441,13 @@ def xml_binding(self):
and k in self.CONVERTIBLE_BIND_ATTRIBUTES
):
v = self.binding_conversions[v]
if k == "jr:constraintMsg" and type(v) is dict:
if k == "jr:constraintMsg" and (
type(v) is dict or re.search(BRACKETED_TAG_REGEX, v)
):
v = "jr:itext('%s')" % self._translation_path("jr:constraintMsg")
if k == "jr:requiredMsg" and type(v) is dict:
if k == "jr:requiredMsg" and (
type(v) is dict or re.search(BRACKETED_TAG_REGEX, v)
):
v = "jr:itext('%s')" % self._translation_path("jr:requiredMsg")
if k == "jr:noAppErrorString" and type(v) is dict:
v = "jr:itext('%s')" % self._translation_path("jr:noAppErrorString")
Expand Down
30 changes: 30 additions & 0 deletions pyxform/tests_v1/test_bind_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ def test_bind_required_conversion(self):
xml__contains=['<bind nodeset="/data/text" required="false()"'],
)

def test_bind_required_message_with_reference(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | required | required_message |
| | int | foo | foo | | |
| | text | text | text | true() | required, ${foo} |
""",
xml__contains=[
'<bind nodeset="/data/text" required="true()" type="string" jr:requiredMsg="jr:itext(\'/data/text:jr:requiredMsg\')"',
'<value> required, <output value=" /data/foo "/> </value>',
],
)

def test_bind_constraint_conversion(self):
self.assertPyxformXform(
name="data",
Expand All @@ -45,6 +60,21 @@ def test_bind_constraint_conversion(self):
],
)

def test_bind_constraint_message_with_reference(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | constraint | constraint_message |
| | int | foo | foo | | |
| | text | text | text | string-length(.) > 1 | too short ${foo} |
""",
xml__contains=[
'<bind constraint="string-length(.) &gt; 1" nodeset="/data/text" type="string" jr:constraintMsg="jr:itext(\'/data/text:jr:constraintMsg\')"',
'<value> too short <output value=" /data/foo "/> </value>',
],
)

def test_bind_custom_conversion(self):
self.assertPyxformXform(
name="data",
Expand Down
4 changes: 4 additions & 0 deletions pyxform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@

INVALID_XFORM_TAG_REGEXP = r"[^a-zA-Z:_][^a-zA-Z:_0-9\-.]*"

LAST_SAVED_INSTANCE_NAME = "__last-saved"
BRACKETED_TAG_REGEX = re.compile(r"\${(last-saved#)?(.*?)}")
LAST_SAVED_REGEX = re.compile(r"\${last-saved#(.*?)}")

NSMAP = {
"xmlns": "http://www.w3.org/2002/xforms",
"xmlns:h": "http://www.w3.org/1999/xhtml",
Expand Down