Beispiel #1
0
def remove_comment(_id):
    comment = Comment.get_comment(_id)
    try:
        if session['email'] is not None:
            user_email = session['email']
            if user_email == comment.author or is_admin():
                Comment.remove_comment(_id)
                return render_template(
                    'post.html',
                    error="The comment removed successfully.",
                    post=Post.get_post(comment.post_id),
                    comments=Comment.get_comments(comment.post_id),
                    session_email=session['email'])

            return render_template('post.html',
                                   error="The comment doesn't belong to you.",
                                   post=Post.get_post(comment.post_id),
                                   comments=Comment.get_comments(
                                       comment.post_id),
                                   session_email=session['email'])
    except KeyError:
        return render_template(
            'post.html',
            error="You need to <a href='/login'>Log in</a>.",
            post=Post.get_post(comment.post_id),
            session_email=None)
Beispiel #2
0
def get_post(post_id):
    try:
        post = Post.get_post(post_id)
        if post is not None:
            return render_template('post.html',
                                   post=post,
                                   comments=Comment.get_comments(post_id),
                                   session_email=session['email'])
        return render_template('posts.html',
                               posts=Post.get_posts(),
                               session_email=session['email'],
                               error="There is no post by this id.")
    except KeyError:
        return render_template('post.html',
                               comments=Comment.get_comments(post_id),
                               post=Post.get_post(post_id),
                               session_email=None)
Beispiel #3
0
def create_new_comment():
    post_id = request.form['post_id']
    if request.method == 'GET':
        return render_template('post.html', post=Post.get_post(post_id))
    else:
        try:
            author = session['email']
            content = request.form['content_comment']
            new_comment = Comment(content=content,
                                  post_id=post_id,
                                  author=author)
            new_comment.save_to_mongo()
            return render_template('post.html',
                                   comments=Comment.get_comments(post_id),
                                   post=Post.get_post(post_id))
        except KeyError:
            return render_template('post.html',
                                   error="You need to log in.",
                                   post=Post.get_post(post_id),
                                   comments=Comment.get_comments(post_id),
                                   session_email=None)
Beispiel #4
0
def edit_post(post_id):
    try:
        if session['email'] is not None:
            user_email = session['email']
            post = Post.get_post(post_id)
            if user_email == post.author or is_admin():
                return render_template('edit_post.html',
                                       session_email=session['email'],
                                       post=post)

            return render_template('posts.html',
                                   error="This post doesn't belong to you.",
                                   posts=Post.get_posts(),
                                   session_email=session['email'])
    except KeyError:
        return render_template(
            'posts.html',
            error="You need to <a href='/login'>Log in</a>.",
            posts=Post.get_posts(),
            session_email=None)