Skip to content
Open
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
1 change: 1 addition & 0 deletions pyxform/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
u"no_app_error_string" : u"bind::jr:noAppErrorString",
u"requiredMsg" : u"bind::jr:requiredMsg",
u"required_message" : u"bind::jr:requiredMsg",
u"body":u"control"
}
list_header = {
u"caption": constants.LABEL,
Expand Down
79 changes: 35 additions & 44 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ def validate(self):
)

def xml_instance(self):
survey = self.get_root()
attributes = {}
attributes.update(self.get(u'instance', {}))
for key, value in attributes.items():
attributes[key] = survey.insert_xpaths(value)
if self.get(u"default"):
#survey = self.get_root()
#return node(self.name, survey.insert_xpaths(unicode(self.get(u"default"))))
return node(self.name, unicode(self.get(u"default")))
return node(self.name)
return node(self.name,
unicode(self.get(u"default")),
**attributes
)
return node(self.name, **attributes)

def xml_control(self):
return None
Expand All @@ -35,14 +43,12 @@ class InputQuestion(Question):
def xml_control(self):
control_dict = self.control
label_and_hint = self.xml_label_and_hint()
control_dict['ref'] = self.get_xpath()

# for SurveyCTO's support for external intents, resolve field references in appearance column:
survey = self.get_root()
appearance=control_dict.get('appearance')
if appearance is not None:
control_dict['appearance'] = survey.insert_xpaths(appearance)

# Resolve field references in attributes
for key, value in control_dict.items():
control_dict[key] = survey.insert_xpaths(value)
control_dict['ref'] = self.get_xpath()

result = node(**control_dict)
if label_and_hint:
for element in self.xml_label_and_hint():
Expand All @@ -63,41 +69,30 @@ class TriggerQuestion(Question):

def xml_control(self):
control_dict = self.control
if u"appearance" in control_dict:
return node(
u"trigger", ref=self.get_xpath(),
appearance=control_dict[u"appearance"],
*self.xml_label_and_hint()
)
else:
return node(u"trigger",
ref=self.get_xpath(),
*self.xml_label_and_hint()
)

survey = self.get_root()
# Resolve field references in attributes
for key, value in control_dict.items():
control_dict[key] = survey.insert_xpaths(value)
control_dict['ref'] = self.get_xpath()
return node(
u"trigger",
*self.xml_label_and_hint(),
**control_dict
)

class UploadQuestion(Question):
def _get_media_type(self):
return self.control[u"mediatype"]

def xml_control(self):
control_dict = self.control
if u"appearance" in control_dict:
return node(
u"upload",
ref=self.get_xpath(),
mediatype=self._get_media_type(),
appearance=control_dict[u"appearance"],
*self.xml_label_and_hint()
)
else:
return node(
u"upload",
ref=self.get_xpath(),
mediatype=self._get_media_type(),
*self.xml_label_and_hint()
)

control_dict['ref'] = self.get_xpath()
control_dict['mediatype'] = self._get_media_type()
return node(
u"upload",
*self.xml_label_and_hint(),
**control_dict
)

class Option(SurveyElement):

Expand Down Expand Up @@ -162,20 +157,16 @@ def validate(self):

def xml_control(self):
assert self.bind[u"type"] in [u"select", u"select1"]

survey = self.get_root()
control_dict = self.control.copy()
# Resolve field references in attributes
for key, value in control_dict.items():
control_dict[key] = survey.insert_xpaths(value)
control_dict['ref'] = self.get_xpath()

# for SurveyCTO's dynamic-select support, resolve field references in appearance column:
appearance = control_dict.get('appearance')
if appearance is not None:
control_dict['appearance'] = survey.insert_xpaths(appearance)

result = node(**control_dict)
for element in self.xml_label_and_hint():
result.appendChild(element)
survey = self.get_root()
# itemset are only supposed to be strings, check to prevent the rare dicts that show up
if self['itemset'] and isinstance( self['itemset'] , basestring):
choice_filter = self.get('choice_filter')
Expand Down
36 changes: 25 additions & 11 deletions pyxform/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ def xml_instance(self, **kwargs):
"""
Creates an xml representation of the section
"""
result = node(self.name, **kwargs)
attributes = {}
attributes.update(kwargs)
attributes.update(self.get(u'instance', {}))
survey = self.get_root()
# Resolve field references in attributes
for key, value in attributes.items():
attributes[key] = survey.insert_xpaths(value)
result = node(self.name, **attributes)
for child in self.children:
if child.get(u"flat"):
for grandchild in child.xml_instance_array():
Expand Down Expand Up @@ -70,10 +77,10 @@ def xml_control(self):
</group>
"""
control_dict = self.control.copy()
jrcount = control_dict.get('jr:count')
if jrcount:
survey = self.get_root()
control_dict['jr:count'] = survey.insert_xpaths(jrcount)
survey = self.get_root()
# Resolve field references in attributes
for key, value in control_dict.items():
control_dict[key] = survey.insert_xpaths(value)
repeat_node = node(u"repeat", nodeset=self.get_xpath(), **control_dict)

for n in Section.xml_control(self):
Expand All @@ -85,7 +92,7 @@ def xml_control(self):
u"group", self.xml_label(), repeat_node,
ref=self.get_xpath()
)
return node(u"group", repeat_node, ref=self.get_xpath())
return node(u"group", repeat_node, ref=self.get_xpath(), **self.control)

#I'm anal about matching function signatures when overriding a function, but there's no reason for kwargs to be an argument
def xml_instance(self, **kwargs):
Expand All @@ -111,24 +118,31 @@ def xml_control(self):
return None

children = []
attrs = {}
attributes = {}
attributes.update(self.control)

survey = self.get_root()

# Resolve field references in attributes
for key, value in attributes.items():
attributes[key] = survey.insert_xpaths(value)

if not self.get('flat'):
attrs['ref'] = self.get_xpath()
attributes['ref'] = self.get_xpath()

if 'label' in self and len(self['label']) > 0:
children.append(self.xml_label())
for n in Section.xml_control(self):
children.append(n)

if u"appearance" in control_dict:
attrs['appearance'] = control_dict['appearance']
attributes['appearance'] = control_dict['appearance']

if u"intent" in control_dict:
survey = self.get_root()
attrs['intent'] = survey.insert_xpaths(control_dict['intent'])
attributes['intent'] = survey.insert_xpaths(control_dict['intent'])

return node(u"group", *children, **attrs)
return node(u"group", *children, **attributes)

def to_json_dict(self):
# This is quite hacky, might want to think about a smart way
Expand Down
1 change: 1 addition & 0 deletions pyxform/survey_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SurveyElement(dict):
u"intent": unicode,
u"jr:count" : unicode,
u"bind": dict,
u"instance": dict,
u"control": dict,
u"media": dict,
# this node will also have a parent and children, like a tree!
Expand Down
47 changes: 47 additions & 0 deletions pyxform/tests/attribute_columns_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Some tests for the new (v0.9) spec is properly implemented.
"""
import unittest2 as unittest
import codecs
import os
import sys
# Hack to make sure that pyxform is on the python import path
parentdir = os.path.dirname(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, parentdir)
import pyxform
from pyxform.errors import PyXFormError

DIR = os.path.dirname(__file__)


class attribute_columns_test(unittest.TestCase):

maxDiff = None

def runTest(self):
filename = "attribute_columns_test.xlsx"
path_to_excel_file = os.path.join(DIR, "example_xls", filename)
# Get the xform output path:
root_filename, ext = os.path.splitext(filename)
output_path = os.path.join(DIR, "test_output", root_filename + ".xml")
expected_output_path = os.path.join(DIR, "test_expected_output",
root_filename + ".xml")
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
# print warnings
# Compare with the expected output:
with codecs.open(expected_output_path, 'rb', encoding="utf-8") \
as expected_file:
with codecs.open(output_path, 'rb', encoding="utf-8") \
as actual_file:
self.assertMultiLineEqual(
expected_file.read(), actual_file.read())


if __name__ == '__main__':
unittest.main()
Binary file not shown.
Loading