forked from grupyrn/jararaca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
74 lines (51 loc) · 2.08 KB
/
Copy pathvalidators.py
File metadata and controls
74 lines (51 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
from rest_framework import serializers
from django.conf import settings
def cpf_validator(value):
if len(value) != 11 or len(set(value)) == 1:
raise serializers.ValidationError(ExceptionMessage.invalid_cpf())
first_cpf_weight = [10, 9, 8, 7, 6, 5, 4, 3, 2]
second_cpf_weight = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
first_part = value[:9]
first_digit = value[9]
second_digit = value[10]
if not ((first_digit == Utils.get_first_digit(number=first_part, weight=first_cpf_weight)
and second_digit == Utils.get_second_digit(updated_number=value[:10], weight=second_cpf_weight))):
raise serializers.ValidationError(ExceptionMessage.invalid_cpf())
class Utils:
DIVIDER = 11
@staticmethod
def get_first_digit(number, weight):
sum = 0
for i in range(len(weight)):
sum += int(number[i]) * weight[i]
rest_division = sum % Utils.DIVIDER
if rest_division < 2:
return '0'
return str(11 - rest_division)
@staticmethod
def get_second_digit(updated_number, weight):
sum = 0
for i in range(len(weight)):
sum += + int(updated_number[i]) * weight[i]
rest_division = sum % Utils.DIVIDER
if rest_division < 2:
return '0'
return str(11 - rest_division)
class ExceptionMessage:
LANGUAGE_CODE = settings.LANGUAGE_CODE
translations = [
{'lang': 'en-US', 'message-cpf': 'Invalid CPF', 'message-cnpj': 'Invalid CNPJ'},
{'lang': 'pt-BR', 'message-cpf': 'CPF inválido', 'message-cnpj': 'CNPJ inválido'},
{'lang': 'es-ES', 'message-cpf': 'CPF no válido', 'message-cnpj': 'CNPJ no válido'},
]
@staticmethod
def invalid_cpf():
for tr in ExceptionMessage.translations:
if tr['lang'] == ExceptionMessage.LANGUAGE_CODE:
return tr['message-cpf']
@staticmethod
def invalid_cnpj():
for tr in ExceptionMessage.translations:
if tr['lang'] == ExceptionMessage.LANGUAGE_CODE:
return tr['message-cnpj']