def test_deserialise_relationship_answers(self): expected_form_data = { 'who-is-related-0': 'Husband or Wife', 'who-is-related-1': 'Son or daughter', 'who-is-related-2': 'Unrelated', } serialised_answers = [{ 'group_id': "household-relationships", 'group_instance': 0, 'block_id': 'relationships', 'answer_id': 'who-is-related', 'answer_instance': 0, 'value': 'Husband or Wife' }, { 'group_id': "household-relationships", 'group_instance': 0, 'block_id': 'relationships', 'answer_id': 'who-is-related', 'answer_instance': 1, 'value': 'Son or daughter' }, { 'group_id': "household-relationships", 'group_instance': 0, 'block_id': 'relationships', 'answer_id': 'who-is-related', 'answer_instance': 2, 'value': 'Unrelated' }] deserialised_form_data = deserialise_relationship_answers( serialised_answers) self.assertEquals(expected_form_data, deserialised_form_data)
def get_form_for_location(schema, block_json, location, answer_store, metadata, disable_mandatory=False): # pylint: disable=too-many-locals """ Returns the form necessary for the location given a get request, plus any template arguments :param schema: schema :param block_json: The block json :param location: The location which this form is for :param answer_store: The current answer store :param metadata: metadata :param disable_mandatory: Make mandatory answers optional :return: form, template_args A tuple containing the form for this location and any additional template arguments """ if disable_mandatory: block_json = disable_mandatory_answers(block_json) if location.block_id == 'household-composition': answer_ids = schema.get_answer_ids_for_block(location.block_id) answers = answer_store.filter(answer_ids, location.group_instance) data = deserialise_composition_answers(answers) return generate_household_composition_form(schema, block_json, data, metadata, location.group_instance) group_instance_id = get_group_instance_id(schema, answer_store, location) if schema.block_has_question_type(location.block_id, 'Relationship'): answer_ids = schema.get_answer_ids_for_block(location.block_id) group = schema.get_group(location.group_id) answers = answer_store.filter(answer_ids, location.group_instance) data = deserialise_relationship_answers(answers) # Relationship block only supports 1 question question = schema.get_question(block_json['questions'][0]['id']) answer_ids = [] repeat_rule = group['routing_rules'][0]['repeat'] if 'answer_ids' in repeat_rule: for answer_id in repeat_rule['answer_ids']: answer_ids.append(answer_id) if 'answer_id' in repeat_rule: answer_ids.append(repeat_rule['answer_id']) relationship_choices = build_relationship_choices(answer_ids, answer_store, location.group_instance, question.get('member_label')) form = generate_relationship_form(schema, block_json, relationship_choices, data, location.group_instance, group_instance_id) return form mapped_answers = get_mapped_answers( schema, answer_store, group_instance=location.group_instance, group_instance_id=group_instance_id, block_id=location.block_id, ) return generate_form(schema, block_json, answer_store, metadata, location.group_instance, group_instance_id, data=mapped_answers)
def get_form_for_location(schema, block_json, location, answer_store, metadata, disable_mandatory=False): # pylint: disable=too-many-locals """ Returns the form necessary for the location given a get request, plus any template arguments :param schema: schema :param block_json: The block json :param location: The location which this form is for :param answer_store: The current answer store :param metadata: metadata :param disable_mandatory: Make mandatory answers optional :return: form, template_args A tuple containing the form for this location and any additional template arguments """ if disable_mandatory: block_json = disable_mandatory_answers(block_json) if location.block_id == 'household-composition': answer_ids = schema.get_answer_ids_for_block(location.block_id) answers = answer_store.filter(answer_ids, location.group_instance) data = deserialise_composition_answers(answers) return generate_household_composition_form(schema, block_json, data, metadata, location.group_instance) if location.block_id in ['relationships', 'household-relationships']: answer_ids = schema.get_answer_ids_for_block(location.block_id) answers = answer_store.filter(answer_ids, location.group_instance) data = deserialise_relationship_answers(answers) relationship_choices = build_relationship_choices( answer_store, location.group_instance) form = generate_relationship_form(schema, block_json, relationship_choices, data, location.group_instance) return form mapped_answers = get_mapped_answers( schema, answer_store, group_instance=location.group_instance, block_id=location.block_id, ) return generate_form(schema, block_json, answer_store, metadata, location.group_instance, data=mapped_answers)
def get_form_for_location(block_json, location, answer_store, error_messages, disable_mandatory=False): """ Returns the form necessary for the location given a get request, plus any template arguments :param disable_mandatory: Make mandatory answers optional :param error_messages: The default error messages to use within the form :param block_json: The block json :param location: The location which this form is for :param answer_store: The current answer store :return: form, template_args A tuple containing the form for this location and any additional template arguments """ if disable_mandatory: block_json = disable_mandatory_answers(block_json) if location.block_id == 'household-composition': answers = answer_store.filter(location=location) data = deserialise_composition_answers(answers) return generate_household_composition_form(block_json, data, error_messages), None elif location.block_id in ['relationships', 'household-relationships']: answers = answer_store.filter(location=location) data = deserialise_relationship_answers(answers) choices = build_relationship_choices(answer_store, location.group_instance) form = generate_relationship_form(block_json, len(choices), data, error_messages) return form, {'relation_instances': choices} else: mapped_answers = answer_store.map( group_id=location.group_id, group_instance=location.group_instance, block_id=location.block_id, ) # Form generation expects post like data, so cast answers to strings for answer_id, mapped_answer in mapped_answers.items(): if isinstance(mapped_answer, list): for index, element in enumerate(mapped_answer): mapped_answers[answer_id][index] = str(element) else: mapped_answers[answer_id] = str(mapped_answer) mapped_answers = deserialise_dates(block_json, mapped_answers) return generate_form(block_json, mapped_answers, error_messages), None