Skip to content

Commit 10f6ba2

Browse files
committed
bugfixes and postman collection
1 parent 15352bc commit 10f6ba2

File tree

6 files changed

+549
-85
lines changed

6 files changed

+549
-85
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ install:
66
pip install -r requirements.txt
77

88
freeze:
9-
pip freeze > requirements.txt
9+
pip freeze > requirements.txt
1010

1111
create_db:
1212
rm -Rf migrations/

app/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@
4747
from app.controllers import perfilController
4848
from app.controllers import cargoController
4949
from app.controllers import cidadeController
50-
from app.controllers import cadastroController
5150

app/controllers/cadastroController.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

app/controllers/usuarioController.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from app import app, db, Messages
2+
from . import resource, paginate
23
from flask import request, jsonify
34
from flask_jwt_extended import jwt_required, get_jwt_identity
45
from sqlalchemy import exc, or_
56
from werkzeug.security import generate_password_hash
6-
from . import resource, paginate
77
from app import Usuario, Perfil
8-
8+
from app import fieldsFormatter
99

1010
from flask_pydantic import validate
1111
from app import UsuarioAddSchema, UsuarioEditSchema
12+
from app import CadastroAddSchema
13+
14+
1215

1316
@app.route("/usuario/all", methods=["GET"])
1417
@jwt_required
@@ -162,7 +165,7 @@ def usuarioEdit(usuario_id):
162165
{"message": Messages.ALREADY_EXISTS.format("email"), "error": True}
163166
)
164167

165-
# if user is tring to edit itself, let it change password
168+
# if user is trying to edit itself, let it change password
166169
if logged_user.id == usuario.id:
167170
# check should change password
168171
if data.get('senha') is not None:
@@ -224,3 +227,71 @@ def usuarioDelete(usuario_id):
224227
return jsonify(
225228
{"message": Messages.REGISTER_DELETE_INTEGRITY_ERROR, "error": True}
226229
)
230+
231+
232+
# --------------------------------------------------------------------------------------------------#
233+
234+
@app.route("/usuario/cadastro", methods=["POST"])
235+
@validate(body=CadastroAddSchema)
236+
def cadastroAdd():
237+
data = request.get_json()
238+
239+
# if there is already a user with email
240+
241+
if Usuario.query.filter_by(email=data.get("email").lower()).first():
242+
return jsonify(
243+
{"message": Messages.ALREADY_EXISTS.format("email"), "error": True}
244+
)
245+
246+
# check if there is already a user with cpf/pis
247+
cpf = fieldsFormatter.CpfFormatter().clean(data["cpf"])
248+
pis = fieldsFormatter.PisFormatter().clean(data["pis"])
249+
perfil = Perfil.query.filter(or_(Perfil.cpf == cpf, Perfil.pis == pis)).first()
250+
if perfil is not None:
251+
return jsonify(
252+
{"message": Messages.ALREADY_EXISTS.format("CPF/PIS"), "error": True}
253+
)
254+
255+
hashed_pass = generate_password_hash(data.get('senha'), method="sha256")
256+
email = data.get("email").lower()
257+
258+
perfil = Perfil(
259+
nome=data.get("nome"),
260+
pis=fieldsFormatter.PisFormatter().clean(data.get("pis")),
261+
cpf=fieldsFormatter.CpfFormatter().clean(data.get("cpf")),
262+
cep=fieldsFormatter.CepFormatter().clean(data.get("cep")),
263+
rua=data.get("rua"),
264+
numero=data.get("numero"),
265+
complemento=data.get("complemento"),
266+
cidade_id=data.get("cidade_id"),
267+
)
268+
269+
db.session.add(perfil)
270+
271+
try:
272+
db.session.flush()
273+
274+
usuario = Usuario(
275+
email=email,
276+
senha=hashed_pass,
277+
perfil_id=perfil.id,
278+
cargo_id=2,
279+
)
280+
281+
db.session.add(usuario)
282+
283+
db.session.commit()
284+
return jsonify(
285+
{
286+
"message": Messages.REGISTER_SUCCESS_CREATED.format("Login"),
287+
"error": False,
288+
}
289+
)
290+
except exc.IntegrityError:
291+
db.session.rollback()
292+
return jsonify(
293+
{"message": Messages.REGISTER_CREATE_INTEGRITY_ERROR, "error": True}
294+
)
295+
296+
297+
# --------------------------------------------------------------------------------------------------#

app/model_schema/cadastroSchema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from typing import Optional
22
import re
33
from pydantic import BaseModel, validator, constr
4-
from app import Usuario, Perfil, fieldsFormatter
5-
64
from validate_docbr import CPF, PIS
75

6+
87
class CadastroAddSchema(BaseModel):
98

109
email: constr(min_length=5, max_length=255)

0 commit comments

Comments
 (0)