Пример #1
0
    def test_hidden(self):
        x = SurveyReader(utils.path_to_text_fixture("hidden.xls"))
        x_results = x.to_json_dict()

        expected_dict = [
            {
                u'type': u'hidden',
                u'name': u'hidden_test'
            },
            {
                'children': [
                    {
                        'bind': {
                            'calculate': "concat('uuid:', uuid())",
                            'readonly': 'true()'
                        },
                        'name': 'instanceID',
                        'type': 'calculate'
                    }
                ],
                'control': {
                    'bodyless': True
                },
                'name': 'meta',
                'type': 'group'
            }
        ]
        self.assertEqual(x_results[u"children"], expected_dict)
Пример #2
0
 def test_xl_date_ambigous(self):
     """Test non standard sheet with exception is processed successfully."""
     filename = "xl_date_ambiguous.xlsx"
     path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
     xls_reader = SurveyReader(path_to_excel_file)
     survey_dict = xls_reader.to_json_dict()
     self.assertTrue(len(survey_dict) > 0)
Пример #3
0
    def test_text_and_integer(self):
        x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls"))

        expected_dict = [
            {
                "label": {"english": "What is your name?"},
                "type": "text",
                "name": "your_name",
            },
            {
                "label": {"english": "How many years old are you?"},
                "type": "integer",
                "name": "your_age",
            },
            {
                "children": [
                    {
                        "bind": {
                            "calculate": "concat('uuid:', uuid())",
                            "readonly": "true()",
                        },
                        "name": "instanceID",
                        "type": "calculate",
                    }
                ],
                "control": {"bodyless": True},
                "name": "meta",
                "type": "group",
            },
        ]

        self.assertEqual(x.to_json_dict()["children"], expected_dict)
Пример #4
0
def create_survey_from_xls(path_or_file):
    excel_reader = SurveyReader(path_or_file)
    d = excel_reader.to_json_dict()
    survey = create_survey_element_from_dict(d)
    if not survey.id_string:
        survey.id_string = excel_reader._name
    return survey
Пример #5
0
    def test_table(self):
        x = SurveyReader(utils.path_to_text_fixture("simple_loop.xls"))

        expected_dict = {
            u"type": u"survey",
            u"name": u"simple_loop",
            u"id_string": u"simple_loop",
            u"default_language": u"default",
            u"title": u"simple_loop",
            u"children": [
                {
                    u"children": [
                        {
                            u"type": u"integer",
                            u"name": u"count",
                            u"label": {u"English": u"How many are there in this group?"},
                        }
                    ],
                    u"type": u"loop",
                    u"name": u"my_table",
                    u"columns": [
                        {u"name": u"col1", u"label": {u"English": u"Column 1"}},
                        {u"name": u"col2", u"label": {u"English": u"Column 2"}},
                    ],
                    u"label": {u"English": u"My Table"},
                }
            ],
        }
        self.maxDiff = None
        self.assertEqual(x.to_json_dict(), expected_dict)
Пример #6
0
    def test_table(self):
        x = SurveyReader(utils.path_to_text_fixture("simple_loop.xls"))

        expected_dict = {
            u'type': u'survey',
            u'name': u'simple_loop',
            u'id_string': u'simple_loop',
            u'default_language': u'default',
            u'title': u'simple_loop',
            u'children': [
                {
                    u'children': [
                        {
                            u'type': u'integer',
                            u'name': u'count',
                            u'label': {u'English': u'How many are there in this group?'}
                            }
                        ],
                    u'type': u'loop',
                    u'name': u'my_table',
                    u'columns': [
                        {
                            u'name': u'col1',
                            u'label': {u'English': u'Column 1'}
                            },
                        {
                            u'name': u'col2',
                            u'label': {u'English': u'Column 2'}
                            }
                        ],
                    u'label': {u'English': u'My Table'}
                    }]}
        self.maxDiff = None
        self.assertEqual(x.to_json_dict(), expected_dict)
Пример #7
0
    def test_gps(self):
        x = SurveyReader(utils.path_to_text_fixture("gps.xls"))

        expected_dict = [
            {
                u'type': u'gps', u'name': u'location', u'label': u'GPS'},
            {
                'children': [
                    {
                        'bind': {
                            'calculate': "concat('uuid:', uuid())",
                            'readonly': 'true()'
                        },
                        'name': 'instanceID',
                        'type': 'calculate'
                    }
                ],
                'control': {
                    'bodyless': True
                },
                'name': 'meta',
                'type': 'group'
            }]

        self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
Пример #8
0
    def test_text_and_integer(self):
        x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls"))

        expected_dict = [
            {u"text": {u"english": u"What is your name?"}, u"type": u"text", u"name": u"your_name"},
            {u"text": {u"english": u"How many years old are you?"}, u"type": u"integer", u"name": u"your_age"},
        ]

        self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
Пример #9
0
 def test_json(self):
     x = SurveyReader(utils.path_to_text_fixture("group.xls"))
     x_results = x.to_json_dict()
     expected_dict = {
         u'name': u'group',
         u'title': u'group',
         u'id_string': u'group',
         u'sms_keyword': u'group',
         u'default_language': u'default',
         u'type': u'survey',
         u'children': [
             {
                 u'name': u'family_name',
                 u'type': u'text',
                 u'label': {u'English': u"What's your family name?"}
                 },
             {
                 u'name': u'father',
                 u'type': u'group',
                 u'label': {u'English': u'Father'},
                 u'children': [
                     {
                         u'name': u'phone_number',
                         u'type': u'phone number',
                         u'label': {u'English': u"What's your father's phone number?"}
                         },
                     {
                         u'name': u'age',
                         u'type': u'integer',
                         u'label': {u'English': u'How old is your father?'}
                         }
                     ],
                 },
             {
                 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.maxDiff = None
     self.assertEqual(x_results, expected_dict)
Пример #10
0
    def test_hidden(self):
        x = SurveyReader(utils.path_to_text_fixture("hidden.xls"))
        x_results = x.to_json_dict()

        expected_dict = [
            {
                u'type': u'hidden',
                u'name': u'hidden_test',
                }
        ]
        self.assertEqual(x_results[u"children"], expected_dict)
Пример #11
0
 def test_json(self):
     x = SurveyReader(utils.path_to_text_fixture("group.xls"))
     x_results = x.to_json_dict()
     expected_dict = {
         "name": "group",
         "title": "group",
         "id_string": "group",
         "sms_keyword": "group",
         "default_language": "default",
         "type": "survey",
         "children": [
             {
                 "name": "family_name",
                 "type": "text",
                 "label": {"English": "What's your family name?"},
             },
             {
                 "name": "father",
                 "type": "group",
                 "label": {"English": "Father"},
                 "children": [
                     {
                         "name": "phone_number",
                         "type": "phone number",
                         "label": {"English": "What's your father's phone number?"},
                     },
                     {
                         "name": "age",
                         "type": "integer",
                         "label": {"English": "How old is your father?"},
                     },
                 ],
             },
             {
                 "children": [
                     {
                         "bind": {
                             "calculate": "concat('uuid:', uuid())",
                             "readonly": "true()",
                         },
                         "name": "instanceID",
                         "type": "calculate",
                     }
                 ],
                 "control": {"bodyless": True},
                 "name": "meta",
                 "type": "group",
             },
         ],
     }
     self.maxDiff = None
     self.assertEqual(x_results, expected_dict)
Пример #12
0
    def test_equality_of_to_dict(self):
        x = SurveyReader(utils.path_to_text_fixture("group.xls"))
        x_results = x.to_json_dict()

        survey = create_survey_element_from_dict(x_results)
        survey_dict = survey.to_json_dict()
        # using the builder sets the title attribute to equal name
        # this won't happen through reading the excel file as done by
        # SurveyReader.
        # Now it happens.
        #del survey_dict[u'title']
        self.maxDiff = None
        self.assertEqual(x_results, survey_dict)
Пример #13
0
def load_file_to_dict(path):
    """
    Takes a file path and loads it into a nested json dict
    following the format in json_form_schema.json
    The file may be a xls file or json file.
    If it is xls it is converted using xls2json.
    """
    if path.endswith(".json"):
        name = _section_name(path)
        return name, utils.get_pyobj_from_json(path)
    else:
        name = _section_name(path)
        excel_reader = SurveyReader(path)
        return name, excel_reader.to_json_dict()
Пример #14
0
 def test_simple_yes_or_no_question(self):
     filename = "yes_or_no_question.xls"
     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 + ".json")
     expected_output_path = os.path.join(DIR, "test_expected_output", root_filename + ".json")
     x = SurveyReader(path_to_excel_file)
     x_results = x.to_json_dict()
     with codecs.open(output_path, mode="w", encoding="utf-8") as fp:
         json.dump(x_results, fp=fp, ensure_ascii=False, indent=4)
     #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())
Пример #15
0
    def test_simple_yes_or_no_question(self):
        x = SurveyReader(utils.path_to_text_fixture("yes_or_no_question.xls"))
        x_results = x.to_json_dict()

        expected_dict = [
            {
                u"label": {u"english": u"have you had a good day today?"},
                u"type": u"select one",
                u"name": u"good_day",
                u"choices": [
                    {u"label": {u"english": u"yes"}, u"name": u"yes"},
                    {u"label": {u"english": u"no"}, u"name": u"no"},
                ],
            }
        ]
        self.assertEqual(x_results[u"children"], expected_dict)
Пример #16
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         excel_reader = SurveyReader(self.xls)
         survey_dict = excel_reader.to_json_dict()
         if has_external_choices(survey_dict):
             self.survey_dict = survey_dict
             self.has_external_choices = True
         survey = create_survey_element_from_dict(survey_dict)
         survey = self._check_version_set(survey)
         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)
Пример #17
0
 def test_survey_reader(self):
     survey_reader = SurveyReader(self.path)
     expected_dict = {
         u'id_string': u'new_id',
         u'sms_keyword': u'new_id',
         u'default_language': u'default',
         u'name': u'settings',
         u'title': u'My Survey',
         u'type': u'survey',
         u'attribute': {
             u'my_number': u'1234567890',
             u'my_string': u'lor\xe9m ipsum'
         },
         u'children': [
             {
                 u'name': u'your_name',
                 u'label': {u'english': u'What is your name?'},
                 u'type': u'text'
             },
             {
                 u'name': u'your_age',
                 u'label': {u'english': u'How many years old are you?'},
                 u'type': u'integer'
             },
             {
                 'children': [
                     {
                         'bind': {
                             'calculate': "concat('uuid:', uuid())",
                             'readonly': 'true()'
                         },
                         'name': 'instanceID',
                         'type': 'calculate'
                     }
                 ],
                 'control': {
                     'bodyless': True
                 },
                 'name': 'meta',
                 'type': 'group'
             }
         ],
     }
     self.assertEqual(survey_reader.to_json_dict(), expected_dict)
Пример #18
0
 def test_table(self):
     filename = "simple_loop.xls"
     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 + ".json")
     expected_output_path = os.path.join(
         DIR, "test_expected_output", root_filename + ".json"
     )
     x = SurveyReader(path_to_excel_file)
     x_results = x.to_json_dict()
     with codecs.open(output_path, mode="w", encoding="utf-8") as fp:
         json.dump(x_results, fp=fp, ensure_ascii=False, indent=4)
     # 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:
             expected_json = json.load(expected_file)
             actual_json = json.load(actual_file)
             self.assertEqual(expected_json, actual_json)
Пример #19
0
 def test_survey_reader(self):
     survey_reader = SurveyReader(self.path)
     expected_dict = {
         u"id_string": u"new_id",
         u"sms_keyword": u"new_id",
         u"default_language": u"default",
         u"name": u"settings",
         u"title": u"My Survey",
         u"type": u"survey",
         u"attribute": {
             u"my_number": u"1234567890",
             u"my_string": u"lor\xe9m ipsum",
         },
         u"children": [
             {
                 u"name": u"your_name",
                 u"label": {u"english": u"What is your name?"},
                 u"type": u"text",
             },
             {
                 u"name": u"your_age",
                 u"label": {u"english": u"How many years old are you?"},
                 u"type": u"integer",
             },
             {
                 "children": [
                     {
                         "bind": {
                             "calculate": "concat('uuid:', uuid())",
                             "readonly": "true()",
                         },
                         "name": "instanceID",
                         "type": "calculate",
                     }
                 ],
                 "control": {"bodyless": True},
                 "name": "meta",
                 "type": "group",
             },
         ],
     }
     self.assertEqual(survey_reader.to_json_dict(), expected_dict)
Пример #20
0
    def clean_xform_file( self ):
        '''
            Clean & checks the validity of the uploaded xform file.
        '''
        data = self.cleaned_data[ 'xform_file' ]

        if data is None:
            return None

        name, file_ext = os.path.splitext( data.name )

        # Parse file depending on file type
        if file_ext == '.xls':
            survey = SurveyReader( data )
        elif file_ext == '.xml':
            survey = XFormReader( data )
        else:
            raise forms.ValidationError( 'Unable to read XForm' )

        return survey.to_json_dict()
Пример #21
0
    def test_hidden(self):
        x = SurveyReader(utils.path_to_text_fixture("hidden.xls"))
        x_results = x.to_json_dict()

        expected_dict = [
            {u"type": u"hidden", u"name": u"hidden_test"},
            {
                "children": [
                    {
                        "bind": {"calculate": "concat('uuid:', uuid())", "readonly": "true()"},
                        "name": "instanceID",
                        "type": "calculate",
                    }
                ],
                "control": {"bodyless": True},
                "name": "meta",
                "type": "group",
            },
        ]
        self.assertEqual(x_results[u"children"], expected_dict)
Пример #22
0
    def test_gps(self):
        x = SurveyReader(utils.path_to_text_fixture("gps.xls"))

        expected_dict = [
            {u"type": u"gps", u"name": u"location", u"label": u"GPS"},
            {
                "children": [
                    {
                        "bind": {"calculate": "concat('uuid:', uuid())", "readonly": "true()"},
                        "name": "instanceID",
                        "type": "calculate",
                    }
                ],
                "control": {"bodyless": True},
                "name": "meta",
                "type": "group",
            },
        ]

        self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
Пример #23
0
    def test_text_and_integer(self):
        x = SurveyReader(
            utils.path_to_text_fixture("text_and_integer.xls"),
            default_name="text_and_integer",
        )

        expected_dict = [
            {
                "label": {
                    "english": "What is your name?"
                },
                "type": "text",
                "name": "your_name",
            },
            {
                "label": {
                    "english": "How many years old are you?"
                },
                "type": "integer",
                "name": "your_age",
            },
            {
                "children": [{
                    "bind": {
                        "jr:preload": "uid",
                        "readonly": "true()"
                    },
                    "name": "instanceID",
                    "type": "calculate",
                }],
                "control": {
                    "bodyless": True
                },
                "name":
                "meta",
                "type":
                "group",
            },
        ]

        self.assertEqual(x.to_json_dict()["children"], expected_dict)
Пример #24
0
 def test_survey_reader(self):
     survey_reader = SurveyReader(self.path, default_name="settings")
     expected_dict = {
         u"id_string": u"new_id",
         u"sms_keyword": u"new_id",
         u"default_language": u"default",
         u"name": u"settings",
         u"title": u"My Survey",
         u"type": u"survey",
         u"attribute": {
             u"my_number": u"1234567890",
             u"my_string": u"lor\xe9m ipsum",
         },
         u"children": [
             {
                 u"name": u"your_name",
                 u"label": {u"english": u"What is your name?"},
                 u"type": u"text",
             },
             {
                 u"name": u"your_age",
                 u"label": {u"english": u"How many years old are you?"},
                 u"type": u"integer",
             },
             {
                 "children": [
                     {
                         "bind": {"jr:preload": "uid", "readonly": "true()"},
                         "name": "instanceID",
                         "type": "calculate",
                     }
                 ],
                 "control": {"bodyless": True},
                 "name": "meta",
                 "type": "group",
             },
         ],
     }
     actual_dict = survey_reader.to_json_dict()
     actual_dict.pop("generated_by", None)
     self.assertEqual(actual_dict, expected_dict)
Пример #25
0
    def clean_xform_file(self):
        """
            Clean & checks the validity of the uploaded xform file.
        """

        data = self.cleaned_data['xform_file']

        if data is None:
            return None

        name, file_ext = os.path.splitext(data.name)

        # Parse file depending on file type
        if file_ext == '.xls':
            survey = SurveyReader(data)
        elif file_ext == '.xml':
            survey = XFormReader(data)
        else:
            raise forms.ValidationError('Unable to read XForm')

        return survey.to_json_dict()
Пример #26
0
 def test_simple_yes_or_no_question(self):
     filename = "yes_or_no_question.xls"
     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 + ".json")
     expected_output_path = os.path.join(DIR, "test_expected_output",
                                         root_filename + ".json")
     x = SurveyReader(path_to_excel_file, default_name="yes_or_no_question")
     x_results = x.to_json_dict()
     with codecs.open(output_path, mode="w", encoding="utf-8") as fp:
         json.dump(x_results, fp=fp, ensure_ascii=False, indent=4)
     # 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:
             expected_json = json.load(expected_file)
             actual_json = json.load(actual_file)
             actual_json.pop("generated_by", None)
             self.assertEqual(expected_json, actual_json)
Пример #27
0
    def test_simple_yes_or_no_question(self):
        x = SurveyReader(utils.path_to_text_fixture("yes_or_no_question.xls"))
        x_results = x.to_json_dict()

        expected_dict = [
            {
                u'label': {u'english': u'have you had a good day today?'},
                u'type': u'select one',
                u'name': u'good_day',
                u'choices': [
                    {
                        u'label': {u'english': u'yes'},
                        u'name': u'yes'
                        },
                    {
                        u'label': {u'english': u'no'},
                        u'name': u'no'
                        }
                    ]
                }
            ]
        self.assertEqual(x_results[u"children"], expected_dict)
Пример #28
0
    def test_hidden(self):
        x = SurveyReader(
            utils.path_to_text_fixture("hidden.xls"), default_name="hidden"
        )
        x_results = x.to_json_dict()

        expected_dict = [
            {"type": "hidden", "name": "hidden_test"},
            {
                "children": [
                    {
                        "bind": {"jr:preload": "uid", "readonly": "true()"},
                        "name": "instanceID",
                        "type": "calculate",
                    }
                ],
                "control": {"bodyless": True},
                "name": "meta",
                "type": "group",
            },
        ]
        self.assertEqual(x_results["children"], expected_dict)
Пример #29
0
 def test_survey_reader(self):
     survey_reader = SurveyReader(self.path)
     expected_dict = {
         u'id_string': u'new_id',
         u'default_language': u'default',
         u'name': u'settings',
         u'title': u'My Survey',
         u'type': u'survey',
         u'children': [
             {
                 u'name': u'your_name',
                 u'text': {u'english': u'What is your name?'},
                 u'type': u'text'
                 },
             {
                 u'name': u'your_age',
                 u'text': {u'english': u'How many years old are you?'},
                 u'type': u'integer'
                 }
             ],
         }
     self.assertEqual(survey_reader.to_json_dict(), expected_dict)
Пример #30
0
    def test_text_and_integer(self):
        x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls"))

        expected_dict = [
            {
                u'label': {
                    u'english': u'What is your name?'
                },
                u'type': u'text',
                u'name': u'your_name'
            },
            {
                u'label': {
                    u'english': u'How many years old are you?'
                },
                u'type': u'integer',
                u'name': u'your_age'
            },
            {
                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.assertEqual(x.to_json_dict()[u"children"], expected_dict)
Пример #31
0
 def test_json(self):
     x = SurveyReader(utils.path_to_text_fixture("group.xls"))
     x_results = x.to_json_dict()
     expected_dict = {
         u'name': u'group',
         u'title': u'group',
         u'id_string': u'group',
         u'default_language': u'default',
         u'type': u'survey',
         u'children': [
             {
                 u'name': u'family_name',
                 u'type': u'text',
                 u'label': {u'English': u"What's your family name?"}
                 },
             {
                 u'name': u'father',
                 u'type': u'group',
                 u'label': {u'English': u'Father'},
                 u'children': [
                     {
                         u'name': u'phone_number',
                         u'type': u'phone number',
                         u'label': {u'English': u"What's your father's phone number?"}
                         },
                     {
                         u'name': u'age',
                         u'type': u'integer',
                         u'label': {u'English': u'How old is your father?'}
                         }
                     ],
                 }
             ],
         }
     self.maxDiff = None
     self.assertEqual(x_results, expected_dict)
Пример #32
0
    def test_text_and_integer(self):
        x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls"))

        expected_dict = [{u'text': {u'english': u'What is your name?'}, u'type': u'text', u'name': u'your_name'}, {u'text': {u'english': u'How many years old are you?'}, u'type': u'integer', u'name': u'your_age'}]

        self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
Пример #33
0
    def test_choice_filter_choice_fields(self):
        """
        Test that the choice filter fields appear on children field of json
        """
        choice_filter_survey = SurveyReader(
            utils.path_to_text_fixture("choice_filter_test.xlsx"),
            default_name="choice_filter_test",
        )

        expected_dict = [
            {
                "choices": [
                    {
                        "name": "texas",
                        "label": "Texas"
                    },
                    {
                        "name": "washington",
                        "label": "Washington"
                    },
                ],
                "type":
                "select one",
                "name":
                "state",
                "list_name":
                "states",
                "parameters": {},
                "label":
                "state",
            },
            {
                "name":
                "county",
                "parameters": {},
                "choice_filter":
                "${state}=cf",
                "label":
                "county",
                "itemset":
                "counties",
                "list_name":
                "counties",
                "choices": [
                    {
                        "label": "King",
                        "cf": "washington",
                        "name": "king"
                    },
                    {
                        "label": "Pierce",
                        "cf": "washington",
                        "name": "pierce"
                    },
                    {
                        "label": "King",
                        "cf": "texas",
                        "name": "king"
                    },
                    {
                        "label": "Cameron",
                        "cf": "texas",
                        "name": "cameron"
                    },
                ],
                "type":
                "select one",
            },
            {
                "name":
                "city",
                "parameters": {},
                "choice_filter":
                "${county}=cf",
                "label":
                "city",
                "itemset":
                "cities",
                "list_name":
                "cities",
                "choices": [
                    {
                        "label": "Dumont",
                        "cf": "king",
                        "name": "dumont"
                    },
                    {
                        "label": "Finney",
                        "cf": "king",
                        "name": "finney"
                    },
                    {
                        "label": "brownsville",
                        "cf": "cameron",
                        "name": "brownsville"
                    },
                    {
                        "label": "harlingen",
                        "cf": "cameron",
                        "name": "harlingen"
                    },
                    {
                        "label": "Seattle",
                        "cf": "king",
                        "name": "seattle"
                    },
                    {
                        "label": "Redmond",
                        "cf": "king",
                        "name": "redmond"
                    },
                    {
                        "label": "Tacoma",
                        "cf": "pierce",
                        "name": "tacoma"
                    },
                    {
                        "label": "Puyallup",
                        "cf": "pierce",
                        "name": "puyallup"
                    },
                ],
                "type":
                "select one",
            },
            {
                "control": {
                    "bodyless": True
                },
                "type":
                "group",
                "name":
                "meta",
                "children": [{
                    "bind": {
                        "readonly": "true()",
                        "jr:preload": "uid"
                    },
                    "type": "calculate",
                    "name": "instanceID",
                }],
            },
        ]
        self.assertEqual(choice_filter_survey.to_json_dict()["children"],
                         expected_dict)
Пример #34
0
def edit_repo( request, repo_id ):
    """
        Edits a data repository
        Takes user to Form Builder
    """
    repo = get_object_or_404( Repository, mongo_id=repo_id )

    # Check that this user has permission to edit this repo
    if not request.user.has_perm( 'delete_repository', repo ):
        return HttpResponse( 'Unauthorized', status=401 )

    if request.method == 'POST':
        form = NewRepoForm( request.POST, request.FILES, user=request.user )
        #print form.cleaned_data
        repo.name = request.POST['name'].replace( ' ','_' )
        repo.description = request.POST['desc']
        repo.save()

        data = request.FILES[ 'xform_file' ]

        # Split apart the file name so we can attempt to detect the file type
        name, file_ext = os.path.splitext( data.name )
        file_ext = file_ext[1:].lower()

        if not settings.DEBUG and not settings.TESTING:
            storage.bucket_name = settings.AWS_TASK_STORAGE_BUCKET_NAME

        s3_url = '%s.%s' % ( repo.mongo_id, file_ext )
        storage.save( s3_url, data )

        xform = storage.open( s3_url, 'r' )
        fields = SurveyReader( xform )
        fields = fields.to_json_dict()

        repo.update_fields( fields.get( 'children' ) )

        #data_repo = db.repo.find_one( ObjectId( repo_id ) )
        #newJSON = json.loads( request.POST['survey_json'].strip() )
        #newfields = newJSON['children']
        #formType = newJSON['type']
        #db.repo.update( {"_id":ObjectId( repo_id )},{"$set": {'fields': newfields, 'type': formType}} )
        return HttpResponseRedirect( '/' )
    else:
        form = NewRepoForm()
        form.initial["name"] = repo.name
        form.initial["desc"] = repo.description

    user_repos = Repository.objects.filter( user=request.user, org=None )
    repos = []
    for temp_repo in user_repos:
        #I want a flat list of repo fields
        repo_info = {
            'name': temp_repo.name,
            'mongo_id': temp_repo.mongo_id,
            'children': temp_repo.fields() }
        repos.append( repo_info )

    data_repo = db.repo.find_one( ObjectId( repo_id ) )
    temp_dict = {}
    temp_dict['children'] = data_repo['fields']
    if 'type' in data_repo:
        temp_dict['type'] = data_repo['type']
    else:
        temp_dict['type'] = "survey"

    return render_to_response( 'new.html',
                               {'form': form,
                                'repo_json': json.dumps(temp_dict),
                                'user_repos': repos },
                               context_instance=RequestContext(request) )
Пример #35
0
 def test_json(self):
     x = SurveyReader(utils.path_to_text_fixture("group.xls"),
                      default_name="group")
     x_results = x.to_json_dict()
     expected_dict = {
         "name":
         "group",
         "title":
         "group",
         "id_string":
         "group",
         "sms_keyword":
         "group",
         "default_language":
         "default",
         "type":
         "survey",
         "children": [
             {
                 "name": "family_name",
                 "type": "text",
                 "label": {
                     "English": "What's your family name?"
                 },
             },
             {
                 "name":
                 "father",
                 "type":
                 "group",
                 "label": {
                     "English": "Father"
                 },
                 "children": [
                     {
                         "name": "phone_number",
                         "type": "phone number",
                         "label": {
                             "English": "What's your father's phone number?"
                         },
                     },
                     {
                         "name": "age",
                         "type": "integer",
                         "label": {
                             "English": "How old is your father?"
                         },
                     },
                 ],
             },
             {
                 "children": [{
                     "bind": {
                         "jr:preload": "uid",
                         "readonly": "true()"
                     },
                     "name": "instanceID",
                     "type": "calculate",
                 }],
                 "control": {
                     "bodyless": True
                 },
                 "name":
                 "meta",
                 "type":
                 "group",
             },
         ],
     }
     self.maxDiff = None
     self.assertEqual(x_results, expected_dict)
Пример #36
0
 def test_blank_second_row(self):
     filename = "blank_second_row.xls"
     path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
     xls_reader = SurveyReader(path_to_excel_file)
     survey_dict = xls_reader.to_json_dict()
     self.assertTrue(len(survey_dict) > 0)
Пример #37
0
 def test_choice_name_as_type(self):
     filename = "choice_name_as_type.xls"
     path_to_excel_file = os.path.join(DIR, "example_xls", filename)
     xls_reader = SurveyReader(path_to_excel_file)
     survey_dict = xls_reader.to_json_dict()
     self.assertTrue(has_external_choices(survey_dict))
Пример #38
0
 def test_blank_second_row(self):
     filename = "blank_second_row.xls"
     path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
     xls_reader = SurveyReader(path_to_excel_file)
     survey_dict = xls_reader.to_json_dict()
     self.assertTrue(len(survey_dict) > 0)
Пример #39
0
 def test_choice_name_as_type(self):
     filename = "choice_name_as_type.xls"
     path_to_excel_file = os.path.join(DIR, "example_xls", filename)
     xls_reader = SurveyReader(path_to_excel_file)
     survey_dict = xls_reader.to_json_dict()
     self.assertTrue(has_external_choices(survey_dict))
Пример #40
0
    def test_choice_filter_choice_fields(self):
        """
        Test that the choice filter fields appear on children field of json
        """
        choice_filter_survey = SurveyReader(
            utils.path_to_text_fixture("choice_filter_test.xlsx"))

        expected_dict = [{
            u'choices': [{
                u'name': u'texas',
                u'label': u'Texas'
            }, {
                u'name': u'washington',
                u'label': u'Washington'
            }],
            u'type':
            u'select one',
            u'name':
            u'state',
            u'parameters': {},
            u'label':
            u'state'
        }, {
            u'name':
            u'county',
            u'parameters': {},
            u'choice_filter':
            u'${state}=cf',
            u'label':
            u'county',
            u'itemset':
            u'counties',
            u'choices': [{
                u'label': u'King',
                u'cf': u'washington',
                u'name': u'king'
            }, {
                u'label': u'Pierce',
                u'cf': u'washington',
                u'name': u'pierce'
            }, {
                u'label': u'King',
                u'cf': u'texas',
                u'name': u'king'
            }, {
                u'label': u'Cameron',
                u'cf': u'texas',
                u'name': u'cameron'
            }],
            u'type':
            u'select one'
        }, {
            u'name':
            u'city',
            u'parameters': {},
            u'choice_filter':
            u'${county}=cf',
            u'label':
            u'city',
            u'itemset':
            u'cities',
            u'choices': [{
                u'label': u'Dumont',
                u'cf': u'king',
                u'name': u'dumont'
            }, {
                u'label': u'Finney',
                u'cf': u'king',
                u'name': u'finney'
            }, {
                u'label': u'brownsville',
                u'cf': u'cameron',
                u'name': u'brownsville'
            }, {
                u'label': u'harlingen',
                u'cf': u'cameron',
                u'name': u'harlingen'
            }, {
                u'label': u'Seattle',
                u'cf': u'king',
                u'name': u'seattle'
            }, {
                u'label': u'Redmond',
                u'cf': u'king',
                u'name': u'redmond'
            }, {
                u'label': u'Tacoma',
                u'cf': u'pierce',
                u'name': u'tacoma'
            }, {
                u'label': u'Puyallup',
                u'cf': u'pierce',
                u'name': u'puyallup'
            }],
            u'type':
            u'select one'
        }, {
            u'control': {
                u'bodyless': True
            },
            u'type':
            u'group',
            u'name':
            u'meta',
            u'children': [{
                u'bind': {
                    u'readonly': u'true()',
                    u'calculate': u"concat('uuid:', uuid())"
                },
                u'type': u'calculate',
                u'name': u'instanceID'
            }]
        }]
        self.assertEqual(choice_filter_survey.to_json_dict()[u"children"],
                         expected_dict)