Exemplo n.º 1
0
def process_xlsform(filepath):

    '''Genrates a pyxform survey object from a filepath or file object'''

    survey = None
    if isinstance(filepath, File):
        survey = create_survey_from_xls(filepath)
    else:
        with open(filepath) as f:
            survey = create_survey_from_xls(f)
    return survey
Exemplo n.º 2
0
    def test_child_record_parent_table_is_updated_when_sheet_is_renamed(self):
        survey = create_survey_from_xls(_logger_fixture_path(
            'childrens_survey_with_a_very_long_name.xls'))
        export_builder = ExportBuilder()
        export_builder.set_survey(survey)
        xls_file = NamedTemporaryFile(suffix='.xlsx')
        filename = xls_file.name
        export_builder.to_xls_export(filename, self.long_survey_data)
        xls_file.seek(0)
        wb = load_workbook(filename)

        # get the children's sheet
        ws1 = wb.get_sheet_by_name('childrens_survey_with_a_very_l1')

        # parent_table is in cell K2
        parent_table_name = ws1.cell('K2').value
        expected_parent_table_name = 'childrens_survey_with_a_very_lo'
        self.assertEqual(parent_table_name, expected_parent_table_name)

        # get cartoons sheet
        ws2 = wb.get_sheet_by_name('childrens_survey_with_a_very_l2')
        parent_table_name = ws2.cell('G2').value
        expected_parent_table_name = 'childrens_survey_with_a_very_l1'
        self.assertEqual(parent_table_name, expected_parent_table_name)
        xls_file.close()
Exemplo n.º 3
0
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
     super(DataDictionary, self).save(*args, **kwargs)
Exemplo n.º 4
0
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
     super(DataDictionary, self).save(*args, **kwargs)
Exemplo n.º 5
0
    def test_child_record_parent_table_is_updated_when_sheet_is_renamed(self):
        survey = create_survey_from_xls(
            _logger_fixture_path('childrens_survey_with_a_very_long_name.xls'))
        export_builder = ExportBuilder()
        export_builder.set_survey(survey)
        xls_file = NamedTemporaryFile(suffix='.xlsx')
        filename = xls_file.name
        export_builder.to_xls_export(filename, self.long_survey_data)
        xls_file.seek(0)
        wb = load_workbook(filename)

        # get the children's sheet
        ws1 = wb.get_sheet_by_name('childrens_survey_with_a_very_l1')

        # parent_table is in cell K2
        parent_table_name = ws1.cell('K2').value
        expected_parent_table_name = 'childrens_survey_with_a_very_lo'
        self.assertEqual(parent_table_name, expected_parent_table_name)

        # get cartoons sheet
        ws2 = wb.get_sheet_by_name('childrens_survey_with_a_very_l2')
        parent_table_name = ws2.cell('G2').value
        expected_parent_table_name = 'childrens_survey_with_a_very_l1'
        self.assertEqual(parent_table_name, expected_parent_table_name)
        xls_file.close()
Exemplo n.º 6
0
def build_form_labels(xlsform_path):
    # read XLSForm and feed PyXForm
    with open(xlsform_path, "rb") as f:
        xlsform = create_survey_from_xls(f)

    # retrive JSONForm
    jsonform = xlsform.to_json_dict()

    # we'll want labels as {name: label}
    labelslist2dict = lambda l: {e["name"]: e["label"] for e in l}

    labels = {}

    def walk(node):
        for item in node:
            if isinstance(item, dict):
                if (
                    "type" in item
                    and item["type"].startswith("select ")
                    and "external" not in item["type"]
                ):
                    labels.update({item["name"]: labelslist2dict(item["children"])})
                if "children" in item:
                    walk(item["children"])
                else:
                    walk(item)
            else:
                pass

    # walk through jsonform tree to find select x questions
    walk(jsonform["children"])

    return labels
Exemplo n.º 7
0
 def test_for_loop(self):
     path = utils.path_to_text_fixture('for_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     try:
         actual_xml = survey.to_xml()
     except ValidationError:
         self.fail("survey.to_xml() raised ValidationError.")
Exemplo n.º 8
0
 def test_for_loop(self):
     path = utils.path_to_text_fixture('for_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     try:
         actual_xml = survey.to_xml()
     except ValidationError:
         self.fail("survey.to_xml() raised ValidationError.")
Exemplo n.º 9
0
    def test_type_conversion(self):
        submission_1 = {
            "_id": 579827,
            "geolocation": "-1.2625482 36.7924794 0.0 21.0",
            "_bamboo_dataset_id": "",
            "meta/instanceID": "uuid:2a8129f5-3091-44e1-a579-bed2b07a12cf",
            "name": "Smith",
            "formhub/uuid": "633ec390e024411ba5ce634db7807e62",
            "_submission_time": "2013-07-03T08:25:30",
            "age": "107",
            "_uuid": "2a8129f5-3091-44e1-a579-bed2b07a12cf",
            "when": "2013-07-03",
            "amount": "250.0",
            "_geolocation": [
                "-1.2625482",
                "36.7924794"
            ],
            "_xform_id_string": "test_data_types",
            "_userform_id": "larryweya_test_data_types",
            "_status": "submitted_via_web",
            "precisely": "2013-07-03T15:24:00.000+03",
            "really": "15:24:00.000+03"
        }

        submission_2 = {
            "_id": 579828,
            "_submission_time": "2013-07-03T08:26:10",
            "_uuid": "5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
            "_bamboo_dataset_id": "",
            "_xform_id_string": "test_data_types",
            "_userform_id": "larryweya_test_data_types",
            "_status": "submitted_via_web",
            "meta/instanceID": "uuid:5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
            "formhub/uuid": "633ec390e024411ba5ce634db7807e62",
            "amount": "",
        }

        survey = create_survey_from_xls(viewer_fixture_path(
            'test_data_types/test_data_types.xls'))
        export_builder = ExportBuilder()
        export_builder.set_survey(survey)
        # format submission 1 for export
        survey_name = survey.name
        indices = {survey_name: 0}
        data = dict_to_joined_export(submission_1, 1, indices, survey_name)
        new_row = export_builder.pre_process_row(data[survey_name],
                                                 export_builder.sections[0])
        self.assertIsInstance(new_row['age'], int)
        self.assertIsInstance(new_row['when'], datetime.date)
        self.assertIsInstance(new_row['amount'], float)

        # check missing values dont break and empty values return blank strings
        indices = {survey_name: 0}
        data = dict_to_joined_export(submission_2, 1, indices, survey_name)
        new_row = export_builder.pre_process_row(data[survey_name],
                                                 export_builder.sections[0])
        self.assertIsInstance(new_row['amount'], basestring)
        self.assertEqual(new_row['amount'], '')
Exemplo n.º 10
0
    def test_type_conversion(self):
        submission_1 = {
            "_id": 579827,
            "geolocation": "-1.2625482 36.7924794 0.0 21.0",
            "_bamboo_dataset_id": "",
            "meta/instanceID": "uuid:2a8129f5-3091-44e1-a579-bed2b07a12cf",
            "name": "Smith",
            "formhub/uuid": "633ec390e024411ba5ce634db7807e62",
            "_submission_time": "2013-07-03T08:25:30",
            "age": "107",
            "_uuid": "2a8129f5-3091-44e1-a579-bed2b07a12cf",
            "when": "2013-07-03",
            "amount": "250.0",
            "_geolocation": [
                "-1.2625482",
                "36.7924794"
            ],
            "_xform_id_string": "test_data_types",
            "_userform_id": "larryweya_test_data_types",
            "_status": "submitted_via_web",
            "precisely": "2013-07-03T15:24:00.000+03",
            "really": "15:24:00.000+03"
        }

        submission_2 = {
            "_id": 579828,
            "_submission_time": "2013-07-03T08:26:10",
            "_uuid": "5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
            "_bamboo_dataset_id": "",
            "_xform_id_string": "test_data_types",
            "_userform_id": "larryweya_test_data_types",
            "_status": "submitted_via_web",
            "meta/instanceID": "uuid:5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
            "formhub/uuid": "633ec390e024411ba5ce634db7807e62",
            "amount": "",
        }

        survey = create_survey_from_xls(viewer_fixture_path(
            'test_data_types/test_data_types.xls'))
        export_builder = ExportBuilder()
        export_builder.set_survey(survey)
        # format submission 1 for export
        survey_name = survey.name
        indices = {survey_name: 0}
        data = dict_to_joined_export(submission_1, 1, indices, survey_name)
        new_row = export_builder.pre_process_row(data[survey_name],
                                                 export_builder.sections[0])
        self.assertIsInstance(new_row['age'], int)
        self.assertIsInstance(new_row['when'], datetime.date)
        self.assertIsInstance(new_row['amount'], float)

        # check missing values dont break and empty values return blank strings
        indices = {survey_name: 0}
        data = dict_to_joined_export(submission_2, 1, indices, survey_name)
        new_row = export_builder.pre_process_row(data[survey_name],
                                                 export_builder.sections[0])
        self.assertIsInstance(new_row['amount'], basestring)
        self.assertEqual(new_row['amount'], '')
Exemplo n.º 11
0
 def test_xls_convert_dates_before_1900(self):
     survey = create_survey_from_xls(viewer_fixture_path("test_data_types/test_data_types.xls"))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     data = [{"name": "Abe", "when": "1899-07-03"}]
     # create export file
     temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
     export_builder.to_xls_export(temp_xls_file.name, data)
     temp_xls_file.close()
Exemplo n.º 12
0
 def handle(self, *args, **options):
     file_path = "/home/xls/a2bc3p3qUsADD2MDedAdSF"
     xls_file = open(file_path + ".csv")
     survey = create_survey_from_xls(xls_file)
     xml = survey.to_xml()
     # xml = xml.encode('ascii', 'ignore').decode('ascii')
     print(xml)
     # f = open(file_path +".xml", "w+")
     # f.write(xml)
     # f.close()
     self.stdout.write('Successfully created xml ')
Exemplo n.º 13
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         survey = self._check_version_set(create_survey_from_xls(self.xls))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemplo n.º 14
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         survey = self._check_version_set(create_survey_from_xls(self.xls))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemplo n.º 15
0
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         survey.update({
             'name': survey.id_string,
         })
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
         set_uuid(self)
         self.set_uuid_in_xml(id_string=survey.id_string)
     super().save(*args, **kwargs)
Exemplo n.º 16
0
 def test_zipped_csv_export_works_with_unicode(self):
     """
     cvs writer doesnt handle unicode we we have to encode to ascii
     """
     survey = create_survey_from_xls(_logger_fixture_path(
         'childrens_survey_unicode.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     temp_zip_file = NamedTemporaryFile(suffix='.zip')
     export_builder.to_zipped_csv(temp_zip_file.name, self.data_utf8)
     temp_zip_file.seek(0)
     temp_dir = tempfile.mkdtemp()
     zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
     zip_file.extractall(temp_dir)
     zip_file.close()
     temp_zip_file.close()
     # check that the children's file (which has the unicode header) exists
     self.assertTrue(
         os.path.exists(
             os.path.join(temp_dir, "children.info.csv")))
     # check file's contents
     with open(os.path.join(temp_dir, "children.info.csv")) as csv_file:
         reader = csv.reader(csv_file)
         expected_headers = ['children.info/name.first',
                             'children.info/age',
                             'children.info/fav_colors',
                             u'children.info/fav_colors/red\u2019s',
                             u'children.info/fav_colors/blue\u2019s',
                             u'children.info/fav_colors/pink\u2019s',
                             'children.info/ice_creams',
                             'children.info/ice_creams/vanilla',
                             'children.info/ice_creams/strawberry',
                             'children.info/ice_creams/chocolate', '_id',
                             '_uuid', '_submission_time', '_index',
                             '_parent_table_name', '_parent_index',
                             u'_tags', '_notes', '_version',
                             '_duration', '_submitted_by']
         rows = [row for row in reader]
         actual_headers = [h.decode('utf-8') for h in rows[0]]
         self.assertEqual(sorted(actual_headers), sorted(expected_headers))
         data = dict(zip(rows[0], rows[1]))
         self.assertEqual(
             data[u'children.info/fav_colors/red\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/blue\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/pink\u2019s'.encode('utf-8')],
             'False')
         # check that red and blue are set to true
     shutil.rmtree(temp_dir)
Exemplo n.º 17
0
    def handle(self, *args, **options):
        try:
            xls_filepath = options['xls_filepath']
        except KeyError:
            raise CommandError(_("You must provide the path to the xls file."))
        # make sure path exists
        if not os.path.exists(xls_filepath):
            raise CommandError(
                _("The xls file '%s' does not exist.") % xls_filepath)

        try:
            username = options['username']
        except KeyError:
            raise CommandError(
                _("You must provide the username to publish the form to."))
        # make sure user exists
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            raise CommandError(_("The user '%s' does not exist.") % username)

        # wasteful but we need to get the id_string beforehand
        survey = create_survey_from_xls(xls_filepath)

        # check if a form with this id_string exists for this user
        form_already_exists = XForm.objects.filter(
            user=user, id_string=survey.id_string).count() > 0

        # id_string of form to replace, if any
        id_string = None
        if form_already_exists:
            if 'replace' in options and options['replace']:
                id_string = survey.id_string
                self.stdout.write(_("Form already exist, replacing ..\n"))
            else:
                raise CommandError(
                    _("The form with id_string '%s' already exists, use the -r"
                      " option to replace it.") % survey.id_string)
        else:
            self.stdout.write(_("Form does NOT exist, publishing ..\n"))

        try:
            project_name = options['project_name']
            project = Project.objects.get(name=project_name)
        except (KeyError, Project.DoesNotExist):
            project = get_user_default_project(user)

        # publish
        xls_file = django_file(xls_filepath, 'xls_file',
                               'application/vnd.ms-excel')
        publish_xls_form(xls_file, user, project, id_string)
        self.stdout.write(_("Done..\n"))
Exemplo n.º 18
0
    def handle(self, *args, **options):
        try:
            xls_filepath = options['xls_filepath']
        except KeyError:
            raise CommandError(_("You must provide the path to the xls file."))
        # make sure path exists
        if not os.path.exists(xls_filepath):
            raise CommandError(
                _("The xls file '%s' does not exist.") % xls_filepath)

        try:
            username = options['username']
        except KeyError:
            raise CommandError(
                _("You must provide the username to publish the form to."))
        # make sure user exists
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            raise CommandError(_("The user '%s' does not exist.") % username)

        # wasteful but we need to get the id_string beforehand
        survey = create_survey_from_xls(xls_filepath)

        # check if a form with this id_string exists for this user
        form_already_exists = XForm.objects.filter(
            user=user, id_string=survey.id_string).count() > 0

        # id_string of form to replace, if any
        id_string = None
        if form_already_exists:
            if 'replace' in options and options['replace']:
                id_string = survey.id_string
                self.stdout.write(_("Form already exist, replacing ..\n"))
            else:
                raise CommandError(
                    _("The form with id_string '%s' already exists, use the -r"
                      " option to replace it.") % survey.id_string)
        else:
            self.stdout.write(_("Form does NOT exist, publishing ..\n"))

        try:
            project_name = options['project_name']
            project = Project.objects.get(name=project_name)
        except (KeyError, Project.DoesNotExist):
            project = get_user_default_project(user)

        # publish
        xls_file = django_file(xls_filepath, 'xls_file',
                               'application/vnd.ms-excel')
        publish_xls_form(xls_file, user, project, id_string)
        self.stdout.write(_("Done..\n"))
Exemplo n.º 19
0
 def test_xls_convert_dates_before_1900(self):
     survey = create_survey_from_xls(
         viewer_fixture_path('test_data_types/test_data_types.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     data = [{
         'name': 'Abe',
         'when': '1899-07-03',
     }]
     # create export file
     temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
     export_builder.to_xls_export(temp_xls_file.name, data)
     temp_xls_file.close()
Exemplo n.º 20
0
 def test_zipped_csv_export_works_with_unicode(self):
     """
     cvs writer doesnt handle unicode we we have to encode to ascii
     """
     survey = create_survey_from_xls(_logger_fixture_path(
         'childrens_survey_unicode.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     temp_zip_file = NamedTemporaryFile(suffix='.zip')
     export_builder.to_zipped_csv(temp_zip_file.name, self.data_utf8)
     temp_zip_file.seek(0)
     temp_dir = tempfile.mkdtemp()
     zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
     zip_file.extractall(temp_dir)
     zip_file.close()
     temp_zip_file.close()
     # check that the children's file (which has the unicode header) exists
     self.assertTrue(
         os.path.exists(
             os.path.join(temp_dir, "children.info.csv")))
     # check file's contents
     with open(os.path.join(temp_dir, "children.info.csv")) as csv_file:
         reader = csv.reader(csv_file)
         expected_headers = ['children.info/name.first',
                             'children.info/age',
                             'children.info/fav_colors',
                             u'children.info/fav_colors/red\u2019s',
                             u'children.info/fav_colors/blue\u2019s',
                             u'children.info/fav_colors/pink\u2019s',
                             'children.info/ice_creams',
                             'children.info/ice_creams/vanilla',
                             'children.info/ice_creams/strawberry',
                             'children.info/ice_creams/chocolate', '_id',
                             '_uuid', '_submission_time', '_index',
                             '_parent_table_name', '_parent_index',
                             u'_tags', '_notes']
         rows = [row for row in reader]
         actual_headers = [h.decode('utf-8') for h in rows[0]]
         self.assertEqual(sorted(actual_headers), sorted(expected_headers))
         data = dict(zip(rows[0], rows[1]))
         self.assertEqual(
             data[u'children.info/fav_colors/red\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/blue\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/pink\u2019s'.encode('utf-8')],
             'False')
         # check that red and blue are set to true
     shutil.rmtree(temp_dir)
Exemplo n.º 21
0
    def test_sav_special_char_columns(self):
        survey = create_survey_from_xls(
            _logger_fixture_path('grains/grains.xls'))
        export_builder = ExportBuilder()
        export_builder.TRUNCATE_GROUP_TITLE = True
        export_builder.set_survey(survey)
        export_builder.INCLUDE_LABELS = True
        export_builder.set_survey(survey)

        for sec in export_builder.sections:
            sav_options = export_builder._get_sav_options(sec['elements'])
            sav_file = NamedTemporaryFile(suffix=".sav")
            # No exception is raised
            SavWriter(sav_file.name, **sav_options)
Exemplo n.º 22
0
    def test_sav_special_char_columns(self):
        survey = create_survey_from_xls(
            _logger_fixture_path('grains/grains.xls'))
        export_builder = ExportBuilder()
        export_builder.TRUNCATE_GROUP_TITLE = True
        export_builder.set_survey(survey)
        export_builder.INCLUDE_LABELS = True
        export_builder.set_survey(survey)

        for sec in export_builder.sections:
            sav_options = export_builder._get_sav_options(sec['elements'])
            sav_file = NamedTemporaryFile(suffix=".sav")
            # No exception is raised
            SavWriter(sav_file.name, **sav_options)
Exemplo n.º 23
0
 def test_xls_export_works_with_unicode(self):
     survey = create_survey_from_xls(_logger_fixture_path("childrens_survey_unicode.xls"))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
     export_builder.to_xls_export(temp_xls_file.name, self.data_utf8)
     temp_xls_file.seek(0)
     # check that values for red\u2019s and blue\u2019s are set to true
     wb = load_workbook(temp_xls_file.name)
     children_sheet = wb.get_sheet_by_name("children.info")
     data = dict([(r[0].value, r[1].value) for r in children_sheet.columns])
     self.assertTrue(data[u"children.info/fav_colors/red\u2019s"])
     self.assertTrue(data[u"children.info/fav_colors/blue\u2019s"])
     self.assertFalse(data[u"children.info/fav_colors/pink\u2019s"])
     temp_xls_file.close()
Exemplo n.º 24
0
 def test_xls_convert_dates_before_1900(self):
     survey = create_survey_from_xls(viewer_fixture_path(
         'test_data_types/test_data_types.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     data = [
         {
             'name': 'Abe',
             'when': '1899-07-03',
         }
     ]
     # create export file
     temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
     export_builder.to_xls_export(temp_xls_file.name, data)
     temp_xls_file.close()
Exemplo n.º 25
0
 def test_xls_export_works_with_unicode(self):
     survey = create_survey_from_xls(
         _logger_fixture_path('childrens_survey_unicode.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
     export_builder.to_xls_export(temp_xls_file.name, self.data_utf8)
     temp_xls_file.seek(0)
     # check that values for red\u2019s and blue\u2019s are set to true
     wb = load_workbook(temp_xls_file.name)
     children_sheet = wb.get_sheet_by_name("children.info")
     data = dict([(r[0].value, r[1].value) for r in children_sheet.columns])
     self.assertTrue(data['children.info/fav_colors/red\u2019s'])
     self.assertTrue(data['children.info/fav_colors/blue\u2019s'])
     self.assertFalse(data['children.info/fav_colors/pink\u2019s'])
     temp_xls_file.close()
Exemplo n.º 26
0
 def test_to_xls_export_generates_valid_sheet_names(self):
     survey = create_survey_from_xls(_logger_fixture_path(
         'childrens_survey_with_a_very_long_name.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     xls_file = NamedTemporaryFile(suffix='.xls')
     filename = xls_file.name
     export_builder.to_xls_export(filename, self.data)
     xls_file.seek(0)
     wb = load_workbook(filename)
     # check that we have childrens_survey, children, children_cartoons
     # and children_cartoons_characters sheets
     expected_sheet_names = ['childrens_survey_with_a_very_lo',
                             'childrens_survey_with_a_very_l1',
                             'childrens_survey_with_a_very_l2',
                             'childrens_survey_with_a_very_l3']
     self.assertEqual(wb.get_sheet_names(), expected_sheet_names)
     xls_file.close()
Exemplo n.º 27
0
 def test_to_xls_export_generates_valid_sheet_names(self):
     survey = create_survey_from_xls(_logger_fixture_path(
         'childrens_survey_with_a_very_long_name.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     xls_file = NamedTemporaryFile(suffix='.xls')
     filename = xls_file.name
     export_builder.to_xls_export(filename, self.data)
     xls_file.seek(0)
     wb = load_workbook(filename)
     # check that we have childrens_survey, children, children_cartoons
     # and children_cartoons_characters sheets
     expected_sheet_names = ['childrens_survey_with_a_very_lo',
                             'childrens_survey_with_a_very_l1',
                             'childrens_survey_with_a_very_l2',
                             'childrens_survey_with_a_very_l3']
     self.assertEqual(wb.get_sheet_names(), expected_sheet_names)
     xls_file.close()
Exemplo n.º 28
0
    def handle(self, *args, **options):
        # xls_directory = "/home/xls"
        xls_directory = options['directory']
        error_file_list = []
        # csv_to_xls(xls_directory)
        for filename in os.listdir(xls_directory):
            if os.path.isfile(os.path.join(xls_directory, filename)):
                if filename.endswith(".xls"):
                    pass
                elif filename.endswith(".csv"):
                    pass
                else:
                    print("##########################")
                    print("##########################")
                    print("Differentt format file", filename)
                    print("##########################")
                    print("##########################")
                    continue

            xls_file = open(os.path.join(xls_directory, filename))
            print("creating survey for ", xls_file)
            try:
                if filename.endswith(".csv"):
                    csv_file = open(os.path.join(xls_directory, filename))
                    bytes_io = csv_to_xls(csv_file)
                    with open(xls_directory + '' + filename.replace('.csv', '.xls'), 'wb') as f:
                        copy_filelike_to_filelike(bytes_io, f)
                        f.close()
                    xls_file = open(xls_directory + '' + filename.replace('.csv', '.xls'), 'r')

                survey = create_survey_from_xls(xls_file)
                xml = survey.to_xml()
                version = get_version(xml)
                id_string = get_id_string(xml)
            except Exception as e:
                error_file_list.append(filename)
                pass

            else:
                xls_file.close()

            # print("version =  ======", version)
            if not XForm.objects.filter(id_string=id_string).exists():
                print("xform with id string not found ", id_string)
                continue
            xform = XForm.objects.get(id_string=id_string)
            xform_version = get_version(xform.xml)
            if version == xform_version:
                print("##########################")
                print("##########################")
                print("this file is current version of Xform", filename, "Ignored")
                print("##########################")
                print("##########################")
                continue
            if not XformHistory.objects.filter(xform=xform, version=version).exists():
                print("creating history from file ", filename)
                if filename.endswith('.csv'):
                    file_obj = open(xls_directory + '' + filename.replace('.csv', '.xls'), 'r')
                else:
                    file_obj = open(os.path.join(xls_directory, filename))
                history = XformHistory(xform=xform, xls=File(file_obj))
                history.save()
            else:
                print('History already exists of this file  ', filename)
            print('Successfully created XFORM HISTORY form  ', filename)

        if error_file_list:
            print('Errors occured at files: ')
            for files in error_file_list:
                print(files)
Exemplo n.º 29
0
 def test_loop(self):
     path = utils.path_to_text_fixture("another_loop.xls")
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         "name": "another_loop",
         "id_string": "another_loop",
         "sms_keyword": "another_loop",
         "default_language": "default",
         "title": "another_loop",
         "type": "survey",
         "children": [
             {
                 "name": "loop_vehicle_types",
                 "type": "group",
                 "children": [
                     {
                         "label": {"English": "Car", "French": "Voiture"},
                         "name": "car",
                         "type": "group",
                         "children": [
                             {
                                 "label": {
                                     "English": "How many do you have?",
                                     "French": "Combien avoir?",
                                 },
                                 "name": "total",
                                 "type": "integer",
                             },
                             {
                                 "bind": {"constraint": ". <= ../total"},
                                 "label": {
                                     "English": "How many are working?",
                                     "French": "Combien marcher?",
                                 },
                                 "name": "working",
                                 "type": "integer",
                             },
                         ],
                     },
                     {
                         "label": {"English": "Motorcycle", "French": "Moto"},
                         "name": "motor_cycle",
                         "type": "group",
                         "children": [
                             {
                                 "label": {
                                     "English": "How many do you have?",
                                     "French": "Combien avoir?",
                                 },
                                 "name": "total",
                                 "type": "integer",
                             },
                             {
                                 "bind": {"constraint": ". <= ../total"},
                                 "label": {
                                     "English": "How many are working?",
                                     "French": "Combien marcher?",
                                 },
                                 "name": "working",
                                 "type": "integer",
                             },
                         ],
                     },
                 ],
             },
             {
                 "children": [
                     {
                         "bind": {"jr:preload": "uid", "readonly": "true()"},
                         "name": "instanceID",
                         "type": "calculate",
                     }
                 ],
                 "control": {"bodyless": True},
                 "name": "meta",
                 "type": "group",
             },
         ],
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Exemplo n.º 30
0
 def test_loop(self):
     path = utils.path_to_text_fixture('another_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         u'name':
         u'another_loop',
         u'id_string':
         u'another_loop',
         u'sms_keyword':
         u'another_loop',
         u'default_language':
         u'default',
         u'title':
         u'another_loop',
         u'type':
         u'survey',
         u'children': [{
             u'name':
             u'loop_vehicle_types',
             u'type':
             u'group',
             u'children': [{
                 u'label': {
                     u'English': u'Car',
                     u'French': u'Voiture'
                 },
                 u'name':
                 u'car',
                 u'type':
                 u'group',
                 u'children': [{
                     u'label': {
                         u'English': u'How many do you have?',
                         u'French': u'Combien avoir?'
                     },
                     u'name': u'total',
                     u'type': u'integer'
                 }, {
                     u'bind': {
                         u'constraint': u'. <= ../total'
                     },
                     u'label': {
                         u'English': u'How many are working?',
                         u'French': u'Combien marcher?'
                     },
                     u'name': u'working',
                     u'type': u'integer'
                 }],
             }, {
                 u'label': {
                     u'English': u'Motorcycle',
                     u'French': u'Moto'
                 },
                 u'name':
                 u'motor_cycle',
                 u'type':
                 u'group',
                 u'children': [{
                     u'label': {
                         u'English': u'How many do you have?',
                         u'French': u'Combien avoir?'
                     },
                     u'name': u'total',
                     u'type': u'integer'
                 }, {
                     u'bind': {
                         u'constraint': u'. <= ../total'
                     },
                     u'label': {
                         u'English': u'How many are working?',
                         u'French': u'Combien marcher?'
                     },
                     u'name': u'working',
                     u'type': u'integer'
                 }],
             }]
         }, {
             u'children': [{
                 u'bind': {
                     'calculate': "concat('uuid:', uuid())",
                     'readonly': 'true()'
                 },
                 u'name': 'instanceID',
                 u'type': 'calculate'
             }],
             u'control': {
                 'bodyless': True
             },
             u'name':
             'meta',
             u'type':
             u'group'
         }],
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Exemplo n.º 31
0
 def test_loop(self):
     path = utils.path_to_text_fixture('another_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         u'name': u'another_loop',
         u'id_string': u'another_loop',
         u'default_language': u'default',
         u'title': u'another_loop',
         u'type': u'survey',
         u'children': [
             {
                 u'label': {u'English': u'Car', u'French': u'Voiture'},
                 u'name': u'car',
                 u'type': u'group',
                 u'children': [
                     {
                         u'label': {
                             u'English': u'How many do you have?',
                             u'French': u'Combien avoir?'
                             },
                         u'name': u'total',
                         u'type': u'integer'
                         },
                     {
                         u'bind': {u'constraint': u'. <= ../total'},
                         u'label': {
                             u'English': u'How many are working?',
                             u'French': u'Combien marcher?'
                             },
                         u'name': u'working',
                         u'type': u'integer'
                         }
                     ],
                 },
             {
                 u'label': {u'English': u'Motorcycle', u'French': u'Moto'},
                 u'name': u'motor_cycle',
                 u'type': u'group',
                 u'children': [
                     {
                         u'label': {
                             u'English': u'How many do you have?',
                             u'French': u'Combien avoir?'
                             },
                         u'name': u'total',
                         u'type': u'integer'
                         },
                     {
                         u'bind': {u'constraint': u'. <= ../total'},
                         u'label': {
                             u'English': u'How many are working?',
                             u'French': u'Combien marcher?'
                             },
                         u'name': u'working',
                         u'type': u'integer'
                         }
                     ],
                 }
             ],
         }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Exemplo n.º 32
0
 def test_loop(self):
     path = utils.path_to_text_fixture('another_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         u'name': u'another_loop',
         u'id_string': u'another_loop',
         u'sms_keyword': u'another_loop',
         u'default_language': u'default',
         u'title': u'another_loop',
         u'type': u'survey',
         u'children': [
             {
                 u'name': u'loop_vehicle_types',
                 u'type': u'group',
         u'children': [
             {
                 u'label': {u'English': u'Car', u'French': u'Voiture'},
                 u'name': u'car',
                 u'type': u'group',
                 u'children': [
                     {
                         u'label': {
                             u'English': u'How many do you have?',
                             u'French': u'Combien avoir?'
                             },
                         u'name': u'total',
                         u'type': u'integer'
                         },
                     {
                         u'bind': {u'constraint': u'. <= ../total'},
                         u'label': {
                             u'English': u'How many are working?',
                             u'French': u'Combien marcher?'
                             },
                         u'name': u'working',
                         u'type': u'integer'
                         }
                     ],
                 },
             {
                 u'label': {u'English': u'Motorcycle', u'French': u'Moto'},
                 u'name': u'motor_cycle',
                 u'type': u'group',
                 u'children': [
                     {
                         u'label': {
                             u'English': u'How many do you have?',
                             u'French': u'Combien avoir?'
                             },
                         u'name': u'total',
                         u'type': u'integer'
                         },
                     {
                         u'bind': {u'constraint': u'. <= ../total'},
                         u'label': {
                             u'English': u'How many are working?',
                             u'French': u'Combien marcher?'
                             },
                         u'name': u'working',
                         u'type': u'integer'
                         }
                     ],
                 }]},
             {
                 u'children': [
                     {
                         u'bind': {
                             'calculate': "concat('uuid:', uuid())",
                             'readonly': 'true()'
                         },
                         u'name': 'instanceID',
                         u'type': 'calculate'
                     }
                 ],
                 u'control': {
                     'bodyless': True
                 },
                 u'name': 'meta',
                 u'type': u'group'
             }
             ],
         }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Exemplo n.º 33
0
 def _create_childrens_survey(self):
     return create_survey_from_xls(
         _logger_fixture_path('childrens_survey.xls'))
Exemplo n.º 34
0
 def _create_childrens_survey(self):
     return create_survey_from_xls(_logger_fixture_path(
         'childrens_survey.xls'))
Exemplo n.º 35
0
 def test_create_from_file_object(self):
     path = utils.path_to_text_fixture('yes_or_no_question.xls')
     with open(path) as f:
         create_survey_from_xls(f)
Exemplo n.º 36
0
 def test_loop(self):
     path = utils.path_to_text_fixture("another_loop.xls")
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         "name": "another_loop",
         "id_string": "another_loop",
         "sms_keyword": "another_loop",
         "default_language": "default",
         "title": "another_loop",
         "type": "survey",
         "children": [
             {
                 "name": "loop_vehicle_types",
                 "type": "group",
                 "children": [
                     {
                         "label": {"English": "Car", "French": "Voiture"},
                         "name": "car",
                         "type": "group",
                         "children": [
                             {
                                 "label": {
                                     "English": "How many do you have?",
                                     "French": "Combien avoir?",
                                 },
                                 "name": "total",
                                 "type": "integer",
                             },
                             {
                                 "bind": {"constraint": ". <= ../total"},
                                 "label": {
                                     "English": "How many are working?",
                                     "French": "Combien marcher?",
                                 },
                                 "name": "working",
                                 "type": "integer",
                             },
                         ],
                     },
                     {
                         "label": {"English": "Motorcycle", "French": "Moto"},
                         "name": "motor_cycle",
                         "type": "group",
                         "children": [
                             {
                                 "label": {
                                     "English": "How many do you have?",
                                     "French": "Combien avoir?",
                                 },
                                 "name": "total",
                                 "type": "integer",
                             },
                             {
                                 "bind": {"constraint": ". <= ../total"},
                                 "label": {
                                     "English": "How many are working?",
                                     "French": "Combien marcher?",
                                 },
                                 "name": "working",
                                 "type": "integer",
                             },
                         ],
                     },
                 ],
             },
             {
                 "children": [
                     {
                         "bind": {
                             "calculate": "concat('uuid:', uuid())",
                             "readonly": "true()",
                         },
                         "name": "instanceID",
                         "type": "calculate",
                     }
                 ],
                 "control": {"bodyless": True},
                 "name": "meta",
                 "type": "group",
             },
         ],
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)
Exemplo n.º 37
0
 def test_create_from_file_object(self):
     path = utils.path_to_text_fixture("yes_or_no_question.xls")
     with open(path, "rb") as f:
         create_survey_from_xls(f)
Exemplo n.º 38
0
 def get_survey(self):
     if self.is_valid():
         xls = self.cleaned_data['xls_file']
         return create_survey_from_xls(xls)
Exemplo n.º 39
0
 def test_loop(self):
     path = utils.path_to_text_fixture('another_loop.xls')
     survey = create_survey_from_xls(path)
     self.maxDiff = None
     expected_dict = {
         u'name':
         u'another_loop',
         u'id_string':
         u'another_loop',
         u'default_language':
         u'default',
         u'title':
         u'another_loop',
         u'type':
         u'survey',
         u'children': [{
             u'label': {
                 u'English': u'Car',
                 u'French': u'Voiture'
             },
             u'name':
             u'car',
             u'type':
             u'group',
             u'children': [{
                 u'label': {
                     u'English': u'How many do you have?',
                     u'French': u'Combien avoir?'
                 },
                 u'name': u'total',
                 u'type': u'integer'
             }, {
                 u'bind': {
                     u'constraint': u'. <= ../total'
                 },
                 u'label': {
                     u'English': u'How many are working?',
                     u'French': u'Combien marcher?'
                 },
                 u'name': u'working',
                 u'type': u'integer'
             }],
         }, {
             u'label': {
                 u'English': u'Motorcycle',
                 u'French': u'Moto'
             },
             u'name':
             u'motor_cycle',
             u'type':
             u'group',
             u'children': [{
                 u'label': {
                     u'English': u'How many do you have?',
                     u'French': u'Combien avoir?'
                 },
                 u'name': u'total',
                 u'type': u'integer'
             }, {
                 u'bind': {
                     u'constraint': u'. <= ../total'
                 },
                 u'label': {
                     u'English': u'How many are working?',
                     u'French': u'Combien marcher?'
                 },
                 u'name': u'working',
                 u'type': u'integer'
             }],
         }],
     }
     self.assertEquals(survey.to_json_dict(), expected_dict)