Beispiel #1
0
def search():
    """ This function is to search other available user
        according to the user's text input
        if user doesn't input anything, it will return all available user
    input: user search input
    return: list of all available user
    """
    if not g.user:
        flash("You are not signed in")
        return redirect(url_for('index'))
    matching = []
    if request.method == 'POST':
        for key in r_server.hkeys('users'):
            if key:
                currentUser = r_server.hgetall('user:%s' %
                                               r_server.hget('users', key))
            if currentUser:
                if request.form['inputSearch'].lower() in currentUser.get(
                        'firstName'
                ).lower() or request.form[
                    'inputSearch'].lower() in currentUser.get(
                    'lastName'
                ).lower() or request.form[
                    'inputSearch'].lower() in currentUser.get(
                    'email'
                ).lower():
                    if currentUser.get('email') not in g.user.get('email'):
                        matching.append(currentUser)
        return render_template('search.html', matching=matching)
    else:
        error = "Unable to search"
        flash(error)
    return redirect(url_for('index', error='Search Error'))
Beispiel #2
0
def index(userID=None):
    """ This is main function and page of the web application
    input:  without userID parameter, user will see his/her own timeline
            [userID] of a user to see a user's timeline
    return: user_id not in session will return user to index.html
            user_id in session will redirect user to timeline.html
    """
    error = None
    if not error and 'error' in request.args:
        error = request.args['error']
    if g.user:
        others = False
        followed = False
        if 'pagination' in request.args and int(
                request.args['pagination']) > 0:
            pagination = int(request.args['pagination'])
        else:
            pagination = 1
        if not userID or userID == session['user_id']:
            userID = session['user_id']
        else:
            if userID in r_server.lrange('following:%s' % escape(
                    session['user_id']), 0, 1000):
                followed = True
            others = True
        # upper and lower postID of post shown
        minimum = (pagination-1)*numitem
        maximum = pagination*numitem-1
        # list of shown postID
        posts = r_server.zrange('timeline:%s' % userID, minimum,
                                maximum, True)
        # get total length of existing post from user
        plen = int(math.ceil(int(r_server.zcard(
            'timeline:%s' % userID))/float(numitem)))
        # get the current accessed user Data
        userData = r_server.hgetall('user:%s' % userID)
        # posts to show in timeline
        postData = {}
        # profile of the user who post
        userPost = {}
        # number of follower from current user
        follower = r_server.llen(
            'followed:%s' % escape(session['user_id']))
        # number of following from current user
        following = r_server.llen(
            'following:%s' % escape(session['user_id']))
        # number of follower from userID
        followerO = r_server.llen('followed:%s' % userID)
        # number of following from userID
        followingO = r_server.llen('following:%s' % userID)
        for post in posts:
            postData[post] = r_server.hgetall('post:%s' % post)
            userPost[post] = r_server.hgetall('user:%s' %
                                              postData[post].get('userID'))
        return render_template('timeline.html', plen=plen,
                               pagination=pagination, followed=followed,
                               userData=userData, posts=posts,
                               postData=postData,
                               userPost=userPost, others=others,
                               follower=follower, following=following,
                               followerO=followerO, userID=userID,
                               followingO=followingO, error=error)
    else:
        posts = r_server.zrange('timeline:', 0, 100, True)
        postData = {}
        userPost = {}
        for post in posts:
            postData[post] = r_server.hgetall('post:%s' % post)
            userPost[post] = r_server.hgetall('user:%s' %
                                              postData[post].get('userID'))
        return render_template('index.html', CLIENT_ID=CLIENT_ID,
                               posts=posts,
                               postData=postData, userPost=userPost,
                               error=error)
Beispiel #3
0
def before_request():
    g.user = None
    if 'user_id' in session:
        g.user = r_server.hgetall('user:%s' % escape(session['user_id']))