def test_get_mandatory_validator_optional(self): answer = { 'mandatory': False } validate_with = get_mandatory_validator(answer, None) self.assertIsInstance(validate_with[0], validators.Optional)
def test_get_mandatory_validator_mandatory(self): answer = {'mandatory': True} validate_with = get_mandatory_validator( answer, {'MANDATORY': 'This is the default mandatory message'}) self.assertIsInstance(validate_with[0], validators.InputRequired) self.assertEqual(validate_with[0].message, 'This is the default mandatory message')
def test_get_mandatory_validator_mandatory(self): answer = { 'mandatory': True } validate_with = get_mandatory_validator(answer, { 'MANDATORY_TEXTFIELD': 'This is the default mandatory message' }, 'MANDATORY_TEXTFIELD') self.assertIsInstance(validate_with[0], ResponseRequired) self.assertEqual(validate_with[0].message, 'This is the default mandatory message')
def generate_relationship_form(block_json, number_of_entries, data, error_messages): answer = SchemaHelper.get_first_answer_for_block(block_json) class HouseHoldRelationshipForm(FlaskForm): question_errors = {} def map_errors(self): ordered_errors = [] if len(self.errors) > 0: for answer_id, error_list in self.errors.items(): for errors in error_list: for error in errors: ordered_errors.append((answer_id, error)) return ordered_errors def answer_errors(self, input_id): return [ error[1] for error in self.map_errors() if input_id == error[0] ] def serialise(self, location): """ Returns a list of answers representing the form data :param location: The location to associate the form data with :return: """ list_field = getattr(self, answer['id']) return serialise_relationship_answers(location, answer['id'], list_field.data) choices = [('', 'Select relationship')] + build_choices(answer['options']) field = FieldList(SelectField( label=answer.get('guidance'), description=answer.get('label'), choices=choices, default='', validators=get_mandatory_validator(answer, error_messages), ), min_entries=number_of_entries) setattr(HouseHoldRelationshipForm, answer['id'], field) if data: form = HouseHoldRelationshipForm(MultiDict(data)) else: form = HouseHoldRelationshipForm() return form
def test_get_mandatory_validator_mandatory_with_error(self): answer = { 'mandatory': True, 'validation': { 'messages': { 'MANDATORY': 'This is the mandatory message for an answer' } } } validate_with = get_mandatory_validator(answer, { 'MANDATORY': 'This is the default mandatory message' }) self.assertIsInstance(validate_with[0], ResponseRequired) self.assertEqual(validate_with[0].message, 'This is the mandatory message for an answer')
def generate_relationship_form(schema, block_json, relationship_choices, data, group_instance, group_instance_id): answer = schema.get_answers_for_block(block_json['id'])[0] class HouseHoldRelationshipForm(FlaskForm): question_errors = {} def map_errors(self): ordered_errors = [] if self.errors: for answer_id, error_list in self.errors.items(): for errors in error_list: for error in errors: ordered_errors.append((answer_id, error)) return ordered_errors def answer_errors(self, input_id): return [ error[1] for error in self.map_errors() if input_id == error[0] ] def serialise(self): """ Returns a list of answers representing the form data :param location: The location to associate the form data with :return: """ list_field = getattr(self, answer['id']) return serialise_relationship_answers(answer['id'], list_field.data, group_instance, group_instance_id) choices = [('', _('Select relationship'))] + build_choices( answer['options']) labels = [] for choice in relationship_choices: labels.append( answer.get('label') % dict(current_person=choice[0], other_person=choice[1])) field = FieldList(RelationshipSelectField( label=labels, choices=choices, default='', validators=get_mandatory_validator(answer, schema.error_messages, 'MANDATORY_TEXTFIELD'), ), min_entries=len(relationship_choices)) setattr(HouseHoldRelationshipForm, answer['id'], field) if data: form = HouseHoldRelationshipForm(MultiDict(data)) else: form = HouseHoldRelationshipForm() return form