Exemplo n.º 1
0
def make_profile():
    """add user info to database"""

    friends = request.form.getlist("friends")
    listings = request.form.getlist("listings")
    looking = True if request.form.get("looking") == "True" else False
    questions = Question.query.all()
    user_id = request.form.get("user_name")
    password = request.form.get("password").encode('utf-8')
    hashed = bcrypt.hashpw(password, bcrypt.gensalt())

    venmo = request.form.get("venmo")

    photo = functions.save_photo("photo")

    kwargs = dict(user_id=user_id,
                  name=request.form.get("full_name"),
                  email=request.form.get("email"),
                  password=hashed,
                  phone=request.form.get("phone"),
                  bio=request.form.get("bio"),
                  photo=photo,
                  state=request.form.get("state"),
                  looking_for_apt=looking,
                  venmo=venmo)

    if User.query.get(user_id):
        flash("User Name Taken")
        return redirect("/users/{}".format(user_id))
    else:
        for key in kwargs.keys():
            if kwargs[key] == "":
                del kwargs[key]
        db.session.add(User(**kwargs))
        db.session.commit()

        for friend in friends:
            db.session.add(Friendship(friend_1_id=friend, friend_2_id=user_id))

            db.session.add(Friendship(friend_2_id=friend, friend_1_id=user_id))

        db.session.commit()

        for listing in listings:
            functions.add_UserListing(user_id, listing)

        for question in questions:
            answer_id = request.form.get("{}".format(question.question_id))

            db.session.add(UserAnswer(user_id=user_id, answer_id=answer_id))

        db.session.commit()

        session["current_user"] = user_id

        return redirect("/users/{}".format(user_id))
Exemplo n.º 2
0
def add_friendship(friend_1_id, friend_2_id):
    """add a friendship between two users"""

    db.session.add(Friendship(friend_1_id=friend_1_id,
                              friend_2_id=friend_2_id))

    db.session.add(Friendship(friend_1_id=friend_2_id,
                              friend_2_id=friend_1_id))

    db.session.commit()
Exemplo n.º 3
0
def befriending(user_id):
    """Friending between session user and another user."""

    existing_friendship = Friendship.query.filter(
        Friendship.friend_1_id == session['user_id'],
        Friendship.friend_2_id == user_id).first()
    session_user = User.query.filter(
        User.user_id == session['user_id']).first()
    other_user = User.query.filter(User.user_id == user_id).first()

    if not existing_friendship:
        # Creating friendship one way.
        new_friendship = Friendship(friend_1_id=session['user_id'],
                                    friend_2_id=user_id)
        db.session.add(new_friendship)

        # Friending txt notification.

        body="{friend_name}, {user_name} has befriend you. "\
        "Go to their profile and check out their calendar: http://localhost:5000/user/{user_id}".format(
                                                                                                        friend_name=other_user.name,
                                                                                                        user_name=session_user.name,
                                                                                                        user_id=session_user.user_id)
        helpers.send_txt_notification(body, ACCOUNT_SID, AUTH_TOKEN)
        db.session.commit()

    return redirect('/user/{user_id}'.format(user_id=user_id))
Exemplo n.º 4
0
def add_user_friendships(friend_page, acct):
    """ Creates new users and adds friendships with existing accounts based on GR_id"""

    friends_list = []  # becomes a list of User objects
    # with db.session.begin():
    for friend in friend_page.user:  # loops over page of 30 friends
        gr_id = int(friend.id.cdata.encode('utf8'))
        gr_url = friend.link.cdata.encode('utf8')
        name = friend.name.cdata.encode('utf8')
        image_url = friend.small_image_url.cdata.encode('utf8')

        try:
            # if user is already in db, add friendship only
            existing_user = User.query.filter_by(gr_id=gr_id).one()
            friends_list.append(existing_user)
        except:
            new_user = User(gr_id=gr_id, gr_url=gr_url,
                            gr_name=name, image_url=image_url)
            db.session.add(new_user)
            print "added new friend: " + friend.name.cdata.encode('utf8')
            friends_list.append(new_user)

    print friends_list
    db.session.commit()

    # after adding missing users to db, add friendship between authorized account
    # and all friends
    for friend in friends_list:

        new_friend = Friendship(user_id=acct.user.user_id, friend_id=friend.user_id)
        old_friend = Friendship(user_id=friend.user_id, friend_id=acct.user.user_id)
        db.session.add(new_friend)
        db.session.add(old_friend)
        print "Added friendship!"

    db.session.commit()
Exemplo n.º 5
0
def load_friendships():
    """Load friendships from seed data into database"""

    print "Friendships"

    with open("seed_data/friendships.txt") as friendships:
        for friendship in friendships:
            friendship = friendship.rstrip()
            friendship_id, friend_1_id, friend_2_id = friendship.split("|")

            friendship = Friendship(friendship_id=friendship_id,
                                    friend_1_id=friend_1_id,
                                    friend_2_id=friend_2_id)

            db.session.add(friendship)

    db.session.commit()
Exemplo n.º 6
0
def add_friend():
    """ Add friend. """

    friend_username = request.form.get("username")
    friend = User.query.filter_by(username=friend_username).first()
    user_username = session.get('username')

    user_one = User.query.filter_by(username=user_username).first()
    user_id = user_one.user_id

    if friend:
        friend_id = friend.user_id
        new_friend = Friendship(user_one_id=user_id, user_two_id=friend_id)
        db.session.add(new_friend)
        db.session.commit()
        flash("Friend added.")
    else:
        flash("Friend not successfully added.")
    return redirect("/user")