예제 #1
0
def facebook_login():
    # Handles the login from the facebook login button

    fb_user_id = request.form.get('fbUserId')
    fb_fname = request.form.get('fbFname')
    fb_lname = request.form.get('fbLname')
    fb_email = request.form.get('fbEmail')
    current_acces_token = request.form.get('accessToken')
    fb_friends = request.form.get('fbFriends')

    fb_friends = json.loads(fb_friends)
    fb_user = User.query.filter_by(fb_id=fb_user_id).first()

    if fb_user:
        # User has previously logged into MLM
        user_id = fb_user.user_id
        session['user_id'] = user_id
        session['current_acces_token'] = current_acces_token

        # check friends list in friends table. If friendship not there, add it.
        if fb_friends:
            friends_user_ids = []
            # turn the fb_ids into user_ids.
            for friend_fb_id in fb_friends:
                friend_user_id = db.session.query(User.user_id
                                                  ).filter_by(
                                                  fb_id=friend_fb_id
                                                  ).first()
                friends_user_ids.append(friend_user_id)
            friends_user_ids = [x[0] for x in friends_user_ids]

            # now see if those friends are in the friendship table.
            for friend in friends_user_ids:
                friend_exists = db.session.query(Friendship.friend_id
                                                 ).filter_by(friend_id=friend
                                                             ).first()
                # if they're not, add them in! Yay friendship!
                if friend_exists is None:
                    Friendship.add_friendship(user_id, friend)

        flash('Login successful!')

        return redirect('/home')
    else:
        # First time for user logging into MLM
        # add the user to the database
        User.add_user(email=fb_email, fname=fb_fname,
                      lname=fb_lname, fb_id=fb_user_id)
        # access that user's information, add it to the session
        fb_user = User.query.filter_by(fb_id=fb_user_id).first()
        user_id = fb_user.user_id

        session['user_id'] = user_id
        session['current_acces_token'] = current_acces_token

        flash('Thanks for creating an account with Make Less Mush')

        return redirect('/home')
예제 #2
0
    def test_add_friendship(self):
        # Ensure function adds friendship to database

        Friendship.add_friendship(1, 2)

        assert Friendship.query.filter_by(admin_id=1, friend_id=2).one()