Exemplo n.º 1
0
def convert_answers_to_data(answer_store, questionnaire_json, routing_path):
    """
    Convert answers into the data format below
    "data": {
          "001": "01-01-2016",
          "002": "30-03-2016"
        }
    :param answer_store: questionnaire answers
    :param questionnaire_json: The survey json
    :param routing_path: the path followed in the questionnaire
    :return: data in a formatted form
    """
    data = OrderedDict()
    for location in routing_path:
        answers_in_block = answer_store.filter(location=location)
        block_json = SchemaHelper.get_block(questionnaire_json, location.block_id)
        answer_schema_list = SchemaHelper.get_answers_by_id_for_block(block_json)

        for answer in answers_in_block:
            answer_schema = answer_schema_list[answer['answer_id']]
            value = answer['value']

            if answer_schema is not None and value is not None:
                if answer_schema['type'] != 'Checkbox' or any('q_code' not in option for option in answer_schema['options']):
                    data[answer_schema['q_code']] = _get_answer_data(data, answer_schema['q_code'], value)
                else:
                    data.update(_get_checkbox_answer_data(answer_schema, value))
    return data
    def test_child_answers_define_parent(self):
        for schema_file in self.all_schema_files():
            with open(schema_file, encoding="utf8") as file:
                schema_json = load(file)

                for block in SchemaHelper.get_blocks(schema_json):
                    answers_by_id = SchemaHelper.get_answers_by_id_for_block(
                        block)

                    for answer_id, answer in answers_by_id.items():
                        if answer['type'] in ['Radio', 'Checkbox']:
                            child_answer_ids = (o['child_answer_id']
                                                for o in answer['options']
                                                if 'child_answer_id' in o)

                            for child_answer_id in child_answer_ids:
                                if child_answer_id not in answers_by_id:
                                    self.fail(
                                        "Child answer with id '%s' does not exist in schema %s"
                                        % (child_answer_id, schema_file))
                                if 'parent_answer_id' not in answers_by_id[
                                        child_answer_id]:
                                    self.fail(
                                        "Child answer '%s' does not define parent_answer_id '%s' in schema %s"
                                        % (child_answer_id, answer_id,
                                           schema_file))
                                if answers_by_id[child_answer_id][
                                        'parent_answer_id'] != answer_id:
                                    self.fail(
                                        "Child answer '%s' defines incorrect parent_answer_id '%s' in schema %s: "
                                        "Should be '%s" %
                                        (child_answer_id,
                                         answers_by_id[child_answer_id]
                                         ['parent_answer_id'], schema_file,
                                         answer_id))
Exemplo n.º 3
0
    def test_generate_month_year_date_form_creates_empty_form(self):
        survey = load_schema_file("test_dates.json")
        block_json = SchemaHelper.get_block(survey, 'date-block')
        error_messages = SchemaHelper.get_messages(survey)

        answers = SchemaHelper.get_answers_by_id_for_block(block_json)

        form = get_month_year_form(answers['month-year-answer'],
                                   error_messages=error_messages)

        self.assertFalse(hasattr(form, 'day'))
        self.assertTrue(hasattr(form, 'month'))
        self.assertTrue(hasattr(form, 'year'))