def update_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() data = request.get_json() if not user: return ResponseGenerator.not_found() if data['admin'] == "True": admin = True else: admin = False user.name = data['name'] user.password = data['password'] user.email = data['email'] user.admin = admin update_user = User(name=user.name, email=user.email, password=user.password, admin=user.admin) User.update(update_user) return ResponseGenerator.generate_response( f"{update_user.name} has been successfully updated", 200)
def delete_todo(current_user, todo_id): todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first() if not todo: return ResponseGenerator.not_found() Todo.delete(todo) return ResponseGenerator.generate_response( f"{todo.text} has been Deleted!", 200)
def delete_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() if not user: return ResponseGenerator.not_found() User.delete(user) return ResponseGenerator.generate_response( f"{user.name} deleted successfully from DB.", 200)
def get_one_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() if not user: return ResponseGenerator.not_found() user_data = {} user_data['id'] = user.id user_data['name'] = user.name user_data['email'] = user.email user_data['admin'] = user.admin return ResponseGenerator.generate_response(user_data, 200)
def post(self): data = request.get_json() email = data["email"] password = data["password"] user = UserRepository.get_by_email(email=email) if not user: return ResponseGenerator.not_found(msg="user not found") if not user.check_password(password=password): return ResponseGenerator.forbidden( msg="email/password combination is invalid") access_token = UserRepository.create_user_access_token(user=user) return ResponseGenerator.generate_response( {"access_token": access_token}, code=200)
def post(self): data = request.get_json() mandatory_fields = ["email", "password"] if any(data.get(item) is None for item in mandatory_fields): return ResponseGenerator.mandatory_field(fields=mandatory_fields) email = data["email"] password = data["password"] user = UserRepository.get_by_email(email=email) if not user: return ResponseGenerator.not_found(msg="user not found") if not user.check_password(password=password): return ResponseGenerator.forbidden(msg="email/password combination is invalid") access_token = UserToken.create_user_access_token(user=user) return ResponseGenerator.generate_response({ "access_token": access_token }, code=200)
def flask_jwt_user_loader_error_callback(identity): return ResponseGenerator.not_found( msg="User::{} not found".format(identity))