def post_form_for_location(block_json, location, answer_store, request_form, error_messages, disable_mandatory=False): """ Returns the form necessary for the location given a post 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 :param request_form: 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': return generate_household_composition_form(block_json, request_form, error_messages), None elif location.block_id in ['relationships', 'household-relationships']: choices = build_relationship_choices(answer_store, location.group_instance) form = generate_relationship_form(block_json, len(choices), request_form, error_messages) return form, {'relation_instances': choices} else: data = clear_other_text_field( request_form, SchemaHelper.get_questions_for_block(block_json)) return generate_form(block_json, data, error_messages), None
def _evaluate_skip_conditions(block_json, location, answer_store, metadata): for question in SchemaHelper.get_questions_for_block(block_json): if 'skip_condition' in question: skip_question = evaluate_skip_condition([question['skip_condition']], metadata, answer_store, location.group_instance) question['skipped'] = skip_question for answer in question['answers']: if answer['mandatory'] and skip_question: answer['mandatory'] = False return block_json
def get_name_form(block_json, error_messages): class NameForm(Form): pass for question in SchemaHelper.get_questions_for_block(block_json): for answer in question['answers']: guidance = answer.get('guidance', '') label = answer.get('label') or question.get('title') field = get_string_field(answer, label, guidance, error_messages) setattr(NameForm, answer['id'], field) return NameForm
def get_page_title_for_location(schema_json, current_location): block = SchemaHelper.get_block_for_location(schema_json, current_location) if block['type'] == 'Interstitial': group = SchemaHelper.get_group(schema_json, current_location.group_id) page_title = '{group_title} - {survey_title}'.format( group_title=group['title'], survey_title=schema_json['title']) elif block['type'] == 'Questionnaire': first_question = next(SchemaHelper.get_questions_for_block(block)) page_title = '{question_title} - {survey_title}'.format( question_title=first_question['title'], survey_title=schema_json['title']) else: page_title = schema_json['title'] return TemplateRenderer.safe_content(page_title)
def generate_form(block_json, data, error_messages): class QuestionnaireForm(FlaskForm): def map_errors(self): ordered_errors = [] answer_json_list = SchemaHelper.get_answers_for_block(block_json) for answer_json in answer_json_list: if answer_json[ 'id'] in self.errors and 'parent_answer_id' not in answer_json: ordered_errors += map_subfield_errors( self.errors, answer_json['id']) if 'options' in answer_json and 'parent_answer_id' not in answer_json: ordered_errors += map_child_option_errors( self.errors, answer_json) return ordered_errors def answer_errors(self, input_id): return [ error[1] for error in self.map_errors() if input_id == error[0] ] answer_fields = {} for question in SchemaHelper.get_questions_for_block(block_json): if question['type'] == 'DateRange': answer_fields.update( get_date_range_field(question, data, error_messages)) else: answer_fields.update( get_answer_fields(question, data, error_messages)) for answer_id, field in answer_fields.items(): setattr(QuestionnaireForm, answer_id, field) if data: form = QuestionnaireForm(MultiDict(data), meta={'csrf': False}) else: form = QuestionnaireForm(meta={'csrf': False}) return form
def map_errors(self): ordered_errors = [] question_json_list = SchemaHelper.get_questions_for_block( self.block_json) for question_json in question_json_list: if question_json['id'] in self.question_errors: ordered_errors += [(question_json['id'], self.question_errors[question_json['id']])] for answer_json in question_json['answers']: if answer_json[ 'id'] in self.errors and 'parent_answer_id' not in answer_json: ordered_errors += map_subfield_errors( self.errors, answer_json['id']) if 'options' in answer_json and 'parent_answer_id' not in answer_json: ordered_errors += map_child_option_errors( self.errors, answer_json) return ordered_errors
def generate_form(block_json, data, error_messages): answer_fields = {} class DynamicForm(QuestionnaireForm): date_ranges = [] for question in SchemaHelper.get_questions_for_block(block_json): if question['type'] == 'DateRange': DynamicForm.date_ranges += [ (question['id'], question['answers'][0]['id'], question['answers'][1]['id']) ] answer_fields.update(get_answer_fields(question, data, error_messages)) for answer_id, field in answer_fields.items(): setattr(DynamicForm, answer_id, field) if data: form = DynamicForm(block_json, MultiDict(data)) else: form = DynamicForm(block_json) return form