Exemple #1
0
def unfollow():
    """ This function is used to unfollow other user.
        This function will delete 100 latest post from the other user
        and won't show post from the user in the future
    input: user ID to be unfollowed
    return: unfollow user ID and delete 100 latest posts from timeline
    """
    if not g.user:
        flash('You are not signed in')
        return redirect(url_for('index', error='Unfollow Error'))
    error = None
    if request.method == 'POST':
        if 'unfollowUserID' not in request.form:
            error = 'Invalid account'
            flash(error)
        elif request.form['unfollowUserID'] not in r_server.lrange(
            'following:%s' % escape(session['user_id']), 0, 100
        ):
            error = "You are not following this person"
            flash(error)
        if not error:
            if r_server.lrem(
                    'following:%s' % escape(
                        session['user_id']
                    ),
                    int(request.form['unfollowUserID']), 0
            ) and r_server.lrem(
                'followed:%s' % request.form['unfollowUserID'],
                int(session['user_id']), 0
            ):
                for post in r_server.lrange(
                        'posts:%s' % request.form['unfollowUserID'],
                        0, 100
                ):
                    r_server.zrem(
                        "timeline:%s" % escape(
                            session['user_id']
                        ), post
                    )
                flash('successfully unfollow the account')
                return redirect(
                    url_for(
                        'index',
                        userID=request.form['unfollowUserID']
                    )
                )
        else:
            error = "Failed to unfollow"
            flash(error)
    else:
        error = "Please choose who you want to unfollow"
        flash(error)
    return redirect(url_for('index', error="Unfollow Error"))
Exemple #2
0
def deletePost():
    """ This function can be used to delete user's post,
        but user must have be the one who posted it
    input: post_ID
    return: success: delete post from the database
            failure: user doesn't have authority to delete the post
    """
    if not g.user:
        flash('You are not signed in')
        return redirect(url_for('index', error='Deletion Error'))
    error = None
    if request.method == 'POST':
        if 'inputPostID' not in request.form:
            error = "ID is unavailable"
            flash(error)
        elif request.form['inputPostID'] in r_server.lrange(
                'posts:%s' % escape(session['user_id']), 0, 1000):
            postID = request.form["inputPostID"]
            if r_server.hget('post:%s' % postID, 'fileType'):
                k = Key(bucket)
                k.key = S3_KEY_PREFIX+'post/'+postID
                k.key += '.'+r_server.hget('post:%s' % postID, 'fileType')
                bucket.delete_key(k)
            if r_server.lrem(
                    'posts:%s' % escape(session['user_id']),
                    int(postID), 0
            ) and r_server.delete(
                'post:%s' % postID
            ) and r_server.zrem(
                'timeline:%s' % escape(session['user_id']),
                postID
            ) and r_server.zrem('timeline:', postID):
                for follower in r_server.lrange(
                        'followed:%s' % escape(session['user_id']), 0, 1000):
                    r_server.zrem("timeline:%s" % follower, postID)
                flash('deletion successfull')
                return redirect(url_for('index'))
            else:
                flash('deletion failed')
                return redirect(url_for('index', error='delete error'))
        else:
            error = "you are not allowed to delete the post"
            flash(error)
    else:
        error = "you are not allowed to delete the post"
        flash(error)
    return redirect(url_for('index', error='Deletion Error'))