def _validate_python(self, value, state=None): regex = re.compile('^[0-9]{3}(( [0-9]{3}){2})$', re.IGNORECASE) # We first check the string format if not value or not regex.match(value): raise ValidationError('wrong_format', self) # The Siren number is formatted correctly, perform the validity check tmp = value.replace(' ', '') digits = [int(x) for x in tmp] digits.reverse() validity = 0 for i, digit in enumerate(digits): to_sum = str(digit * 2) if (i + 1) % 2 == 0 else str(digit) for figure in to_sum: validity += int(figure) if validity % 10 != 0: raise ValidationError('invalid_siren', self) # Check for duplicates in the database try: CompanyAlchemy.get_company(value) except NoResultFound: # There are no duplicates, the validation is therefore successful pass else: # This Siren number is already present in the database, notify the # user that the company he's trying to register already exists. raise ValidationError('duplicate_siren', self)
def _validate_python(self, value, state=None): if len(value) > self.max_length: raise ValidationError('toolong', self) name_slug = slugify(value) # Check for duplicates in the database try: CompanyAlchemy.get_company(name_slug) except NoResultFound: # There are no duplicates, the validation is therefore successful pass else: # This company slug name is already present in the database, notify # the user that the company he's trying to register already exists. raise ValidationError('already_exists', self)