def auth_register(): """ Creates a new user in the app Returns: Tuple containing the dict of the new user and status code """ user_fields = user_schema.load(request.json) # Check uniqueness of username/email and return aborts instead of getting errors if User.query.filter_by(username=user_fields["username"]).first(): return abort(400, description="Username is unavailable.") if User.query.filter_by(email=user_fields["email"]).first(): return abort(400, description="Email already registered.") user = User() user.username = user_fields["username"] user.email = user_fields["email"] user.password = bcrypt.generate_password_hash( user_fields["password"]).decode("utf-8") db.session.add(user) db.session.commit() return (jsonify(user_schema.dump(user)), 201)
def auth_register(): """ Creates a new user in the app Returns: Tuple containing the dict of the new user and status code """ user_fields = user_register_schema.load(request.json) # Check uniqueness of email and return abort instead of getting errors if User.query.filter_by(email=user_fields["email"]).first(): return abort(400, description="Email already registered.") user = User() user.email = user_fields["email"] user.password = bcrypt.generate_password_hash(user_fields["password"]).decode("utf-8") db.session.add(user) db.session.commit() # Need to retrieve the user after adding to the database so that the href and uri fields # can be updated with the correct user id users = User.query.filter_by(email=user_fields["email"]) # Need to remove password from user_fields otherwise it will replace the password hash with # the clear text password entered by the user to register del user_fields["password"] user_fields["href"] = f"https://api.spotify.com/user/{users[0].id}" user_fields["uri"] = f"spotify:user:{users[0].id}" users.update(user_fields) db.session.commit() return (jsonify(user_schema.dump(user)), 201)
def profile_page(username): # Retrieve user profile user, posts = get_profile_svc(username) return jsonify([ user_schema.dump(user), posts_schema.dump(posts), { "followers": user.followers_count() } ])
def auth_register(): current_user = get_jwt_identity() if current_user: return redirect(url_for("user.profile")) new_user = user_schema.load(request.json) user = register_user_svc(new_user) if user is None: return abort(400, description="Email already registered") return jsonify(user_schema.dump(user))
def get_user(): """ Gets the current user's profile Returns: Dict of the retrieved user """ user = User.query.get(get_jwt_identity()) if not user: return abort(404, description="User not found.") return jsonify(user_schema.dump(user))
def get_user(user, user_id): """ Gets a single user from the users table using an id number, the verify user wrapper will actually return the user so just return the json Parameters: user: User The user object for the user trying to make the request user_id: integer The user id number for the user to retrieve Returns: Dict of the retrieved user """ return jsonify(user_schema.dump(user))
def auth_register(): current_user = get_jwt_identity() if current_user: return jsonify({"msg": "Already logged in"}), 200 user_fields = user_schema.load(request.json) user = register_user_svc(f_name=user_fields["f_name"], l_name=user_fields["l_name"], email=user_fields["email"], bio=user_fields["bio"], username=user_fields["username"], password=user_fields["password"]) if user is None: return abort(400, description="Email already registered") return jsonify(user_schema.dump(user))
def auth_register(): user_fields = user_schema.load(request.json) user = User.query.filter_by(email=user_fields["email"]).first() if user: return abort(400, description="User already") user = User() user.email = user_fields["email"] user.password = bcrypt.generate_password_hash( user_fields["password"]).decode("utf-8") db.session.add(user) db.session.commit() return jsonify(user_schema.dump(user))
def delete_user(user, user_id): """ Deletes a user from the database, this also deletes their created checklists and the items in those checklists. Also removes the user from any group checklists and items they have been assigned to Parameters: user: User The user object for the user trying to make the request user_id: integer The user id number for the user to delete Returns: Tuple containing a message of the response outcome and the dict of the removed user """ db.session.delete(user) db.session.commit() return jsonify("The following user was deleted from the database.", user_schema.dump(user))
def update_user(user, user_id): """ Updates user details for the current user Parameters: user: User The user object for the user trying to make the request user_id: integer The user id number for the user to update Returns: Dict of the updated user """ user_fields = user_schema.load(request.json, partial=True) users = User.query.filter_by(id=user.id) users.update(user_fields) db.session.commit() return jsonify(user_schema.dump(users[0]))
def profile(username): # Retrieve user profile this_user = get_profile(username) return jsonify(user_schema.dump(this_user))