def siret_validator():
    """Validate a SIRET: check its length (14), its final code, and pass it
    through the Luhn algorithm."""

    def _validate_siret(form, field, siret=""):
        """SIRET validator.

        A WTForm validator wants a form and a field as parameters. We
        also want to give directly a siret, for a scripting use.
        """
        if field is not None:
            siret = (field.data or "").strip()

        if len(siret) != 14:
            msg = _("SIRET must have exactly 14 characters ({count})").format(
                count=len(siret)
            )
            raise validators.ValidationError(msg)

        if not all(("0" <= c <= "9") for c in siret):
            if not siret[-3:] in SIRET_CODES:
                msg = _(
                    "SIRET looks like special SIRET but geographical "
                    "code seems invalid (%(code)s)",
                    code=siret[-3:],
                )
                raise validators.ValidationError(msg)

        elif not luhn(siret):
            msg = _("SIRET number is invalid (length is ok: verify numbers)")
            raise validators.ValidationError(msg)

    return _validate_siret
def siret_validator ( ) : def _validate_siret ( form , field , siret = "" ) : if field is not None : siret = ( field . data or "" ) . strip ( ) if len ( siret ) != 14 : msg = _ ( "SIRET must have exactly 14 characters ({count})" ) . format ( count = len ( siret ) ) raise validators . ValidationError ( msg ) if not all ( ( "0" <= c <= "9" ) for c in siret ) : if not siret [ - 3 : ] in SIRET_CODES : msg = _ ( "SIRET looks like special SIRET but geographical " "code seems invalid (%(code)s)" , code = siret [ - 3 : ] , ) raise validators . ValidationError ( msg ) elif not luhn ( siret ) : msg = _ ( "SIRET number is invalid (length is ok: verify numbers)" ) raise validators . ValidationError ( msg ) return _validate_siret
Validate a SIRET: check its length (14), its final code, and pass it
    through the Luhn algorithm.