Beispiel #1
0
def login():
    """Log user in if credentials provided are correct."""

    login_email = request.form.get("login_email")
    login_password = request.form.get("login_password")

    try:
        current_user = db.session.query(User).filter(User.email == login_email,
                                                     User.password == login_password).one()
    except NoResultFound:
        flash("The email or password you have entered did not match our records. Please try again.", "danger")
        return redirect("/login")

    # Get current user's friend requests and number of requests to display in badges
    received_friend_requests, sent_friend_requests = get_friend_requests(current_user.user_id)
    num_received_requests = len(received_friend_requests)
    num_sent_requests = len(sent_friend_requests)
    num_total_requests = num_received_requests + num_sent_requests

    # Use a nested dictionary for session["current_user"] to store more than just user_id
    session["current_user"] = {
        "first_name": current_user.first_name,
        "user_id": current_user.user_id,
        "num_received_requests": num_received_requests,
        "num_sent_requests": num_sent_requests,
        "num_total_requests": num_total_requests
    }

    flash("Welcome {}. You have successfully logged in.".format(current_user.first_name), "success")

    return redirect("/users/{}".format(current_user.user_id))
Beispiel #2
0
def show_friends_and_requests():
    """Show friend requests and list of all friends"""

    # This returns User objects for current user's friend requests
    received_friend_requests, sent_friend_requests = get_friend_requests(session["current_user"]["user_id"])

    # This returns a query for current user's friends (not User objects), but adding .all() to the end gets list of User objects
    friends = get_friends(session["current_user"]["user_id"]).all()

    return render_template("friends.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends)
Beispiel #3
0
def show_friends_and_requests():
    """
    show friends and  requests
    """

    received_friend_requests, sent_friend_requests = get_friend_requests(
        session["current_user"]["user_id"])

    friends = get_friends(session["current_user"]["user_id"]).all()

    return render_template("friends.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends)
Beispiel #4
0
def show_friends_and_requests():
    """Show friend requests and list of all friends"""

    # This returns User objects for current user's friend requests
    received_friend_requests, sent_friend_requests = get_friend_requests(
        session["current_user"]["user_id"])

    # This returns a query for current user's friends (not User objects), but adding .all() to the end gets list of User objects
    friends = get_friends(session["current_user"]["user_id"]).all()

    return render_template("friends.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends)
Beispiel #5
0
def search_users():
    """Search for a user by email and return results."""

    # Returns users for current user's friend requests
    received_friend_requests, sent_friend_requests = get_friend_requests(session["current_user"]["user_id"])

    # Returns query for current user's friends (not User objects) so add .all() to the end to get list of User objects
    friends = get_friends(session["current_user"]["user_id"]).all()

    user_input = request.args.get("q")

    # Search user's query in users table of db and return all search results
    search_results = search(db.session.query(User), user_input).all()

    return render_template("friends_search_results.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends,
                           search_results=search_results)
Beispiel #6
0
def search_users():
    """Search for a user by email and return results."""

    received_friend_requests, sent_friend_requests = get_friend_requests(
        session["current_user"]["user_id"])

    friends = get_friends(session["current_user"]["user_id"]).all()

    user_input = request.args.get("q")

    # query all users whose email contain user_input
    search_results = db.session.query(User).filter(
        User.email.like('%' + user_input + '%')).all()

    return render_template("friends_search_results.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends,
                           search_results=search_results)
Beispiel #7
0
def search_users():
    """Search for a user by email and return results."""

    # Returns users for current user's friend requests
    received_friend_requests, sent_friend_requests = get_friend_requests(
        session["current_user"]["user_id"])

    # Returns query for current user's friends (not User objects) so add .all() to the end to get list of User objects
    friends = get_friends(session["current_user"]["user_id"]).all()

    user_input = request.args.get("q")

    # Search user's query in users table of db and return all search results
    search_results = search(db.session.query(User), user_input).all()

    return render_template("friends_search_results.html",
                           received_friend_requests=received_friend_requests,
                           sent_friend_requests=sent_friend_requests,
                           friends=friends,
                           search_results=search_results)
Beispiel #8
0
def process_login():
    """
    user log in and redirect to user's page
    """

    email = request.form.get("email")
    password = request.form.get("password")

    current_user = User.query.filter(User.email == email).first()

    #user exists, check password
    if current_user:
        if password == current_user.password:
            #login successfully

            received_friend_requests, sent_friend_requests = get_friend_requests(
                current_user.user_id)
            num_received_requests = len(received_friend_requests)
            num_sent_requests = len(sent_friend_requests)
            num_total_requests = num_received_requests + num_sent_requests

            session["current_user"] = {
                "first_name": current_user.first_name,
                "user_id": current_user.user_id,
                "num_received_requests": num_received_requests,
                "num_sent_requests": num_sent_requests,
                "num_total_requests": num_total_requests
            }

            flash("You have successfully logged in!", "success")
            return redirect("/users/%s" % current_user.user_id)

        else:
            #wrong password
            flash("You entered wrong password. Please try again.", "danger")
            return redirect("/login")

    #user doesn't exist, need to register
    else:
        flash("No user associated with this email. Please sign up.", "danger")
        return redirect("/signup")
Beispiel #9
0
def login():
    """Log user in if credentials provided are correct."""

    login_email = request.form.get("login_email")
    login_password = request.form.get("login_password")

    try:
        current_user = db.session.query(User).filter(
            User.email == login_email, User.password == login_password).one()
    except NoResultFound:
        flash(
            "The email or password you have entered did not match our records. Please try again.",
            "danger")
        return redirect("/login")

    # Get current user's friend requests and number of requests to display in badges
    received_friend_requests, sent_friend_requests = get_friend_requests(
        current_user.user_id)
    num_received_requests = len(received_friend_requests)
    num_sent_requests = len(sent_friend_requests)
    num_total_requests = num_received_requests + num_sent_requests

    # Use a nested dictionary for session["current_user"] to store more than just user_id
    session["current_user"] = {
        "first_name": current_user.first_name,
        "user_id": current_user.user_id,
        "num_received_requests": num_received_requests,
        "num_sent_requests": num_sent_requests,
        "num_total_requests": num_total_requests
    }

    flash(
        "Welcome {}. You have successfully logged in.".format(
            current_user.first_name), "success")

    return redirect("/users/{}".format(current_user.user_id))