コード例 #1
0
def comment_edit(comment_id):
    # check if post exists
    try:
        comment = Comment(comment_id)
    except:
        error_context = {
            'error_name': "404 Not Found",
            'error_info': "The comment you tried to access does not exist"
        }
        return render_template('error.html', **error_context)

    # check if user is logged in
    if not check.logged_in():
        error_context = {
            'error_name': "Unauthorized",
            'error_info': "You must log in first"
        }
        return render_template('error.html', **error_context)

    # check if user is OP
    if not (comment.user_id == session['user_id']):
        error_context = {
            'error_name': "Unauthorized",
            'error_info': "You are not the original poster of this comment"
        }
        return render_template('error.html', **error_context)

    # get POST input
    form = CommentForm(content=comment.content)
    if form.validate_on_submit():
        comment.content = form.content.data
        comment.content_html = md.render(form.content.data)
        comment.save()

        flash("Comment edited successfully")
        return redirect(
            url_for('post_pages.post_view', post_id=comment.post_id))

    return render_template('post_text_edit.html',
                           form=form,
                           body=comment.content,
                           name="Comment")
コード例 #2
0
def post_view(post_id):
    # get post info by id,
    # render post block
    # TO DO AFTER:
    # expose comment input
    # get all comments
    # group comments (nesting)
    # sort comments by opt
    """
    Expand this DOCSTRING
    """
    try:
        # get post object with all comments
        post = Post(post_id, True)
    except:
        error_context = {
            'error_name': "404 Not Found",
            'error_info': "The post you tried to access does not exist"
        }
        return render_template('error.html', **error_context)

    form = CommentForm()
    if form.validate_on_submit():
        if not check.logged_in():
            error_context = {
                'error_name':
                "403 Forbidden",
                'error_info':
                "You may not comment without an account. Please log in or create an account"
            }
            return render_template('error.html', **error_context)
        # create comment
        comment = Comment()
        comment.user_id = session['user_id']
        comment.post_id = post_id
        comment.content_type = 'text'
        comment.content = form.content.data
        comment.content_html = md.render(form.content.data)
        comment.is_external = False
        comment.date = datetime.now()
        comment.current_vote = 0

        comment.save()

        post.comment_count += 1
        post.save()

        flash('Comment created successfuly')
        # reload page with the new comment
        return redirect(url_for('post_pages.post_view', post_id=post_id))

    context = post.generate_context()
    # sets flag if viewer is logged in
    context['is_logged_in'] = check.logged_in()
    # sets flag if viewer is the original poster (a.k.a OP)
    context['is_op'] = context['is_logged_in'] and (post.user_id
                                                    == session['user_id'])
    if not context['is_op']:
        context['is_op'] = context['is_logged_in'] and TagModerator.is_mod(
            session['user_id'], post.tag_id)

    return render_template('post.html', **context, form=form)