-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.py
153 lines (117 loc) · 5.5 KB
/
controllers.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from rest_framework.serializers import ModelSerializer
from usuario.models import Usuario
from usuario.services import UsuarioService
from usuario.exceptions import *
from rest_framework.response import Response
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from rest_framework import status
class UsuarioController(ModelSerializer):
class Meta:
model = Usuario
fields = ('id', 'id_tipo_usuario', 'tipo_usuario', 'username', 'password',
'email', 'nome', 'telefone', 'cpf', 'data_nascimento')
# ID USER TYPE
def get_user_type(self, id_int):
return {
0: 'ADMIN',
1: 'SUPORTE',
2: 'JORNALISTA',
3: 'PESSOA FÍSICA',
4: 'PESSOA JURIDICA',
}[id_int]
# SUCCESS TYPE
def get_success_type(self, status_code):
return {
201 : status.HTTP_201_CREATED,
}[status_code]
# SUCCESS RESPONSE
def success_response(self, status_code, headers):
return Response(self.validated_data, status=self.get_success_type(status_code), headers=headers)
# ERROR TYPE
def get_error_type(self, status_code):
return {
400 : status.HTTP_400_BAD_REQUEST,
409 : status.HTTP_409_CONFLICT,
406 : status.HTTP_406_NOT_ACCEPTABLE,
}[status_code]
# ERROR RESPONSE
def error_response(self, status_code, message):
return Response(status=self.get_error_type(status_code), data= {
'error' : message,
'status' : status_code})
# SERVICE LAYER CALL
def validate_business_logic(self, request):
service = UsuarioService(request['username'],
request['nome'],
request['password'],
request['cpf'],
request['email'],
request['telefone'])
# Checking if exist a user with same username
if not service.validate_username():
return (False, InvalidUsername)
if not service.validate_cpf_len():
return (False, InvalidCpfLen)
if not service.validate_cpf_digits():
return (False, InvalidCpfDigits)
if not service.validate_email_existence():
return (False, InvalidEmailExist)
if not service.validate_email():
return (False, InvalidEmail)
if not service.validate_email_domain():
return (False, InvalidEmailDomain)
if not service.validate_br_phone_number():
return (False, InvalidBrPhoneNumber)
if not service.validate_password():
return (False, InvalidPasswordShort)
if not service.validate_name():
return (False, InvalidLastname)
# else
return (True, Exception)
# CREATE USER
def create_user(self, post_request):
# Validate serializer(controller) - django requirement
if self.is_valid(post_request):
# Getting the validation and the exception
validated, excepted = self.validate_business_logic(self.validated_data)
if validated:
post_request.perform_create(self)
headers = post_request.get_success_headers(self.validated_data)
user = User.objects.create_user(username= self.validated_data['username'],
email= self.validated_data['email'],
password= self.validated_data['password'])
return self.success_response(201, headers)
else:
try:
raise excepted
except InvalidUsername:
return self.error_response(409,
'Nome de usuário ja existe! Verifique as credenciais.')
except InvalidCpfLen:
return self.error_response(406,
'CPF Inválido -> tamanho fora do padrão nacional.')
except InvalidCpfDigits:
return self.error_response(406,
'CPF Inválido -> digitos não validados.')
except InvalidEmailExist:
return self.error_response(409,
'Email Inválido -> Email ja cadastrado.')
except InvalidEmail:
return self.error_response(406,
'Email Inválido -> string de email esperada é exemplo@exemplo.com.')
except InvalidEmailDomain:
return self.error_response(406,
'Email Inválido -> verifique o dominio do email.')
except InvalidBrPhoneNumber:
return self.error_response(406,
'Telefone Inválido -> formato deve ser 00 00000 0000')
except InvalidPasswordShort:
return self.error_response(406,
'Senha Inválida -> mínimo de 6 caracteres.')
except InvalidLastname:
return self.error_response(406,
'Nome Inválido -> Insira nome e sobrenome.')
else:
return self.error_response(400,
'Formulário de usuário não foi preenchido corretamente')