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 is_later_than_date(self, date): """Checks if the array of day, month and year strings represents a date later than another.""" day = self.data[0] month = self.data[1] year = self.data[2] own_date = CommonValidator.build_date(day, month, year) other_date = CommonValidator.build_date(date[0], date[1], date[2]) default_message = '{} was not later than date'.format( self.display_name) if not CommonValidator.is_later_than_date(own_date, other_date): self.validation_error_builder.add_error(self, default_message) return self
def is_int(self): default_message = 'Provided number must not have decimal places' if not CommonValidator.is_int(self.data): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_uploaded_filesize_less_than_bytes(self, no_of_bytes): default_message = "File is too big" if self.data and not CommonValidator.is_uploaded_filesize_less_than_bytes( self.data, no_of_bytes): self.validation_error_builder.add_error(self, default_message, self.allow_multiple)
def is_valid_date(self): """Checks if the array of day, month and year strings represents a valid date.""" default_message = '{} is invalid'.format(self.display_name) if not CommonValidator.is_valid_date(self.data): self.validation_error_builder.add_error(self, default_message) return self
def is_positive_number_or_zero(self): default_message = 'Provided number must be a positive number or zero' if not self.__is_parsable(self.data) or \ (self.__is_parsable(self.data) and not CommonValidator.is_positive_number_or_zero(self.data)): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_pdf(self): """Checks if the file provided is a pdf""" default_message = '{} is not a valid pdf'.format(self.display_name) if self.data and not CommonValidator.is_pdf(self.data.content_type): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_required(self): """Checks if the data provided is not empty.""" default_message = '{} is required'.format(self.display_name) if not CommonValidator.is_required(self.data): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_email(self): """Checks if the data provided is a valid email format.""" default_message = '{} is not a valid email address'.format( self.display_name) if self.data and not CommonValidator.is_email(self.data): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def does_string_contain(self, character): default_message = '{} contains the character: {}'.format( self.display_name, character) if self.data and CommonValidator.does_string_contain( self.data, character): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_phone_number(self): """Checks if the data provided is a valid phone number ie. all numeric, allow space.""" default_message = '{} is not a valid phone number'.format( self.display_name) if self.data and not CommonValidator.is_phone_number(self.data): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_length_equal_to(self, length): """Checks if the data provided is equal to a specified length.""" default_message = '{} is not {} characters'.format( self.display_name, str(length)) if self.data and not CommonValidator.is_length_equal_to( self.data, length): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def has_zero_area_polygon_geometry(self): """Checks that the geometry supplied has no zero area polygons""" default_message = '{} contains polygon geometry which has zero area'.format( self.display_name) if self.data and not CommonValidator.check_geometry( self.data, ['zero_area']): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def has_valid_geometry(self): """Checks that the geometry supplied is valid""" default_message = '{} contains geometry which is not valid'.format( self.display_name) if self.data and not CommonValidator.check_geometry( self.data, ['is_valid']): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_number_x_length_y_decimal_places(self, length, decimal_places): """Checks if the data provided is a number up to x characters long with exactly y decimal places.""" default_message = '{} is not a valid number format'.format( self.display_name) if self.data and not CommonValidator.is_number_x_length_y_decimal_places( self.data, length, decimal_places): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_number_with_zero_or_up_to_x_decimal_places(self, decimal_places): """Checks if the data provided is a number with zero X decimal places.""" default_message = '{} is not a valid number format'.format( self.display_name) if self.data and not CommonValidator.is_number_with_zero_or_up_to_x_decimal_places( self.data, decimal_places): self.validation_error_builder.add_error(self, default_message, self.allow_multiple) return self
def is_past_or_present_date(self): """Checks if the array of day, month and year strings represents a date in the past or the current date.""" day = self.data[0] month = self.data[1] year = self.data[2] default_message = '{} was not the current date or in the past'.format( self.display_name) if not CommonValidator.is_past_or_present_date(day, month, year): self.validation_error_builder.add_error(self, default_message) return self
def has_enough_fields_populated(self, no_of_fields): """Checks if a sufficient number of fields are populated in the dataset.""" populated_field_count = 0 for field in self.data: if CommonValidator.is_required(field): populated_field_count += 1 default_message = 'At least {} of {} is required'.format( str(no_of_fields), self.display_name) if populated_field_count < no_of_fields: self.validation_error_builder.add_error(self, default_message) return self
def test_good_geojson_simple(self): result = CommonValidator.check_geometry(GOOD_GEOJSON['features'][1], 'is_simple') self.assertTrue(result)
def test_is_pdf_with_pdf(self): result = CommonValidator.is_pdf(MIME_TYPE['pdf']) self.assertTrue(result)
def test_is_pdf_with_text(self): result = CommonValidator.is_pdf(MIME_TYPE['text']) self.assertFalse(result)
def test_is_positive_number_or_zero_false(self): result = CommonValidator.is_positive_number_or_zero(NEGATIVE_NUMBER) self.assertFalse(result)
def test_bad_geojson_valid(self): result = CommonValidator.check_geometry(BAD_GEOJSON['features'][0], 'is_valid') self.assertFalse(result)
def test_is_positive_number_or_zero_true(self): result = CommonValidator.is_positive_number_or_zero(POSITIVE_NUMBER) self.assertTrue(result)
def test_is_positive_number_or_zero_true_for_zero(self): result = CommonValidator.is_positive_number_or_zero(ZERO) self.assertTrue(result)
def test_is_length_equal_to_false(self): result = CommonValidator.is_length_equal_to(NINE_CHARS, 10) self.assertFalse(result)
def test_is_length_equal_to_true(self): result = CommonValidator.is_length_equal_to(NINE_CHARS, 9) self.assertTrue(result)
def test_bad_geojson_no_geo(self): result = CommonValidator.check_geometry(BAD_GEOJSON['features'][4], 'zero_area') self.assertFalse(result)
def test_good_geojson_zero_area(self): result = CommonValidator.check_geometry(GOOD_GEOJSON['features'][0], 'zero_area') self.assertTrue(result)
def test_bad_geojson_zero_length(self): result = CommonValidator.check_geometry(BAD_GEOJSON['features'][1], 'zero_length') self.assertFalse(result)