def validate(day, month, year): """Specifies which validation methods should be called for each input field. parameters: - day: The day the charge was added. This is an optional field on the form. - month: The month the charge was added. This is an optional field on the form. - year: The year the charge was added. This is an optional field on the form. returns: dict: An instance of ValidationErrorBuilder with a ValidationError dict and a heading summary message. """ validation_error_builder = ValidationErrorBuilder() if day or month or year: FieldsetValidator([day, month, year], 'date', 'Date', validation_error_builder) \ .is_valid_date() FieldValidator(year, "date", 'Date', validation_error_builder, summary_message="Date is invalid", inline_message="Year must be in the format YYYY")\ .is_year_format() FieldsetValidator([day, month, year], 'date', 'Date', validation_error_builder, summary_message="Date is invalid", inline_message="Date cannot be a future date") \ .is_past_date() return validation_error_builder.get()
def test_is_required_with_invalid_input(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator(INVALID_FIELD_SET, VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .is_required() validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 1) self.assertTrue(VALID_FIELD_NAME in validation_errors)
def test_is_past_date_with_future_date(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator([VALID_DAY, VALID_MONTH, VALID_FUTURE_YEAR], VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .is_past_date() validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 1) self.assertTrue(VALID_FIELD_NAME in validation_errors)
def test_is_valid_date_does_not_add_error_when_date_valid(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator([VALID_DAY, VALID_MONTH, VALID_PAST_YEAR], VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .is_valid_date() validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 0) self.assertTrue(VALID_FIELD_NAME not in validation_errors)
def test_is_valid_date_adds_error_when_date_invalid(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator([VALID_DAY, VALID_MONTH], VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .is_valid_date() validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 1) self.assertTrue(VALID_FIELD_NAME in validation_errors)
def test_has_enough_fields_populated_equal(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator([VALID_DATA, VALID_DATA, EMPTY_STRING, EMPTY_STRING], VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .has_enough_fields_populated(2) validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 0) self.assertTrue(VALID_FIELD_NAME not in validation_errors)
def test_is_future_date_with_current_date(self): validation_error_builder = ValidationErrorBuilder() FieldsetValidator([CURRENT_DAY, CURRENT_DAY, CURRENT_DAY], VALID_FIELD_NAME, VALID_DISPLAY_NAME, validation_error_builder) \ .is_future_date() validation_errors = validation_error_builder.get().errors self.assertEqual(len(validation_errors), 1) self.assertTrue(VALID_FIELD_NAME in validation_errors)
def compare_definitive_and_temporary_dates(definitive_date, temporary_date, error_builder): if CommonValidator.is_valid_date( definitive_date) and CommonValidator.is_valid_date(temporary_date): FieldsetValidator(definitive_date, 'tribunal_definitive_certificate_date', None, error_builder, summary_message='Date (definitive certificate) must come ' 'after date (temporary certificate).', inline_message='Date (definitive certificate) must come ' 'after date (temporary certificate).')\ .is_later_than_date(temporary_date)
def validate(form, files): form_b_name = "Form B" court_order_name = "Court Order" definitive_certificate_name = "Definitive Certificate" validation_error_builder = ValidationErrorBuilder() """ Form Fields """ # Selected Certificates vary_lon_options = form.getlist('vary-lon-options') # Date of Definitive Certificate definitive_cert_day = form.get('definitive_cert_day') definitive_cert_month = form.get('definitive_cert_month') definitive_cert_year = form.get('definitive_cert_year') tribunal_definitive_certificate_date = [ definitive_cert_day, definitive_cert_month, definitive_cert_year ] """ Files """ definitive_certificate = files.get('definitive-certificate-file-input') form_b = files.get('form-b-file-input') court_order = files.get('court-order-file-input') FieldValidator(vary_lon_options, 'vary-lon-options', None, validation_error_builder, summary_message='Choose one option', inline_message='Choose one option') \ .is_required() if definitive_certificate_name in vary_lon_options: FieldsetValidator(tribunal_definitive_certificate_date, 'tribunal_definitive_certificate_date', None, validation_error_builder, summary_message='Date is invalid', inline_message='Check that the date is in Day/Month/Year format') \ .is_valid_date() FieldsetValidator(tribunal_definitive_certificate_date, 'tribunal_definitive_certificate_date', None, validation_error_builder, summary_message='Date is invalid', inline_message='Date cannot be a future date') \ .is_past_or_present_date() FieldValidator(definitive_certificate, 'definitive-certificate-file-input', 'Definitive Certificate', validation_error_builder, inline_message="Upload a document for Definitive Certificate", summary_message="Upload a document for Definitive Certificate") \ .is_required() FieldValidator(definitive_certificate, 'definitive-certificate-file-input', "Definitive Certificate", validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() if form_b_name in vary_lon_options: FieldValidator(form_b, 'form-b-file-input', 'Form B', validation_error_builder, inline_message="Upload a document for Form B", summary_message="Upload a document for Form B") \ .is_required() FieldValidator(form_b, 'form-b-cancel-lon-file-input', "Form B", validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() if court_order_name in vary_lon_options: FieldValidator(court_order, 'court-order-file-input', "Court Order", validation_error_builder, inline_message="Upload a document for Court Order", summary_message="Upload a document for Court Order") \ .is_required() FieldValidator(court_order, 'court-order-file-input', "Court Order", validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() return validation_error_builder.get()
def validate(form, files): definitive_cert = "Definitive LON certificate" temporary_cert = "Temporary LON certificate" validation_error_builder = ValidationErrorBuilder() """ Form Fields """ # Selected Certificates certificates = form.getlist('certificate') # Date of Temporary Certificate temp_cert_day = form.get('temp_cert_day') temp_cert_month = form.get('temp_cert_month') temp_cert_year = form.get('temp_cert_year') tribunal_temporary_certificate_date = [ temp_cert_day, temp_cert_month, temp_cert_year ] # Expiry of Temporary Certificate temp_expiry_day = form.get('temp_expiry_day') temp_expiry_month = form.get('temp_expiry_month') temp_expiry_year = form.get('temp_expiry_year') # Date of Definitive Certificate definitive_cert_day = form.get('definitive_cert_day') definitive_cert_month = form.get('definitive_cert_month') definitive_cert_year = form.get('definitive_cert_year') tribunal_definitive_certificate_date = [ definitive_cert_day, definitive_cert_month, definitive_cert_year ] """ Files """ form_a = files.get('form-a-file-input') definitive_certificate = files.get('definitive-lon-cert-file-input') temporary_certificate = files.get('temporary-lon-cert-file-input') FieldValidator(certificates, 'certificate', None, validation_error_builder, summary_message='Choose one option', inline_message='Choose one option') \ .is_required() if definitive_cert in certificates: FieldsetValidator(tribunal_definitive_certificate_date, 'tribunal_definitive_certificate_date', None, validation_error_builder, summary_message='Date is invalid (certificate date, definitive certificate)', inline_message='Check that the date is in Day/Month/Year format') \ .is_valid_date() FieldsetValidator(tribunal_definitive_certificate_date, 'tribunal_definitive_certificate_date', None, validation_error_builder, summary_message='Date is invalid (certificate date, definitive certificate)', inline_message='Date cannot be a future date') \ .is_past_or_present_date() FieldValidator(definitive_certificate, 'definitive-lon-cert-file-input', 'Definitive Certificate', validation_error_builder, inline_message="Upload a document for Definitive LON certificate", summary_message="Upload a document for Definitive LON certificate") \ .is_required() FieldValidator(definitive_certificate, 'definitive-lon-cert-file-input', "Definitive Certificate", validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() if temporary_cert in certificates: FieldsetValidator(tribunal_temporary_certificate_date, 'tribunal_temporary_certificate_date', None, validation_error_builder, summary_message='Date is invalid (certificate date, temporary certificate)', inline_message='Check that the date is in Day/Month/Year format') \ .is_valid_date() FieldsetValidator(tribunal_temporary_certificate_date, 'tribunal_temporary_certificate_date', None, validation_error_builder, summary_message='Date is invalid (certificate date, temporary certificate)', inline_message='Date cannot be a future date') \ .is_past_or_present_date() tribunal_temporary_certificate_expiry_date = [ temp_expiry_day, temp_expiry_month, temp_expiry_year ] FieldsetValidator(tribunal_temporary_certificate_expiry_date, 'tribunal_temporary_certificate_expiry_date', None, validation_error_builder, summary_message='Date is invalid (expiry date, temporary certificate)', inline_message='Check that the date is in Day/Month/Year format') \ .is_valid_date() FieldValidator(temporary_certificate, 'temporary-lon-cert-file-input', "Temporary Certificate", validation_error_builder, inline_message="Upload a document for Temporary LON certificate", summary_message="Upload a document for Temporary LON certificate") \ .is_required() FieldValidator(temporary_certificate, 'temporary-lon-cert-file-input', "Temporary Certificate", validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() if definitive_cert in certificates and temporary_cert in certificates: compare_definitive_and_temporary_dates( tribunal_definitive_certificate_date, tribunal_temporary_certificate_date, validation_error_builder) FieldValidator(form_a, 'form-a-file-input', 'form_a', validation_error_builder, inline_message="Upload a file for Plan A and colour plan", summary_message="Upload a file for Plan A and colour plan") \ .is_required() FieldValidator(form_a, 'form-a-file-input', 'form_a', validation_error_builder, inline_message='Upload a different file type. Accepted file formats are: pdf', summary_message="File not recognised") \ .is_pdf() return validation_error_builder.get()