Esempio n. 1
0
def post_submit():
    """
    This view is for submitting posts. An account is required for submitting posts.
    Get parameters supply the tag name, e.g. a link from the tag page to post with that tag.
    Post request receive the form submission
    """
    # Forbid submission of post if user is not logged in
    if not check.logged_in():
        error_context = {
            'error_name':
            "403 Forbidden",
            'error_info':
            "You may not post without an account. Please log in or create an account"
        }
        return render_template('error.html', **error_context)
    # User is logged in, show text submission form
    else:
        form = TextPostForm()

        if form.validate_on_submit():
            post = Post()
            post.user_id = int(session['user_id'])
            post.date = datetime.now()
            post.title = form.title.data
            post.content_type = form.content_type.data
            post.content = form.content.data
            post.content_html = md.render(form.content.data)
            # TODO: Implement external links
            post.is_external = False
            post.current_vote = 0
            post.is_banned = False
            post.comment_count = 0
            # TODO: Implement tag existance check
            #       This should be done with custom validator after tags are created
            try:
                tag = Tag(form.tag.data)
                print(form.tag.data)
                post.tag_id = tag.id
            except NotImplementedError as error:
                error_context = {
                    'error_name': "INVALID TAG",
                    'error_info': "the tag you entered is invalid"
                }
                return render_template('error.html', **error_context)

            post.save()

            flash('Post created sucessfully')
            return redirect(url_for('post_pages.post_view', post_id=post.id))

        else:
            return render_template('post_text_submit.html', form=form)
Esempio n. 2
0
def post_edit(post_id):
    # check if post exists
    try:
        post = Post(post_id, False)
    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)

    # 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 (post.user_id == session['user_id']):
        error_context = {
            'error_name': "Unauthorized",
            'error_info': "You are not the original poster"
        }
        return render_template('error.html', **error_context)

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

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

    return render_template('post_text_edit.html',
                           form=form,
                           body=post.content,
                           name="Post")