Example #1
0
 def create_xform(cls, xlsx_file):
     with NamedTemporaryFile(suffix='.xlsx') as tmp:
         tmp.write(xlsx_file.read())
         tmp.seek(0)
         survey = create_survey_from_xls(tmp)
         # survey.title = name
     return survey.to_xml()
Example #2
0
def parse_xls_form(xls_file: typing.BinaryIO,
                   *,
                   previous_version: str = None) -> Survey:
    """Parse an ODK xls form file.

    :param xls_file: a named file-like object (a persistent file or a django uploaded file will do)
    :param previous_version: used to validate the version present in the excel file or to auto-generate it
    """

    try:
        survey = create_survey_from_xls(xls_file, default_name="data")
    except errors.PyXFormError as e:
        raise ParsingError(f"Invalid XLS file: {e}")

    if survey.version == "":
        survey.version = _generate_form_version(previous_version)

    if not survey.version.isnumeric() or len(survey.version) > 10:
        raise ParsingError(
            "Invalid XLS file: Invalid version (must be a string of 1-10 numbers)."
        )

    if previous_version is not None and previous_version.isnumeric(
    ) and int(previous_version) >= int(survey.version):
        raise ParsingError(
            "Invalid XLS file: Parsed version should be greater than previous version."
        )

    return Survey(survey, xls_file_name=xls_file.name)
Example #3
0
 def create_xform(self, name='Test form'):
     with NamedTemporaryFile(suffix='.xlsx') as tmp:
         self.wb.save(tmp.name)
         tmp.seek(0)
         survey = create_survey_from_xls(tmp)
         survey.title = name
     return survey.to_xml()
Example #4
0
 def allow_surveys_with_comment_rows(self):
     """assume that a survey with rows that don't have name, type, or label
     headings raise warning only"""
     path = utils.path_to_text_fixture("allow_comment_rows_test.xls")
     survey = create_survey_from_xls(path)
     expected_dict = {
         "default_language": "default",
         "id_string": "allow_comment_rows_test",
         "children": [
             {
                 "name": "farmer_name",
                 "label": {"English": "First and last name of farmer"},
                 "type": "text",
             }
         ],
         "name": "allow_comment_rows_test",
         "_translations": {
             "English": {
                 "/allow_comment_rows_test/farmer_name:label": {
                     "long": "First and last name of farmer"
                 }
             }
         },
         "title": "allow_comment_rows_test",
         "_xpath": {
             "allow_comment_rows_test": "/allow_comment_rows_test",
             "farmer_name": "/allow_comment_rows_test/farmer_name",
         },
         "type": "survey",
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Example #5
0
def to_json_dict(form_version):  # TODO: remove
    if not form_version.xls_file:
        return None
    try:
        survey = create_survey_from_xls(form_version.xls_file.file)
        return survey.to_json_dict()
    except FileNotFoundError as e:
        print("Failed to form in xls", e)

    return None
Example #6
0
 def save(self, *args, **kwargs):
     if self.xls and not self.xml:
         survey = create_survey_from_xls(self.xls)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
         # set_uuid(self)
         # self._set_uuid_in_xml()
     if not self.version:
         self.version = self.get_version
     super(XformHistory, self).save(*args, **kwargs)
Example #7
0
    def setUp(self):
        self.survey = create_survey_from_xls("parsed_xforms/tests/name_survey.xls")
        self.xform = XForm.objects.create(xml=self.survey.to_xml())
        json_str = json.dumps(self.survey.to_dict())
        self.data_dictionary = DataDictionary.objects.create(xform=self.xform, json=json_str)

        info = {"survey_name": self.survey.get_name(), "id_string": self.survey.id_string(), "name": "Andrew"}
        xml_str = (
            u"<?xml version='1.0' ?><%(survey_name)s id=\"%(id_string)s\"><name>%(name)s</name></%(survey_name)s>"
            % info
        )
        self.instance = Instance.objects.create(xml=xml_str)
        self.parsed_instance = ParsedInstance.objects.get(instance=self.instance)
Example #8
0
def convert_xls_to_xform(xls_file, warnings=False):
    """
    This receives an XLS file object and runs it through
    pyxform to create and validate the survey.
    """
    survey = create_survey_from_xls(xls_file)
    xml = ""
    if warnings:
        with NamedTemporaryFile(suffix='.xml') as named_tmp:
            survey.print_xform_to_file(path=named_tmp.name, validate=True, warnings=warnings)
            named_tmp.seek(0)
            xml = named_tmp.read()
    else:
        xml = survey.to_xml()
    return xml
Example #9
0
    def setUp(self):
        self.survey = create_survey_from_xls("odk_viewer/tests/name_survey.xls")
        self.xform = XForm.objects.create(xml=self.survey.to_xml())
        json_str = json.dumps(self.survey.to_dict())
        self.data_dictionary = DataDictionary.objects.create(
            xform=self.xform, json=json_str)

        info = {
            "survey_name" : self.survey.name,
            "id_string" : self.survey.id_string,
            "name" : "Andrew"
            }
        xml_str = u'<?xml version=\'1.0\' ?><%(survey_name)s id="%(id_string)s"><name>%(name)s</name></%(survey_name)s>' % info
        self.instance = Instance.objects.create(xml=xml_str)
        self.parsed_instance = ParsedInstance.objects.get(instance=self.instance)
Example #10
0
def convert_xls_to_xform(xls_file, warnings=False):
    """
    This receives an XLS file object and runs it through
    pyxform to create and validate the survey.
    """
    survey = create_survey_from_xls(xls_file)
    xml = ""
    if warnings:
        with NamedTemporaryFile(suffix='.xml') as named_tmp:
            survey.print_xform_to_file(path=named_tmp.name,
                                       validate=True,
                                       warnings=warnings)
            named_tmp.seek(0)
            xml = named_tmp.read()
    else:
        xml = survey.to_xml()
    return xml
Example #11
0
    def setUp(self):
        MainTestCase.setUp(self)
        self.survey = create_survey_from_xls(
            "odk_viewer/tests/name_survey.xls")
        json_str = json.dumps(self.survey.to_json_dict())
        self.data_dictionary = DataDictionary.objects.create(
            xml=self.survey.to_xml(), json=json_str, user=self.user)

        info = {
            "survey_name": self.survey.name,
            "id_string": self.survey.id_string,
            "name": "Andrew"
        }
        xml_str = u'<?xml version=\'1.0\' ?><%(survey_name)s id="%(id_string)s"><name>%(name)s</name></%(survey_name)s>' % info
        self.instance = Instance.objects.create(xml=xml_str, user=self.user)
        ParsedInstance.objects.get_or_create(instance=self.instance)
        self.parsed_instance = ParsedInstance.objects.get(
            instance=self.instance)
Example #12
0
    def setUp(self):
        MainTestCase.setUp(self)
        self.survey = create_survey_from_xls(
            "odk_viewer/tests/name_survey.xls")
        json_str = json.dumps(self.survey.to_json_dict())
        self.data_dictionary = DataDictionary.objects.create(
            xml=self.survey.to_xml(), json=json_str, user=self.user)

        info = {
            "survey_name": self.survey.name,
            "id_string": self.survey.id_string,
            "name": "Andrew"
        }
        xml_str = u'<?xml version=\'1.0\' ?><%(survey_name)s'\
            u' id="%(id_string)s"><name>%(name)s</name></%(survey_name)s>'\
            % info
        self.instance = Instance.objects.create(xml=xml_str, user=self.user)
        ParsedInstance.objects.get_or_create(instance=self.instance)
        self.parsed_instance = ParsedInstance.objects.get(
            instance=self.instance)
Example #13
0
 def allow_surveys_with_comment_rows(self):
     """assume that a survey with rows that don't have name, type, or label
     headings raise warning only"""
     path = utils.path_to_text_fixture("allow_comment_rows_test.xls")
     survey = create_survey_from_xls(path)
     expected_dict = {
         "default_language":
         "default",
         "id_string":
         "allow_comment_rows_test",
         "children": [{
             "name": "farmer_name",
             "label": {
                 "English": "First and last name of farmer"
             },
             "type": "text",
         }],
         "name":
         "allow_comment_rows_test",
         "_translations": {
             "English": {
                 "/allow_comment_rows_test/farmer_name:label": {
                     "long": "First and last name of farmer"
                 }
             }
         },
         "title":
         "allow_comment_rows_test",
         "_xpath": {
             "allow_comment_rows_test": "/allow_comment_rows_test",
             "farmer_name": "/allow_comment_rows_test/farmer_name",
         },
         "type":
         "survey",
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Example #14
0
 def setUp(self):
     self.survey = create_survey_from_xls("pyxform/tests/yes_or_no_question.xls")
Example #15
0
 def setUp(self):
     self.survey = create_survey_from_xls(
         utils.path_to_text_fixture("yes_or_no_question.xls"))
Example #16
0
def create_survey_from_csv_text(csv_text):
    with tempfile.NamedTemporaryFile(suffix=".csv") as csv_file:
        csv_file.write(csv_text)
        csv_file.flush()
        return create_survey_from_xls(csv_file.name)
Example #17
0
 def setUp(self):
     self.survey = create_survey_from_xls(
         utils.path_to_text_fixture("yes_or_no_question.xls"))
Example #18
0
 def test_dict_organizer(self):
     self.survey = create_survey_from_xls("odk_viewer/tests/household.xls")
     json_str = json.dumps(self.survey.to_json_dict())
     self.data_dictionary = DataDictionary.objects.create(
         xml=self.survey.to_xml(), json=json_str, user=self.user)
     serious_xml = u'''
     <?xml version='1.0' ?>
     <household id="serious_survey">
       <number_of_members>7</number_of_members>
       <man>
         <name>Alex</name>
       </man>
       <man>
         <name>Bob</name>
       </man>
       <woman>
         <name>Carla</name>
           <child>
             <name>Danny</name>
           </child>
           <child>
             <name>Ed</name>
           </child>
       </woman>
       <woman>
         <name>Fran</name>
         <child>
           <name>Greg</name>
         </child>
       </woman>
       <ice_cream_flavors>
         <number_for_vanilla>5</number_for_vanilla>
         <number_for_strawberry>2</number_for_strawberry>
       </ice_cream_flavors>
     </household>
     '''
     serious_xml = re.sub(r">\s+<", "><", serious_xml)
     d = xform_instance_to_dict(serious_xml, self.data_dictionary)
     dict_organizer = DictOrganizer()
     expected_dict = {
         u'household': [{
             u'_parent_index': -1,
             u'number_of_members': u'7',
             u'_parent_table_name': u'',
             u'_index': 0
         }],
         u'woman': [{
             u'_parent_index': 0,
             u'name': u'Carla',
             u'_parent_table_name': u'household',
             u'_index': 0
         }, {
             u'_parent_index': 0,
             u'name': u'Fran',
             u'_parent_table_name': u'household',
             u'_index': 1
         }],
         u'ice_cream_flavors': [{
             u'_parent_index': 0,
             u'number_for_strawberry': u'2',
             u'number_for_vanilla': u'5',
             u'_parent_table_name': u'household',
             u'_index': 0
         }],
         u'man': [{
             u'_parent_index': 0,
             u'name': u'Alex',
             u'_parent_table_name': u'household',
             u'_index': 0
         }, {
             u'_parent_index': 0,
             u'name': u'Bob',
             u'_parent_table_name': u'household',
             u'_index': 1
         }],
         u'child': [{
             u'_parent_index': 0,
             u'name': u'Danny',
             u'_parent_table_name': u'woman',
             u'_index': 0
         }, {
             u'_parent_index': 0,
             u'name': u'Ed',
             u'_parent_table_name': u'woman',
             u'_index': 1
         }, {
             u'_parent_index': 1,
             u'name': u'Greg',
             u'_parent_table_name': u'woman',
             u'_index': 2
         }]
     }
     self.assertEqual(dict_organizer.get_observation_from_dict(d),
                      expected_dict)
Example #19
0
 def test_dict_organizer(self):
     self.survey = create_survey_from_xls("odk_viewer/tests/household.xls")
     json_str = json.dumps(self.survey.to_json_dict())
     self.data_dictionary = DataDictionary.objects.create(
         xml=self.survey.to_xml(), json=json_str, user = self.user)
     serious_xml = u'''
     <?xml version='1.0' ?>
     <household id="serious_survey">
       <number_of_members>7</number_of_members>
       <man>
         <name>Alex</name>
       </man>
       <man>
         <name>Bob</name>
       </man>
       <woman>
         <name>Carla</name>
           <child>
             <name>Danny</name>
           </child>
           <child>
             <name>Ed</name>
           </child>
       </woman>
       <woman>
         <name>Fran</name>
         <child>
           <name>Greg</name>
         </child>
       </woman>
       <ice_cream_flavors>
         <number_for_vanilla>5</number_for_vanilla>
         <number_for_strawberry>2</number_for_strawberry>
       </ice_cream_flavors>
     </household>
     '''
     serious_xml = re.sub(r">\s+<", "><", serious_xml)
     d = xform_instance_to_dict(serious_xml, self.data_dictionary)
     dict_organizer = DictOrganizer()
     expected_dict = {
         u'household': [
             {
                 u'_parent_index': -1,
                 u'number_of_members': u'7',
                 u'_parent_table_name':
                 u'', u'_index': 0
             }
         ],
         u'woman': [
             {
                 u'_parent_index': 0,
                 u'name': u'Carla',
                 u'_parent_table_name': u'household',
                 u'_index': 0
             },
             {
                 u'_parent_index': 0,
                 u'name': u'Fran',
                 u'_parent_table_name': u'household',
                 u'_index': 1
             }
         ],
         u'ice_cream_flavors': [
             {
                 u'_parent_index': 0,
                 u'number_for_strawberry': u'2',
                 u'number_for_vanilla': u'5',
                 u'_parent_table_name': u'household',
                 u'_index': 0
             }
         ],
         u'man': [
             {
                 u'_parent_index': 0,
                 u'name': u'Alex',
                 u'_parent_table_name': u'household',
                 u'_index': 0
             },
             {
                 u'_parent_index': 0,
                 u'name': u'Bob',
                 u'_parent_table_name': u'household',
                 u'_index': 1
             }
         ],
         u'child': [
             {
                 u'_parent_index': 0,
                 u'name': u'Danny',
                 u'_parent_table_name': u'woman',
                 u'_index': 0
             },
             {
                 u'_parent_index': 0,
                 u'name': u'Ed',
                 u'_parent_table_name': u'woman',
                 u'_index': 1
             },
             {
                 u'_parent_index': 1,
                 u'name': u'Greg',
                 u'_parent_table_name': u'woman',
                 u'_index': 2
             }
         ]
     }
     self.assertEqual(
         dict_organizer.get_observation_from_dict(d),
         expected_dict
         )