def get_month_year_form(answer, error_messages): """ Returns a month year form metaclass with appropriate validators. Used in both date and date range form creation. :param answer: The answer on which to base this form :param error_messages: The messages to use upon this form during validation :return: The generated MonthYearDateForm metaclass """ class MonthYearDateForm(Form): year = StringField() validate_with = [OptionalForm()] if answer['mandatory'] is True: error_message = error_messages['MANDATORY'] if 'validation' in answer and 'messages' in answer['validation'] \ and 'MANDATORY' in answer['validation']['messages']: error_message = answer['validation']['messages']['MANDATORY'] validate_with = [DateRequired(message=error_message)] if 'validation' in answer and 'messages' in answer['validation'] \ and 'INVALID_DATE' in answer['validation']['messages']: error_message = answer['validation']['messages']['INVALID_DATE'] validate_with += [MonthYearCheck(error_message)] else: validate_with += [MonthYearCheck()] # Set up all the calendar month choices for select month_choices = [('', 'Select month')] + [(str(x), calendar.month_name[x]) for x in range(1, 13)] MonthYearDateForm.month = SelectField(choices=month_choices, default='', validators=validate_with) return MonthYearDateForm
def get_date_form(answer=None, to_field_data=None, validate_range=False, error_messages=None): """ Returns a date form metaclass with appropriate validators. Used in both date and date range form creation. :param error_messages: The messages during validation :param answer: The answer on which to base this form :param to_field_data: The data coming from the :param validate_range: Whether the dateform should add a daterange validator :return: The generated DateForm metaclass """ class DateForm(Form): # Set up all the calendar month choices for select MONTH_CHOICES = [('', 'Select month')] + [ (str(x), calendar.month_name[x]) for x in range(1, 13) ] month = SelectField(choices=MONTH_CHOICES, default='') year = StringField() validate_with = [validators.Optional()] if not error_messages: date_messages = {} else: date_messages = error_messages.copy() if answer['mandatory'] is True: if 'validation' in answer and 'messages' in answer['validation'] \ and 'MANDATORY' in answer['validation']['messages']: date_messages['MANDATORY'] = answer['validation']['messages'][ 'MANDATORY'] validate_with = [DateRequired(message=date_messages['MANDATORY'])] if 'validation' in answer and 'messages' in answer['validation'] \ and 'INVALID_DATE' in answer['validation']['messages']: date_messages['INVALID_DATE'] = answer['validation']['messages'][ 'INVALID_DATE'] validate_with += [DateCheck(date_messages['INVALID_DATE'])] if validate_range and to_field_data: validate_with += [ DateRangeCheck(to_field_data=to_field_data, messages=date_messages) ] DateForm.day = StringField(validators=validate_with) return DateForm
def test_valid__year(self): validator = DateRequired() mock_form = Mock() mock_form.year.data = '2017' mock_field = Mock() try: validator(mock_form, mock_field) except StopValidation: self.fail('Valid date raised StopValidation')
def test_date_required_empty(self): validator = DateRequired() mock_form = Mock() mock_form.day.data = '' mock_form.month.data = '' mock_form.year.data = '' mock_field = Mock() with self.assertRaises(StopValidation) as ite: validator(mock_form, mock_field) self.assertEqual(error_messages['MANDATORY_DATE'], str(ite.exception))
def test_date_year_required_empty(self): validator = DateRequired() class TestYearSpec: year = None mock_form = Mock(spec=TestYearSpec) mock_form.year.data = '' mock_field = Mock() with self.assertRaises(StopValidation) as ite: validator(mock_form, mock_field) self.assertEqual(error_messages['MANDATORY_DATE'], str(ite.exception))
def test_valid_date(self): validator = DateRequired() mock_form = Mock() mock_form.day.data = '01' mock_form.month.data = '01' mock_form.year.data = '2015' mock_field = Mock() try: validator(mock_form, mock_field) except StopValidation: self.fail("Valid date raised StopValidation")
def get_date_form(answer=None, error_messages=None): """ Returns a date form metaclass with appropriate validators. Used in both date and date range form creation. :param error_messages: The messages during validation :param answer: The answer on which to base this form :return: The generated DateForm metaclass """ class DateForm(Form): day = StringField() year = StringField() validate_with = [OptionalForm()] if not error_messages: date_messages = {} else: date_messages = error_messages.copy() if answer['mandatory'] is True: if 'validation' in answer and 'messages' in answer['validation'] \ and 'MANDATORY' in answer['validation']['messages']: date_messages['MANDATORY'] = answer['validation']['messages']['MANDATORY'] validate_with = [DateRequired(message=date_messages['MANDATORY'])] if 'validation' in answer and 'messages' in answer['validation'] \ and 'INVALID_DATE' in answer['validation']['messages']: date_messages['INVALID_DATE'] = answer['validation']['messages']['INVALID_DATE'] validate_with += [DateCheck(date_messages['INVALID_DATE'])] # Set up all the calendar month choices for select month_choices = [('', 'Select month')] + [(str(x), calendar.month_name[x]) for x in range(1, 13)] DateForm.month = SelectField(choices=month_choices, default='', validators=validate_with) return DateForm
def validate_mandatory_date(error_messages, answer): error_message = get_bespoke_message( answer, 'MANDATORY_DATE') or error_messages['MANDATORY_DATE'] validate_with = [DateRequired(message=error_message)] return validate_with