def setUp(self): self.user = User.objects.create(username="******", email="*****@*****.**") self.user.set_password("pass") self.xform1 = DataDictionary() self.xform1.user = self.user self.xform1.json = """ {"id_string": "yes_or_no", "children": [{"name": "yesno", "label": "Yes or no?", "type": "text"}], "name": "yes_or_no", "title": "yes_or_no", "type": "survey"} """.strip() self.xform2 = DataDictionary() self.xform2.user = self.user self.xform2.json = """ {"id_string": "start_time", "children": [{"name": "start_time", "type": "start"}], "name": "start_time", "title": "start_time", "type": "survey"} """.strip() def get_xml_for_form(xform): builder = SurveyElementBuilder() sss = builder.create_survey_element_from_json(xform.json) xform.xml = sss.to_xml() xform._mark_start_time_boolean() xform.save() get_xml_for_form(self.xform1) get_xml_for_form(self.xform2)
def publish_xml_form(xml_file, user, id_string=None): xml = xml_file.read() survey = create_survey_element_from_xml(xml) form_json = survey.to_json() if id_string: dd = DataDictionary.objects.get(user=user, id_string=id_string) dd.xml = xml dd.json = form_json dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml() dd.save() return dd else: dd = DataDictionary(user=user, xml=xml, json=form_json) dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml(file_name=xml_file.name) dd.save() return dd
def setUp(self): self.user = User.objects.create(username="******", email="*****@*****.**") self.user.set_password("pass") self.xform1 = DataDictionary() self.xform1.user = self.user self.xform1.json = """ {"id_string": "yes_or_no", "children": [{"name": "yesno", "label": "Yes or no?", "type": "text"}], "name": "yes_or_no", "title": "yes_or_no", "type": "survey"} """.strip() self.xform2 = DataDictionary() self.xform2.user = self.user self.xform2.json = """ {"id_string": "start_time", "children": [{"name": "start_time", "type": "start"}], "name": "start_time", "title": "start_time", "type": "survey"} """.strip() def get_xml_for_form(xform): builder = SurveyElementBuilder()#question_type_dictionary=qtd) sss = builder.create_survey_element_from_json(xform.json) xform.xml = sss.to_xml() xform._mark_start_time_boolean() xform.save() get_xml_for_form(self.xform1) get_xml_for_form(self.xform2)
def build_sections( current_section, survey_element, sections, select_multiples, gps_fields, encoded_fields, field_delimiter='/'): for child in survey_element.children: current_section_name = current_section['name'] # if a section, recurs if isinstance(child, Section): # if its repeating, build a new section if isinstance(child, RepeatingSection): # section_name in recursive call changes section = { 'name': child.get_abbreviated_xpath(), 'elements': []} self.sections.append(section) build_sections( section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter) else: # its a group, recurs using the same section build_sections( current_section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter) elif isinstance(child, Question) and child.bind.get(u"type")\ not in QUESTION_TYPES_TO_EXCLUDE: # add to survey_sections if isinstance(child, Question): child_xpath = child.get_abbreviated_xpath() current_section['elements'].append({ 'title': ExportBuilder.format_field_title( child.get_abbreviated_xpath(), field_delimiter), 'xpath': child_xpath, 'type': child.bind.get(u"type") }) if _is_invalid_for_mongo(child_xpath): if current_section_name not in encoded_fields: encoded_fields[current_section_name] = {} encoded_fields[current_section_name].update( {child_xpath: _encode_for_mongo(child_xpath)}) # if its a select multiple, make columns out of its choices if child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE\ and self.SPLIT_SELECT_MULTIPLES: current_section['elements'].extend( [{ 'title': ExportBuilder.format_field_title( c.get_abbreviated_xpath(), field_delimiter), 'xpath': c.get_abbreviated_xpath(), 'type': 'string' } for c in child.children]) _append_xpaths_to_section( current_section_name, select_multiples, child.get_abbreviated_xpath(), [c.get_abbreviated_xpath() for c in child.children]) # split gps fields within this section if child.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components xpaths = DataDictionary.get_additional_geopoint_xpaths( child.get_abbreviated_xpath()) current_section['elements'].extend( [ { 'title': ExportBuilder.format_field_title( xpath, field_delimiter), 'xpath': xpath, 'type': 'decimal' } for xpath in xpaths ]) _append_xpaths_to_section( current_section_name,gps_fields, child.get_abbreviated_xpath(), xpaths)
class TestSimpleSubmission(TestCase): def setUp(self): self.user = User.objects.create(username="******", email="*****@*****.**") self.user.set_password("pass") self.xform1 = DataDictionary() self.xform1.user = self.user self.xform1.json = """ {"id_string": "yes_or_no", "children": [{"name": "yesno", "label": "Yes or no?", "type": "text"}], "name": "yes_or_no", "title": "yes_or_no", "type": "survey"} """.strip() self.xform2 = DataDictionary() self.xform2.user = self.user self.xform2.json = """ {"id_string": "start_time", "children": [{"name": "start_time", "type": "start"}], "name": "start_time", "title": "start_time", "type": "survey"} """.strip() def get_xml_for_form(xform): builder = SurveyElementBuilder()#question_type_dictionary=qtd) sss = builder.create_survey_element_from_json(xform.json) xform.xml = sss.to_xml() xform._mark_start_time_boolean() xform.save() get_xml_for_form(self.xform1) get_xml_for_form(self.xform2) def tearDown(self): self.xform1.delete() self.user.delete() def test_start_time_boolean_properly_set(self): self.assertTrue(self.xform1.has_start_time == False) self.assertTrue(self.xform2.has_start_time == True) def test_simple_yes_submission(self): def submit_simple_yes(): create_instance(self.user.username, TempFileProxy(""" <?xml version='1.0' ?><yes_or_no id="yes_or_no"><yesno>Yes</yesno></yes_or_no> """.strip()), []) self.assertEquals(0, self.xform1.surveys.count()) submit_simple_yes() self.assertEquals(1, self.xform1.surveys.count()) # a simple "yes" submission *SHOULD* increment the survey count submit_simple_yes() self.assertEquals(2, self.xform1.surveys.count()) def test_start_time_submissions(self): """ This test checks to make sure that surveys *with start_time available* are marked as duplicates when the XML is a direct match. """ def submit_at_hour(hour): st_xml = """ <?xml version='1.0' ?><start_time id="start_time"><start_time>2012-01-11T%d:00:00.000</start_time></start_time> """.strip() % hour create_instance(self.user.username, TempFileProxy(st_xml), []) self.assertEquals(0, self.xform2.surveys.count()) submit_at_hour(11) self.assertEquals(1, self.xform2.surveys.count()) submit_at_hour(12) self.assertEquals(2, self.xform2.surveys.count()) # an instance from 11 AM already exists in the database, so it *SHOULD NOT* increment the survey count. submit_at_hour(11) self.assertEquals(2, self.xform2.surveys.count())
class TestSimpleSubmission(TestCase): def setUp(self): self.user = User.objects.create(username="******", email="*****@*****.**") self.user.set_password("pass") self.xform1 = DataDictionary() self.xform1.user = self.user self.xform1.json = """ {"id_string": "yes_or_no", "children": [{"name": "yesno", "label": "Yes or no?", "type": "text"}], "name": "yes_or_no", "title": "yes_or_no", "type": "survey"} """.strip() self.xform2 = DataDictionary() self.xform2.user = self.user self.xform2.json = """ {"id_string": "start_time", "children": [{"name": "start_time", "type": "start"}], "name": "start_time", "title": "start_time", "type": "survey"} """.strip() def get_xml_for_form(xform): builder = SurveyElementBuilder() #question_type_dictionary=qtd) sss = builder.create_survey_element_from_json(xform.json) xform.xml = sss.to_xml() xform._mark_start_time_boolean() xform.save() get_xml_for_form(self.xform1) get_xml_for_form(self.xform2) def tearDown(self): self.xform1.delete() self.user.delete() def test_start_time_boolean_properly_set(self): self.assertTrue(self.xform1.has_start_time == False) self.assertTrue(self.xform2.has_start_time == True) def test_simple_yes_submission(self): def submit_simple_yes(): create_instance( self.user.username, TempFileProxy(""" <?xml version='1.0' ?><yes_or_no id="yes_or_no"><yesno>Yes</yesno></yes_or_no> """.strip()), []) self.assertEquals(0, self.xform1.surveys.count()) submit_simple_yes() self.assertEquals(1, self.xform1.surveys.count()) # a simple "yes" submission *SHOULD* increment the survey count submit_simple_yes() self.assertEquals(2, self.xform1.surveys.count()) def test_start_time_submissions(self): """ This test checks to make sure that surveys *with start_time available* are marked as duplicates when the XML is a direct match. """ def submit_at_hour(hour): st_xml = """ <?xml version='1.0' ?><start_time id="start_time"><start_time>2012-01-11T%d:00:00.000</start_time></start_time> """.strip() % hour create_instance(self.user.username, TempFileProxy(st_xml), []) self.assertEquals(0, self.xform2.surveys.count()) submit_at_hour(11) self.assertEquals(1, self.xform2.surveys.count()) submit_at_hour(12) self.assertEquals(2, self.xform2.surveys.count()) # an instance from 11 AM already exists in the database, so it *SHOULD NOT* increment the survey count. submit_at_hour(11) self.assertEquals(2, self.xform2.surveys.count())
def build_sections(current_section, survey_element, sections, select_multiples, gps_fields, encoded_fields, field_delimiter='/'): for child in survey_element.children: current_section_name = current_section['name'] # if a section, recurs if isinstance(child, Section): # if its repeating, build a new section if isinstance(child, RepeatingSection): # section_name in recursive call changes section = { 'name': child.get_abbreviated_xpath(), 'elements': [] } self.sections.append(section) build_sections(section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter) else: # its a group, recurs using the same section build_sections(current_section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter) elif isinstance(child, Question) and child.bind.get(u"type")\ not in QUESTION_TYPES_TO_EXCLUDE: # add to survey_sections if isinstance(child, Question): child_xpath = child.get_abbreviated_xpath() current_section['elements'].append({ 'title': ExportBuilder.format_field_title( child.get_abbreviated_xpath(), field_delimiter), 'xpath': child_xpath, 'type': child.bind.get(u"type") }) if _is_invalid_for_mongo(child_xpath): if current_section_name not in encoded_fields: encoded_fields[current_section_name] = {} encoded_fields[current_section_name].update( {child_xpath: _encode_for_mongo(child_xpath)}) # if its a select multiple, make columns out of its choices if child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE\ and self.SPLIT_SELECT_MULTIPLES: current_section['elements'].extend([{ 'title': ExportBuilder.format_field_title( c.get_abbreviated_xpath(), field_delimiter), 'xpath': c.get_abbreviated_xpath(), 'type': 'string' } for c in child.children]) _append_xpaths_to_section( current_section_name, select_multiples, child.get_abbreviated_xpath(), [ c.get_abbreviated_xpath() for c in child.children ]) # split gps fields within this section if child.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components xpaths = DataDictionary.get_additional_geopoint_xpaths( child.get_abbreviated_xpath()) current_section['elements'].extend([{ 'title': ExportBuilder.format_field_title( xpath, field_delimiter), 'xpath': xpath, 'type': 'decimal' } for xpath in xpaths]) _append_xpaths_to_section( current_section_name, gps_fields, child.get_abbreviated_xpath(), xpaths)