예제 #1
0
def tweet_by_id(tweet_id):
    """
    This method handles Tweet by its Id View.
    :param tweet_id: Id of Tweet for which we want to display Comments.
    :return: If "GET" rendering template "tweet_by_id" and displays all comments for that Tweet,
            If "POST" adds new Comment to db and redirecting back to tweet_by_id.
    """
    if not session['logged_in']:
        return redirect(url_for('login'))

    if request.method == "GET":
        cnx = connect_db()
        tweet = Tweet.load_tweet_by_id(cnx.cursor(), tweet_id)
        user = User.load_user_by_id(cnx.cursor(), tweet.user_id)
        comments = Comment.load_comments_by_tweet_id(cnx.cursor(), tweet_id)
        return render_template('tweet_by_id.html',
                               tweet=tweet,
                               user=user,
                               comments=comments)

    elif request.method == "POST":
        comment = Comment()
        comment.user_id = session['user_id']
        comment.tweet_id = tweet_id
        comment.text = request.form['new_comment']
        comment.creation_date = datetime.now()

        cnx = connect_db()
        comment.add_comment(cnx.cursor())
        cnx.commit()
        return redirect(('tweet_by_id/{}'.format(tweet_id)))
예제 #2
0
def post_comment():
    if request.method == "POST":
        user_id = session.get("user_id")
        comment = request.form.get("comment")
        Comment.add_comment(g.db,user_id=user_id,user_comment=comment)
        return redirect("/comment")
    return redirect("/")
예제 #3
0
def add_comment(tid):
    user = current_user()
    if user is None:
        return 'fail'
    res = Comment.add_comment(user, tid, request.form)
    if res is None:
        return 'fail'
    else:
        return 'success'