def build_view_context_for_calculated_summary(metadata, schema, answer_store, schema_context, block_type, variables, current_location): block = schema.get_block(current_location.block_id) section_list = _build_calculated_summary_section_list( schema, block, current_location.group_id) context = build_view_context_for_summary(schema, section_list, answer_store, metadata, block_type, variables, schema_context) context['summary']['groups'] = [ context['summary']['groups'][current_location.group_instance] ] schema_context['group_instance_id'] = get_group_instance_id( schema, answer_store, current_location) rendered_block = renderer.render(block, **schema_context) formatted_total = _get_formatted_total(context['summary'].get( 'groups', [])) context['summary'].update({ 'calculated_question': _get_calculated_question(rendered_block['calculation'], answer_store, schema, metadata, current_location.group_instance, formatted_total), 'title': get_question_title(rendered_block, answer_store, schema, metadata, current_location.group_instance) % dict(total=formatted_total), }) return context
def test_get_question_title_returns_empty_title(self): expected_value = '' test_schema = {'title': expected_value} actual_value = get_question_title(test_schema, None, None, None, None) self.assertEqual(expected_value, actual_value)
def test_get_question_title_no_title_results_in_titles_resolution(self): expected_value = 'TitlesElement' test_schema = {} with patch('app.templating.utils.get_title_from_titles', return_value='TitlesElement'): actual_value = get_question_title(test_schema, None, None, None, None) self.assertEqual(expected_value, actual_value)
def test_get_question_title_title_returned_if_present(self): expected_value = 'MyTitle' test_schema = {'title': expected_value} actual_value = get_question_title(test_schema, None, None, None, None) self.assertEqual(expected_value, actual_value)
def build_view_context_for_calculated_summary(metadata, schema, answer_store, schema_context, block_type, variables, csrf_token, current_location): rendered_block = renderer.render( schema.get_block(current_location.block_id), **schema_context) section_list = _build_calculated_summary_section_list( schema, rendered_block, current_location.group_id) context = build_view_context_for_summary(schema, section_list, answer_store, metadata, csrf_token, block_type, variables) formatted_total = _get_formatted_total(rendered_block, answer_store, current_location.group_instance, schema) context['summary'].update({ 'calculated_question': _get_calculated_question(rendered_block['calculation'], answer_store, schema, metadata, current_location.group_instance, formatted_total), 'title': get_question_title(rendered_block, answer_store, schema, metadata, current_location.group_instance) % dict(total=formatted_total), }) return context
def __init__(self, question_schema, answer_store, metadata, schema, group_instance): self.id = question_schema['id'] + '-' + str(group_instance) self.type = question_schema['type'] self.schema = schema self.answer_schemas = iter(question_schema['answers']) self.title = (get_question_title(question_schema, answer_store, schema, metadata, group_instance=group_instance) or question_schema['answers'][0]['label']) self.number = question_schema.get('number', None) self.answers = self._build_answers(answer_store, question_schema, group_instance)
def __init__(self, question_schema, answer_store, metadata, schema): self.id = question_schema['id'] self.type = question_schema['type'] answer_schemas = iter(question_schema['answers']) # Using group instance as 0 for now as summary rendering context only has knowledge of current locations group instance (i.e. 0) self.title = get_question_title(question_schema, answer_store, schema, metadata, group_instance=0) or question_schema['answers'][0]['label'] self.number = question_schema.get('number', None) self.answers = self._build_answers(answer_store, question_schema, answer_schemas)
def get_answer_fields(question, data, error_messages, schema, answer_store, metadata, group_instance, group_instance_id): answer_fields = {} for answer in question.get('answers', []): if 'parent_answer_id' in answer and answer['parent_answer_id'] in data and \ data[answer['parent_answer_id']] == 'Other': answer['mandatory'] = \ next(a['mandatory'] for a in question['answers'] if a['id'] == answer['parent_answer_id']) name = answer.get('label') or get_question_title(question, answer_store, schema, metadata, group_instance, group_instance_id) answer_fields[answer['id']] = get_field(answer, name, error_messages, answer_store, metadata, group_instance=group_instance) return answer_fields
def get_name_form(schema, block_json, metadata, group_instance): class NameForm(Form): pass for question in schema.get_questions_for_block(block_json): for answer in question['answers']: guidance = answer.get('guidance', '') label = answer.get('label') or get_question_title( question, answer_store, schema, metadata, group_instance) field = get_string_field(answer, label, guidance, schema.error_messages) setattr(NameForm, answer['id'], field) return NameForm
def _get_calculated_question(calculation, answer_store, schema, metadata, group_instance, formatted_total): calculation_title = get_question_title(calculation, answer_store, schema, metadata, group_instance) return { 'title': calculation_title, 'id': 'calculated-summary-question', 'answers': [ { 'id': 'calculated-summary-answer', 'value': formatted_total, }, ], }
def test_evaluates_when_rule_if_present(self): expected_value = 'MyTitle' test_schema = { 'titles': [{ 'value': expected_value, 'when': [{ 'id': 'behalf-of-answer', 'condition': 'equals', 'value': 'chad' }] }] } with patch('app.templating.utils.evaluate_when_rules', return_value=True): actual_value = get_question_title(test_schema, None, None, None, None) self.assertEqual(expected_value, actual_value)
def get_page_title_for_location(schema, current_location, metadata, answer_store): block = schema.get_block(current_location.block_id) if block['type'] == 'Interstitial': group = schema.get_group(current_location.group_id) page_title = '{group_title} - {survey_title}'.format( group_title=group['title'], survey_title=schema.json['title']) elif block['type'] == 'Question': first_question = next(schema.get_questions_for_block(block)) question_title = get_question_title(first_question, answer_store, schema, metadata, current_location.group_instance) page_title = '{question_title} - {survey_title}'.format( question_title=question_title, survey_title=schema.json['title']) else: page_title = schema.json['title'] return TemplateRenderer.safe_content(page_title)
def get_answer_fields(question, data, error_messages, schema, answer_store, metadata, group_instance, group_instance_id): answer_fields = {} for answer in question.get('answers', []): for option in answer.get('options', []): disable_validation = False if 'detail_answer' in option: detail_answer = option['detail_answer'] detail_answer_error_messages = detail_answer['validation'][ 'messages'] if detail_answer.get( 'validation') else error_messages if isinstance(data, MultiDict): option_value_in_data = option['value'] in data.to_dict( flat=False).get(answer['id'], []) else: option_value_in_data = option['value'] in dict(data).get( answer['id'], []) if not option_value_in_data: disable_validation = True answer_fields[detail_answer['id']] = get_field( detail_answer, detail_answer.get('label'), detail_answer_error_messages, answer_store, metadata, group_instance=group_instance, disable_validation=disable_validation) name = answer.get('label') or get_question_title( question, answer_store, schema, metadata, group_instance, group_instance_id) answer_fields[answer['id']] = get_field(answer, name, error_messages, answer_store, metadata, group_instance=group_instance) return answer_fields
def test_default_titles_if_no_when_clauses(self): expected_value = 'MyTitle' test_schema = {'titles': [{'value': expected_value}]} actual_value = get_question_title(test_schema, None, None, None, None) self.assertEqual(expected_value, actual_value)