Exemplo n.º 1
0
def validate_min_max_date(answer,
                          answer_store,
                          metadata,
                          date_format,
                          group_instance=0):
    messages = None
    if 'validation' in answer:
        messages = answer['validation'].get('messages')
    minimum_date, maximum_date = get_dates_for_single_date_period_validation(
        answer, answer_store, metadata, group_instance=group_instance)

    display_format = 'd MMMM YYYY'
    if date_format == 'yyyy-mm':
        display_format = 'MMMM YYYY'
        minimum_date = minimum_date.replace(
            day=1) if minimum_date else None  # First day of Month
        maximum_date = maximum_date + relativedelta(
            day=31) if maximum_date else None  # Last day of month
    elif date_format == 'yyyy':
        display_format = 'YYYY'
        minimum_date = minimum_date.replace(
            month=1, day=1) if minimum_date else None  # January 1st
        maximum_date = maximum_date.replace(
            month=12, day=31) if maximum_date else None  # Last day of december

    return SingleDatePeriodCheck(messages=messages,
                                 date_format=display_format,
                                 minimum_date=minimum_date,
                                 maximum_date=maximum_date)
Exemplo n.º 2
0
    def test_valid_single_date_period():
        minimum_date = convert_to_datetime('2016-03-20')
        maximum_date = convert_to_datetime('2016-03-31')
        validator = SingleDatePeriodCheck(minimum_date=minimum_date,
                                          maximum_date=maximum_date)

        mock_form = Mock()
        mock_form.data = '2016-03-26'

        mock_field = Mock()

        validator(mock_form, mock_field)
Exemplo n.º 3
0
    def test_invalid_single_date_period_with_bespoke_message(self):
        maximum_date = convert_to_datetime('2016-03-31')
        message = {'SINGLE_DATE_PERIOD_TOO_LATE': 'Test %(max)s'}
        validator = SingleDatePeriodCheck(messages=message,
                                          maximum_date=maximum_date)

        mock_form = Mock()
        mock_form.data = '2016-04-29'

        mock_field = Mock()

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

            self.assertEqual('Test 1 April 2016', str(ite.exception))
Exemplo n.º 4
0
    def test_invalid_single_date_period_maximum_date(self):
        maximum_date = convert_to_datetime('2016-03-31')
        validator = SingleDatePeriodCheck(maximum_date=maximum_date)

        mock_form = Mock()
        mock_form.data = '2016-04-29'

        mock_field = Mock()

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

            self.assertEqual(
                error_messages['SINGLE_DATE_PERIOD_TOO_LATE'] %
                dict(max='1 April 2016'), str(ite.exception))