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)
def index(self, *args, **kwargs): try: companies = CompanyAlchemy.get_validated_companies() except NoResultFound: companies = None return dict(companies=companies, company_search_form=CompaniesResearchForm)
def details(self, company_id, *args, **kwargs): try: company = CompanyAlchemy.get_validated_company(company_id) except NoResultFound: raise HTTPNotFound() except Exception as exc: logging.getLogger(__name__).log(logging.ERROR, exc) raise HTTPNotFound() else: return dict(company=company)
def _build_company_obj(self, **kwargs): company = CompanyAlchemy() company.id = slugify(kwargs["company_name"]) company.name = kwargs["company_name"] company.logo_url = kwargs["company_logo"] company.url = kwargs["company_url"] company.description = kwargs["company_description"] company.technologies = self._parse_technologies(kwargs["company_technologies"]) city_dict = json.loads(kwargs["company_city"]) company.address = self._format_address(kwargs["company_street"], city_dict["name"], city_dict["country"]) company.address_is_valid = True company.email = kwargs["company_email"] company.phone = kwargs["company_phone"] return company
def _build_company_obj(self, **kwargs): company = CompanyAlchemy() company.id = slugify(kwargs['company_name']) company.name = kwargs['company_name'] company.logo_url = kwargs['company_logo'] company.url = kwargs['company_url'] company.description = kwargs['company_description'] company.technologies = self._parse_technologies( kwargs['company_technologies']) city_dict = json.loads(kwargs['company_city']) company.address = self._format_address( kwargs['company_street'], city_dict['name'], city_dict['country']) company.address_is_valid = True company.email = kwargs['company_email'] company.phone = kwargs['company_phone'] return company