|
1 | 1 | from app import app, db, Messages |
| 2 | +from . import resource, paginate |
2 | 3 | from flask import request, jsonify |
3 | 4 | from flask_jwt_extended import jwt_required, get_jwt_identity |
4 | 5 | from sqlalchemy import exc, or_ |
5 | 6 | from werkzeug.security import generate_password_hash |
6 | | -from . import resource, paginate |
7 | 7 | from app import Usuario, Perfil |
8 | | - |
| 8 | +from app import fieldsFormatter |
9 | 9 |
|
10 | 10 | from flask_pydantic import validate |
11 | 11 | from app import UsuarioAddSchema, UsuarioEditSchema |
| 12 | +from app import CadastroAddSchema |
| 13 | + |
| 14 | + |
12 | 15 |
|
13 | 16 | @app.route("/usuario/all", methods=["GET"]) |
14 | 17 | @jwt_required |
@@ -162,7 +165,7 @@ def usuarioEdit(usuario_id): |
162 | 165 | {"message": Messages.ALREADY_EXISTS.format("email"), "error": True} |
163 | 166 | ) |
164 | 167 |
|
165 | | - # if user is tring to edit itself, let it change password |
| 168 | + # if user is trying to edit itself, let it change password |
166 | 169 | if logged_user.id == usuario.id: |
167 | 170 | # check should change password |
168 | 171 | if data.get('senha') is not None: |
@@ -224,3 +227,71 @@ def usuarioDelete(usuario_id): |
224 | 227 | return jsonify( |
225 | 228 | {"message": Messages.REGISTER_DELETE_INTEGRITY_ERROR, "error": True} |
226 | 229 | ) |
| 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 | +# --------------------------------------------------------------------------------------------------# |
0 commit comments