Exemple #1
0
def string_len(func, *args, **kwargs):
    if 'min' not in kwargs:
        raise Exception(_('Min param must be filled.'))

    if 'max' not in kwargs:
        raise Exception(_('Max param must be filled.'))

    message = _(
        '{0} must be a minimum of {1} characters and a maximum of {2}.'.format(
            kwargs['field'], kwargs['min'], kwargs['max']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if not isinstance(arg2, str):
                raise ValidationException(kwargs['field'], message)

            if len(arg2) < int(kwargs['min']) or len(arg2) > int(
                    kwargs['max']):
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #2
0
def valid_cpf(func, *args, **kwargs):
    message = _('{0} must be valid.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is None:
            return func(obj, arg1, arg2)

        if not isinstance(arg2, str):
            raise ValidationException(kwargs['field'], message)

        if not arg2.isdigit():
            raise ValidationException(kwargs['field'], message)

        if len(arg2) != 11:
            raise ValidationException(kwargs['field'], message)

        if arg2 in [s * 11 for s in [str(n) for n in range(10)]]:
            raise ValidationException(kwargs['field'], message)

        calc = [i for i in range(1, 10)]
        d1 = sum([int(a) * (11 - b) for a, b in zip(arg2[:-2], calc)]) % 11
        d1 = 0 if d1 < 2 else (11 - d1)
        calc = [i for i in range(1, 11)]
        d2 = sum([int(a) * (12 - b) for a, b in zip(arg2[:-1], calc)]) % 11
        d2 = 0 if d2 < 2 else (11 - d2)

        if str(d1) != arg2[-2] or str(d2) != arg2[-1]:
            raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #3
0
def valid_cnpj(func, *args, **kwargs):
    message = _('{0} must be valid.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is None:
            return func(obj, arg1, arg2)

        if not isinstance(arg2, str):
            raise ValidationException(kwargs['field'], message)

        if not arg2.isdigit():
            raise ValidationException(kwargs['field'], message)

        if len(arg2) != 14:
            raise ValidationException(kwargs['field'], message)

        if arg2 in [s * 14 for s in [str(n) for n in range(13)]]:
            raise ValidationException(kwargs['field'], message)

        f_dv = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
        d1 = sum([int(a) * b for a, b in zip(arg2[:-2], f_dv)]) % 11
        d1 = 0 if d1 < 2 else (11 - d1)
        f_dv = [6] + f_dv
        d2 = sum([int(a) * b for a, b in zip(arg2[:-1], f_dv)]) % 11
        d2 = 0 if d2 < 2 else (11 - d2)

        if str(d1) != arg2[-2] or str(d2) != arg2[-1]:
            raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #4
0
    def maker(*args, **kwargs):
        if 'field' not in kwargs:
            raise Exception(_('arg field is missing'))

        def wrapper(fn):
            return decorator(fn, *args, **kwargs)

        return wrapper
Exemple #5
0
def match_regex(func, *args, **kwargs):
    if 'regex' not in kwargs:
        raise Exception(_('Regex param must be filled.'))

    message = _('{0} is incorrect.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if not isinstance(arg2, str):
                raise ValidationException(kwargs['field'], message)

            if not re.match(kwargs['regex'], arg2):
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #6
0
def not_empty(func, *args, **kwargs):
    message = _('{0} may not be empty.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is None or len(arg2) == 0:
            raise ValidationException(kwargs['field'], message)
        else:
            return func(obj, arg1, arg2)

    return wrapper
Exemple #7
0
def not_null(func, *args, **kwargs):

    message = _('{0} cannot be null.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is None:
            raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #8
0
def min_max(func, *args, **kwargs):
    message = _('{0} must be valid.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if arg2 < int(kwargs['min']) or arg2 > int(kwargs['max']):
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #9
0
def is_alpha_space(func, *args, **kwargs):
    message = _('{0} must contain only letters.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if not isinstance(arg2, str):
                raise ValidationException(kwargs['field'], message)

            if not arg2.replace(" ", "").isalpha():
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #10
0
def is_digit(func, *args, **kwargs):
    message = _('{0} must contain only digits.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if not isinstance(arg2, str):
                raise ValidationException(kwargs['field'], message)

            if not arg2.isdigit():
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #11
0
def valid_email(func, *args, **kwargs):
    message = _('{0} must be valid.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is not None:

            if not isinstance(arg2, str):
                raise ValidationException(kwargs['field'], message)

            if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", arg2):
                raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper
Exemple #12
0
def valid_cep(func, *args, **kwargs):
    message = _('{0} must be valid.'.format(kwargs['field']))
    if 'message' in kwargs:
        message = kwargs['message']

    def wrapper(obj, arg1, arg2):
        if arg2 is None:
            return func(obj, arg1, arg2)

        if not isinstance(arg2, str):
            raise ValidationException(kwargs['field'], message)

        if not arg2.isdigit():
            raise ValidationException(kwargs['field'], message)

        if len(arg2) != 8:
            raise ValidationException(kwargs['field'], message)

        return func(obj, arg1, arg2)

    return wrapper