<commit_before># -*- coding: utf-8 -*-
import re
from slugify import slugify

import tw2.core as twc
import tw2.forms as twf
import tw2.forms.widgets as tww
from tw2.core.validation import ValidationError

from pyjobsweb.model import CompanyAlchemy
from sqlalchemy.orm.exc import NoResultFound

french_validation_messages = {
    'required': (u'Veuillez saisir une valeur'),
    'decode': (u'Mauvais jeu de caractère reçu; devrait être $encoding'),
    'corrupt': (u"Données du formulaire reçues corrompues; veuillez réessayer "
                u"s'il vous plaît."),
    'childerror': '',  # Children of this widget have errors
}


class CompanyNameValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(CompanyNameValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['already_exists'] = (u'Cette entreprise existe déjà dans '
                                       u'notre base')

    def _validate_python(self, value, state=None):
        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)


class PhoneNumberValidator(twc.RegexValidator):
    def __init__(self, **kwargs):
        super(PhoneNumberValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = (u'Numéro de téléphone invalide')

    regex = re.compile('^0([0-9](\.[0-9]{2}){4})$', re.IGNORECASE)


class SirenValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(SirenValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['wrong_format'] = (u'Format de saisie non respecté')
        self.msgs['invalid_siren'] = (u'Numéro de Siren invalide')
        self.msgs['duplicate_siren'] = (u'Numéro de Siren déjà utilisé')

    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)


class TechnologiesValidator(twc.RegexValidator):
    def __init__(self, **kwargs):
        super(TechnologiesValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = (u'Format du champ invalide')

    regex = re.compile('^(\w|\+|\-)+(\, (\w|\+|\-)+){0,9}$',
                       re.IGNORECASE | re.UNICODE)


class RequiredValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(RequiredValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.required = True


class UrlValidator(twc.UrlValidator):
    def __init__(self, **kwargs):
        super(UrlValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'URL invalide'


class EmailValidator(twc.EmailValidator):
    def __init__(self, **kwargs):
        super(EmailValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'Adresse email invalide'


class NewCompanyForm(twf.Form):
    def __init__(self, **kwargs):
        super(NewCompanyForm, self).__init__(**kwargs)
        self.submit = None
<commit_msg>Code proprer<commit_after># -*- coding: utf-8 -*-
import re
from slugify import slugify

import tw2.core as twc
import tw2.forms as twf
import tw2.forms.widgets as tww
from tw2.core.validation import ValidationError

from pyjobsweb.model import CompanyAlchemy
from sqlalchemy.orm.exc import NoResultFound

french_validation_messages = {
    'required': u'Veuillez saisir une valeur',
    'decode': u'Mauvais jeu de caractère reçu; devrait être $encoding',
    'corrupt': u"Données du formulaire reçues corrompues; veuillez réessayer "
                u"s'il vous plaît.",
    'childerror': '',  # Children of this widget have errors
}


class CompanyNameValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(CompanyNameValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['already_exists'] = u'Cette entreprise existe déjà dans '\
                                      u'notre base'

    def _validate_python(self, value, state=None):
        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)


class PhoneNumberValidator(twc.RegexValidator):
    def __init__(self, **kwargs):
        super(PhoneNumberValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'Numéro de téléphone invalide'

    regex = re.compile('^0([0-9](\.[0-9]{2}){4})$', re.IGNORECASE)


class SirenValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(SirenValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['wrong_format'] = (u'Format de saisie non respecté')
        self.msgs['invalid_siren'] = (u'Numéro de Siren invalide')
        self.msgs['duplicate_siren'] = (u'Numéro de Siren déjà utilisé')

    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)


class TechnologiesValidator(twc.RegexValidator):
    def __init__(self, **kwargs):
        super(TechnologiesValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'Format du champ invalide'

    regex = re.compile('^(\w|\+|\-)+(\, (\w|\+|\-)+){0,9}$',
                       re.IGNORECASE | re.UNICODE)


class RequiredValidator(twc.Validator):
    def __init__(self, **kwargs):
        super(RequiredValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.required = True


class UrlValidator(twc.UrlValidator):
    def __init__(self, **kwargs):
        super(UrlValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'URL invalide'


class EmailValidator(twc.EmailValidator):
    def __init__(self, **kwargs):
        super(EmailValidator, self).__init__(**kwargs)
        self.msgs = dict(french_validation_messages)
        self.msgs['badregex'] = u'Adresse email invalide'


class NewCompanyForm(twf.Form):
    def __init__(self, **kwargs):
        super(NewCompanyForm, self).__init__(**kwargs)
        self.submit = None

{'fraction_non_alphanumeric': 0.11027756939234809, 'fraction_numerical': 0.006215839674204265, 'mean_word_length': 2.8089795918367346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067679', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2708, 'n_words': 719}