Exemplo n.º 1
0
def follow():
    """ This function allow user to follow other users and
        have that other user's post appear in the timeline
        This has limitation of only taking the 100 latest post
        of the followed user at first time it follow
        input: userID to be followed
        return: timeline of other user is added to the logged in user
                and post from other user will also appear in the future
    """
    if not g.user:
        flash('You are not signed in')
        return redirect(url_for('index', error='Follow Error'))
    error = None
    if request.method == 'POST':
        if 'followUserID' not in request.form:
            error = 'Invalid account'
            flash(error)
        elif request.form['followUserID'] in r_server.lrange(
            'following:%s' % escape(session['user_id']), 0, 100
        ):
            error = "You already follow this person"
            flash(error)
        if not error:
            if r_server.lpush(
                    'following:%s' % escape(
                        session['user_id']
                    ), int(request.form['followUserID'])
            ) and r_server.lpush(
                'followed:%s' % request.form['followUserID'],
                int(session['user_id'])
            ):
                for post in r_server.lrange(
                        'posts:%s' % request.form['followUserID'], 0, 100
                ):
                    r_server.zadd(
                        "timeline:%s" % escape(
                            session['user_id']
                        ), int(post), post
                    )
                flash('successfully follow the account')
                return redirect(
                    url_for(
                        'index',
                        userID=request.form['followUserID']
                    )
                )
        else:
            error = "Failed to follow"
            flash(error)
    else:
        error = "Unable to follow"
        flash(error)
    return redirect(url_for('index', error='Follow Error'))
Exemplo n.º 2
0
def share():
    """"This function will create new post in the user timeline and
        user's follower timeline if successfull,
        user can also attach the picture inside the post
    input: user's post content and image file
    return: success: add new post to user and user's follower timeline
            failure: return to timeline page and show error
    """
    if not g.user:
        error = 'You are not signed in'
        flash(error)
        return redirect(url_for('index', error='Share Error'))
    error = None
    filen = None
    if request.method == 'POST':
        if 'inputPost' not in request.form:
            error = 'Please write your thoughts first'
            flash(error)
        elif len(request.form['inputPost']) > 300:
            error = 'Your thought is too long'
            flash(error)
        try:
            if 'uploadImg' in request.files:
                filen = request.files['uploadImg']
                if filen and not allowed_file(filen.filename):
                    error = 'Please upload correct file'
                    flash(error)
        except IOError:
            error = 'File cannot be found'
            flash(error)
        if not error:
            r_server.incr('next_postID')
            postID = r_server.get('next_postID')
            if r_server.hmset(
                    "post:%s" % postID,
                    {
                        'content': request.form['inputPost'].encode('utf8'),
                        'userID': session['user_id'],
                        'datetime': datetime.now(timezone('UTC')).strftime(
                            "%Y-%m-%dT%H:%M:%S")
                    }
            ) and r_server.lpush(
                'posts:%s' % escape(session['user_id']),
                postID
            ) and r_server.zadd(
                'timeline:%s' % escape(
                    session['user_id']
                ),
                postID, postID
            ) and r_server.zadd(
                'timeline:', postID, postID
            ):
                for follower in r_server.lrange(
                    'followed:%s' % escape(
                        session['user_id']), 0, 1000):
                    r_server.zadd(
                        "timeline:%s" % follower, postID, postID
                    )
                try:
                    if filen:
                        fileType = filen.filename.rsplit('.', 1)[1]
                        k = Key(bucket)
                        k.key = S3_KEY_PREFIX+'post/'+postID
                        k.key += '.'+fileType
                        k.set_contents_from_file(filen)
                        k.make_public()
                        r_server.hset("post:%s" % postID,
                                      "imageURL",
                                      k.generate_url(0).split('?', 1)[0])
                        r_server.hset("post:%s" % postID,
                                      "fileType",
                                      fileType)
                except IOError:
                    error = 'File cannot be found'
                    flash(error)
                    r_server.decr('next_postID')
                    return redirect(url_for('index',
                                            error='Upload File Error'))
            else:
                r_server.decr('next_postID')
            return redirect(url_for('index'))
        else:
            error = "Your thought failed to be posted"
            flash(error)
    else:
        error = "your thought is abstract"
        flash(error)
    return redirect(url_for('index', error='Share Error'))