def get(cls, user_id: str = None): if not user_id: return user_list_schema.dump(UserModel.fetch_all()), 200 user = UserModel.find_by_id(user_id) if not user: return {"message": "User not found"}, 404 return user_schema.dump(user)
def post(cls): user_id = get_jwt_identity() if user_id: new_token = create_access_token(identity=user_id, fresh=False) user = UserModel.find_by_id(user_id) ret = jsonify(user_schema.dump(user)) set_access_cookies(ret, new_token) ret.status_code = 200 return ret
def update(self, user_id, value): user = User.query.get(user_id) if not user: raise EntityNotFoundException(user_id, 'User') user.update(value) self.db.session.commit() return user_schema.dump(user)
def update_user(user): json = request.get_json(force=True) user.password = json.get('password', user.password) user.email = json.get('email', user.email) if user.save(): return response(user_schema.dump(user)) return bad_request()
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_email(user.email): return {"message": "E-mail already exists"}, 400 hash = bcrypt.hashpw(user.password.encode('utf8'), bcrypt.gensalt(10)) user.password = hash user.save() return user_schema.dump(user), 201
def create_user(): json = request.get_json(force=True) error = params_user_schema.validate(json) if error: return bad_request() user = User.new(email=json['email'], password=json['password'], username='******') if user.save(): return response(user_schema.dump(user)) return bad_request()
def get(cls): user_id = get_jwt_identity() if user_id: user = UserModel.find_by_id(user_id) confirmation = user.most_recent_confirmation if user: if confirmation and confirmation.confirmed: return user_schema.dump(user), 200 else: return { "message": "Please confirm your user account." }, 400 else: return None, 400
def put(cls, user_id: str): user_json = request.get_json() updated_user = user_schema.load(user_json) old_user = UserModel.find_by_id(user_id) if not old_user: return {"message": f"User <id={user_id} not found."}, 404 if updated_user.email != old_user.email and UserModel.find_by_email( updated_user.email): return {"message": "E-mail already exists"}, 400 hash = bcrypt.hashpw(updated_user.password.encode('utf8'), bcrypt.gensalt(10)) updated_user.password = hash updated_user.id = user_id updated_user.save() return user_schema.dump(updated_user), 200
def patch(cls, user_id: str): user_json = request.get_json() user_data = user_schema.load(user_json) user = UserModel.find_by_id(user_id) if not user: return {"message": f"User <id={user_id} not found."}, 404 if user_data.email != user.email and UserModel.find_by_email( user_data.email): return {"message": "E-mail already exists"}, 400 else: user.email = user_data.email if user_data.password: hash = bcrypt.hashpw(user_data.password.encode('utf8'), bcrypt.gensalt(10)) user.password = hash user.save() return user_schema.dump(user), 204
def delete_user(user): if user.delete(): return response(user_schema.dump(user)) return bad_request()
def get_user(user): return response(user_schema.dump(user))