def delete(self, login): user = UserModel.find_user_by_login(login) if user: try: user.delete_user() return {"message": f"User '{login}' deleted."}, 200 except Exception as e: return { "message": f"An internal error ({e}) ocurred trying to delete user '{login}'." }, 500 else: return {"message": f"User '{login}' not found."}, 404
def post(self): atributos = reqparse.RequestParser() atributos.add_argument('login', type=str, required=True, help="The field 'login' cannot be left blank.") atributos.add_argument('senha', type=str, required=True, help="The field 'senha' cannot be left blank.") dados = atributos.parse_args() user = UserModel.find_user_by_login(dados['login']) if user and safe_str_cmp(user.senha, dados['senha']): if user.ativado: token_de_acesso = create_access_token(identity=user.user_id) return {"access_token": token_de_acesso}, 200 else: return {"message": "User not confirmed."}, 400 else: # Unauthorized return {"message": "The username or password is incorrect."}, 401
def post(self): atributos = reqparse.RequestParser() atributos.add_argument('nome', type=str) atributos.add_argument('login', type=str, required=True, help="The field 'login' cannot be left blank.") atributos.add_argument('email', type=str, required=True, help="The field 'email' cannot be left blank.") atributos.add_argument('senha', type=str, required=True, help="The field 'senha' cannot be left blank.") atributos.add_argument('ativado', type=bool) dados = atributos.parse_args() if UserModel.find_user_by_login(dados['login']): return {"message": f"User '{dados['login']}' already exists."}, 400 elif UserModel.find_user_by_email(dados['email']): return { "message": f"Email '{dados['email']}' already registered." }, 400 else: user = UserModel(**dados) user.ativado = False try: user.save_user() user.send_confirmation_email() return { "message": f"User '{dados['login']}' created successfully!" }, 201 except Exception as e: user.delete_user() return { "message": f"An internal error ({e}) ocurred trying to create user '{dados['login']}'." }, 500
def get(self, login): user = UserModel.find_user_by_login(login) if user: return user.json() else: return {"message": f"User '{login}' not found."}, 404