Exemplo n.º 1
0
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
Exemplo n.º 2
0
    def test_month_year_date_validator_valid(self):
        validator = MonthYearCheck()

        mock_form = Mock()
        mock_form.month.data = "01"
        mock_form.year.data = "2017"

        mock_field = Mock()

        try:
            validator(mock_form, mock_field)
        except ValidationError:
            self.fail("Valid date raised ValidationError")
Exemplo n.º 3
0
    def test_month_year_date_validator_none(self):
        validator = MonthYearCheck()

        mock_form = Mock()
        mock_form.month.data = None
        mock_form.year.data = None

        mock_field = Mock()

        with self.assertRaises(ValidationError) as ite:
            validator(mock_form, mock_field)

        self.assertEqual(error_messages['INVALID_DATE'], str(ite.exception))
    def test_month_year_date_validator_valid(self):
        validator = MonthYearCheck()

        mock_form = Mock()
        mock_form.month.data = '01'
        mock_form.year.data = '2017'

        mock_field = Mock()

        try:
            validator(mock_form, mock_field)
        except StopValidation:
            self.fail('Valid date raised StopValidation')
    def test_month_year_date_validator_invalid_month(self):
        validator = MonthYearCheck()

        mock_form = Mock()
        mock_form.month.data = '13'
        mock_form.year.data = '2017'

        mock_field = Mock()

        with self.assertRaises(StopValidation) as ite:
            validator(mock_form, mock_field)

        self.assertEqual(error_messages['INVALID_DATE'], str(ite.exception))
Exemplo n.º 6
0
def get_month_year_form(answer,
                        answer_store,
                        metadata,
                        error_messages,
                        group_instance=0):
    """
    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()

        @property
        def data(self):
            data = super().data
            if not data['year'] and not data['month']:
                return None

            return '{:04d}-{:02d}'.format(
                int(data['year']),
                int(data['month']),
            )

    validate_with = [OptionalForm()]

    if answer['mandatory'] is True:
        validate_with = validate_mandatory_date(error_messages, answer)

    error_message = get_bespoke_message(answer, 'INVALID_DATE')
    validate_with.append(MonthYearCheck(error_message))

    if 'minimum' in answer or 'maximum' in answer:
        min_max_validation = validate_min_max_date(
            answer,
            answer_store,
            metadata,
            'yyyy-mm',
            group_instance=group_instance)
        validate_with.append(min_max_validation)

    MonthYearDateForm.month = get_month_selection_field(validate_with)

    return MonthYearDateForm