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)