Exemplo n.º 1
0
def edit(post_id):
    # Return post form, but filled out
    # Use existing PostForm
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post) # post here is the post in previous line; fills out the form with that data
    if form.validate_on_submit():
        original_image=post.image
        form.populate_obj(post) # Replaces post with the contents of the form
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("The image was not uploaded.")
            if filename:
                post.image=filename
        else:
            post.image = original_image
        
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
    return render_template('blog/post.html', form=form, post=post, action="edit")
Exemplo n.º 2
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        #pdb.set_trace()
        image = request.files.get('image')
        filename = None
        print("image: ", image)
        try:
            filename = uploaded_images.save(image)
            print("filename: ", filename)
        except:
            flash("The image was not uploaded.")
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        author = Author.query.filter_by(username=session['username']).first()
        author_id = author.id
        blog = Blog.query.filter_by(admin=author_id).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action="new")
Exemplo n.º 3
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form=PostForm(obj=post)
    if form.validate_on_submit():
        original_image=post.image
        form.populate_obj(post)
        if form.image.has_file():
            image=request.files.get('image')
            try:
                filename= uploaded_images.save(image)
            except:
                flash("Image was not uploaded")
            if filename:
                post.image=filename
        else:
            post.image=original_image
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))        
    return render_template('blog/post.html', form=form, post=post, action='edit')
    
    
Exemplo n.º 4
0
def blog_post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash("The image was not uploaded")
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('blog_article', slug=slug))
    return render_template('blog/blog_post.html', form=form, action="new")
Exemplo n.º 5
0
def post():

    form = PostForm()
    tags_field = request.values.get("tags_field", '')

    if form.validate_on_submit():
        image_id = None

        if form.image.data:
            file_path, img_id = secure_image_uploader(
                form, file_path=BLOG_POST_IMAGES_PATH)
            _resize_post_image_for_home_page_and_post_page(file_path, img_id)
            image_id = img_id

        category = _get_form_category_data(form)

        author = Author.query.get(Session.get_session_by_id())
        title, body = _get_data_from_form(form)

        post = Post(author=author,
                    title=title,
                    body=body,
                    image=image_id,
                    category=category)
        _save_tag(post, tags_field)
        db.session.add(post)
        db.session.commit()

        slug = _gen_slug_from_post(post)
        flash(constants.ARTICLE_POSTED_MSG)

        if is_safe_url('blog_app.article'):
            return redirect(url_for('blog_app.articles', slug=slug))
    return render_template("blog/post.html", form=form, action="new")
Exemplo n.º 6
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post)
    if form.validate_on_submit():
        original_image = post.image
        form.populate_obj(post)
        if form.image.has_file():
            image = request.files.get('image')
            filename = None
            try:
                filename = uploaded_images.save(image)
            except:
                flash("The image was not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = original_image
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
    return render_template('blog/post.html',
                           form=form,
                           post=post,
                           action="edit")
Exemplo n.º 7
0
def post():
    form = PostForm()
    comment_form = CommentForm()
    error=None
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename=uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush() # trick again, hasn't committed but creates object, commit in the end.
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data) # gets primary key (id) of the selection
            category = Category.query.filter_by(id=category_id).first()
        else:
            category=None
        blog = Blog.query.first() # assuming one blog, if system with more than 1 blog - figure out here
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug)) # sending to article page with slug
        
    return render_template('blog/post.html', form=form, comment_form=comment_form, action="new")
Exemplo n.º 8
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post) # assign values to form from object above, inbuilt feature
    if form.validate_on_submit():
        original_image = post.image
        form.populate_obj(post) # loads and replaces post object with contents of the form
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("Image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = original_image # post.image has been populated by the form content so need to redefine (if user hasnt chosen a file)
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
            
    return render_template('blog/post.html', form=form, post=post, action=edit) # pre loading post
Exemplo n.º 9
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action='new')
Exemplo n.º 10
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash("The image was not uploaded")

        # we are assuming we can have only category per post
        # basic order is check for new category, else check for new existing category and if none of these are presen
        if form.new_category.data:
            # checks if a new category is created or not
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            # check if a user seleected existing category
            category_id = form.category.get_pk(form.category.data)
            # helper function get_pk gives out the primary key
            category = Category.query.filter_by(id=category_id).first()
        else:
            # So this is condition when no category is specified
            # So we put all that in the unknown category
            # we search if there is already that category created otherwise we create it
            try:
                category_id = form.category.get_pk("Unknown")
                category = Category.query.filter_by(id=category_id).first()
            except:
                category = Category("Unknown")

        blog = Blog.query.first()
        # assuming we have only one blog
        # need to add functionality if more than one blog
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))

    return render_template('blog/post.html', form=form, action="new")
Exemplo n.º 11
0
def edit(post_id):

    post = Post.query.filter_by(id=post_id).first_or_404()

    blog = Blog.query.all()

    form = PostForm(obj=post)

    if form.validate_on_submit():
        original_image = post.img
        # form.populate_obj(post)
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash('Image could not be uploded')
            if filename:
                post.img = filename
        else:
            post.img = original_image

        if(form.maincategory.data):
            post.maincategory_id = (form.maincategory.data).id
        if(form.subcategory.data):
            post.subcategory_id = (form.subcategory.data).id
        if(form.subsubcategory.data):
            post.subsubcategory_id = (form.subsubcategory.data).id

        if form.new_subcategory.data:
            maincategory.add_subcategory(form.new_subcategory.data, maincategory.id)
            subcategory = SubCategory.query.filter_by(name=form.new_subcategory.data)
            post.subcategory = subcategory.id

        if form.new_subsubcategory.data:
            subcategory.add_subsubcategory(form.new_subsubcategory.data, subcategory.id)
            subsubcategory = SubSubCategory.query.filter_by(name=form.new_subsubcategory.data).first()
            post.subsubcategory = subsubcategory.id


        db.session.commit()
        return redirect(url_for('.article', slug = post.slug))

    return render_template('/admin/post.html', form=form, post=post, action='edit', blog=blog)
Exemplo n.º 12
0
def post():
    #return 'Blog post'
    form = PostForm()
    error = ""

    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')

        if form.new_category.data:
            # add new category to database
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(
                form.category.data)  # get primary key for form category field
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None

        blog = Blog.query.first()  # this app has only 1 blog

        author = Author.query.filter_by(username=session['username']).first()

        title = form.title.data
        body = form.body.data
        slug = slugify(title)

        post = Post(blog, author, title, body, category, filename, slug)

        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))

    return render_template('blog/post.html',
                           form=form,
                           error=error,
                           action="new")
Exemplo n.º 13
0
def post():
    form = PostForm()
    error = ''
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('File not uploaded')
        if form.new_category.data:
            new_category  = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        slug = slugify(form.title.data)
        post = Post(
            blog,
            author,
            form.title.data,
            form.body.data,
            category,
            filename,
            slug
            )
        db.session.add(post)
        db.session.flush()
        
        if post.id:
            db.session.commit()
            flash('Post Created')
            return redirect(url_for('article', slug=slug))
        else:
            db.session.rollback()
            error = 'Error creating post'
    return render_template('blog/post.html', form=form, error=error)
Exemplo n.º 14
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        blog = Blog.query.first()
        author = User.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form)
Exemplo n.º 15
0
def post():
    #return 'Blog post'
    form = PostForm()
    error = ""
    
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
            
        if form.new_category.data:
            # add new category to database
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data) # get primary key for form category field
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
            
        blog = Blog.query.first() # this app has only 1 blog
        
        author = Author.query.filter_by(username = session['username']).first()
        
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        
        post = Post(blog, author, title, body,category, filename, slug)
            
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
        
        
    return render_template('blog/post.html', form=form, error=error, action="new")
Exemplo n.º 16
0
def post_page():
    form = PostForm()

    if form.validate_on_submit():
        # import pdb; pdb.set_trace()
        image = request.files.get("image")
        filename = None

        try:
            filename = uploaded_images.save(image)
        except Exception as e:
            flash("The image was not uploaded! ")
            print(e)

        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data

        try:
            blog = Blog.query.first()
            blog_id = blog.blog_id
            user = User.query.filter_by(username=session["username"]).first()
            author = user.user_id
            print(str(author))
            title = form.title.data
            body = form.body.data
            slug = slugify(form.title.data)
            post = Post(blog_id, author, title, body, category, slug, filename)
            db.session.add(post)
            db.session.commit()
            return redirect(url_for("article_page", slug=slug))
        except Exception as e:
            print(e)
            error = "error found while committing to database"
            return render_template("blog/post.html", form=form, error=error)
    return render_template("blog/post.html", form=form, action="new")
Exemplo n.º 17
0
def edit(post_id):
    # get record
    post = Post.query.filter_by(id=post_id).first_or_404()
    # populate form fields with record data
    form = PostForm(obj=post)
    # form validated
    if form.validate_on_submit():
        # save image
        orig_image = post.image
        # repopulate from form fields
        form.populate_obj(post)

        # if form has new image
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("the image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = orig_image

        if form.new_category.data:
            new_cat = Category(form.new_category.data)
            db.session.add(new_cat)
            # write new category to get id
            db.session.flush()
            post.category = new_cat

        # write full record to database
        db.session.commit()

        return redirect(url_for('article', slug=post.slug))

    return render_template('blog/post.html',
                           form=form,
                           post=post,
                           action="edit")
Exemplo n.º 18
0
def post_page():
    form = PostForm()

    if form.validate_on_submit():
        # import pdb; pdb.set_trace()
        image = request.files.get("image")
        filename = None

        try:
            filename = uploaded_images.save(image)
        except Exception as e:
            flash("The image was not uploaded! ")
            print(e)

        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data

        try:
            blog = Blog.query.first()
            blog_id = blog.blog_id
            user = User.query.filter_by(username=session["username"]).first()
            author = user.user_id
            print(str(author))
            title = form.title.data
            body = form.body.data
            slug = slugify(form.title.data)
            post = Post(blog_id, author, title, body, category, slug, filename)
            db.session.add(post)
            db.session.commit()
            return redirect(url_for("article_page", slug=slug))
        except Exception as e:
            print(e)
            error = "error found while committing to database"
            return render_template("blog/post.html", form=form, error=error)
    return render_template("blog/post.html", form=form, action="new")
Exemplo n.º 19
0
def edit(post_id):
    # get record
    post = Post.query.filter_by(id=post_id).first_or_404()
    # populate form fields with record data
    form = PostForm(obj=post)
    # form validated
    if form.validate_on_submit():
        # save image
        orig_image = post.image
        # repopulate from form fields
        form.populate_obj(post)
        
        # if form has new image
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("the image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = orig_image
            
        if form.new_category.data:
            new_cat = Category(form.new_category.data)
            db.session.add(new_cat)
            # write new category to get id
            db.session.flush()
            post.category = new_cat
        
        # write full record to database
        db.session.commit()
        
        return redirect(url_for('article', slug=post.slug))
                
            
            
    return render_template('blog/post.html', form=form, post=post, action="edit")
Exemplo n.º 20
0
def edit(slug):

    post = Post.query.filter_by(slug=slug).first_or_404()
    form = PostForm(obj=post)

    if form.validate_on_submit():

        original_img_id, original_title = post.image, post.title
        form.populate_obj(post)

        post.image = _if_image_is_found_process_image(form, original_img_id)
        _if_new_category_is_found_process(form, post)
        _if_post_title_has_changed_slugify(form, post, original_title)

        db.session.commit()

        flash(constants.ARTICLE_EDITED_MSG)

        if is_safe_url('blog_articles_app.articles'):
            return redirect(url_for('blog_articles_app.articles', slug=post.slug))

    return render_template("blog/post.html", form=form, post=post, action="edit")
Exemplo n.º 21
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form)
Exemplo n.º 22
0
def post():
    form = PostForm()
    error = ''
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('File not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        slug = slugify(form.title.data)
        post = Post(blog, author, form.title.data, form.body.data, category,
                    filename, slug)
        db.session.add(post)
        db.session.flush()

        if post.id:
            db.session.commit()
            flash('Post Created')
            return redirect(url_for('article', slug=slug))
        else:
            db.session.rollback()
            error = 'Error creating post'
    return render_template('blog/post.html', form=form, error=error)
Exemplo n.º 23
0
def write(id=0):
    post_form = PostForm()
    # 标签
    tag_ids = []
    tagids = db.session.query(articles_tags).filter_by(article_id=id)
    for tagid in tagids:
        tag_ids.append(tagid[1])
    # 新增时
    if id == 0:
        # current_user._get_current_object()获取当前用户,只用current_user报错
        post = Article(create_user=g.user._get_current_object())
        page = {'catalog_id': '', 'id': id, 'tag_ids': tag_ids}
    # 修改时
    else:
        post = Article.query.get_or_404(id)
        post_form.body.data = post.body
        post_form.title.data = post.title
        post_form.order.data = post.order_id
        post_form.photo.data = post.photo
        page = {'catalog_id': post.catalog_id, 'id': id, 'tag_ids': tag_ids}
    # 提交内容
    if post_form.po_submit.data and post_form.validate_on_submit():
        post.title = yrh.req('title')
        post.catalog_id = yrh.req('catalog_id')
        post.body = request.form.get('body')
        post.recommand = yrh.req('recommand')
        post.order_id = yrh.req('order') or 0
        post_form.photo.data = post.photo
        if request.files['photo']:
            post.photo = yrh.req('title') + '-' + secure_filename(
                request.files['photo'].filename)
            upload_file(request.files['photo'], yrh.req('title'))

        # 标题、分类、链接图片必须有值
        if post.title and post.catalog_id and post.photo and post.body:
            db.session.add(post)
            db.session.commit()
            # 更新标签
            yrh.execute('delete from articles_tags where article_id=:id',
                        {'id': id})
            id = yrh.execute('select max(id) from article',
                             origin=True).first()[0] if id == 0 else id
            for tag_id in request.values.getlist('tag_id'):
                yrh.execute(
                    '''
                    insert into articles_tags (article_id,tag_id) 
                    value (:id,:tag_id)''', {
                        'id': id,
                        'tag_id': tag_id
                    })
            db.session.commit()
            return redirect(url_for('content.detail', id=post.id))
    title = u'添加新文章'
    if id > 0:
        title = u'编辑 - %s' % post.title
    # 如果没有修改也没有新增,回滚Post(author=g.user._get_current_object())
    db.session.rollback()
    return render_template('write.html',
                           title=title,
                           post_form=post_form,
                           post=post,
                           catalogs=Catalog.query,
                           tags=Tag.query,
                           login_form=LoginForm(),
                           hots=g.hot_list,
                           links=g.links,
                           page=yrh.eip_format(page),
                           para={'title': ''})
Exemplo n.º 24
0
def blogpost():

    blog = Blog.query.all()

    form = PostForm()
    filename = None

    if form.validate_on_submit():
        image = request.files.get('image')
        if image:
            try:
                filename = uploaded_images.save(image)
            except:
                flash('The image could not be uploaded')
        else:
            print("File error!")

        maincategory = form.maincategory.data

        if form.new_subcategory.data:
            if not SubCategory.query.filter_by(
                    name=form.new_subcategory.data).first():
                maincategory.add_subcategory(form.new_subcategory.data,
                                             maincategory.id)
            subcategory = SubCategory.query.filter_by(
                name=form.new_subcategory.data).first()
        elif form.subcategory.data:
            subcategory = form.subcategory.data
        else:
            if (not SubCategory.query.filter_by(name="No Category")):
                maincategory.add_subcategory("No Category", maincategory.id)

            subcategory = SubCategory.query.filter_by(
                name="No Category").first()

        if form.new_subsubcategory.data:
            if not SubSubCategory.query.filter_by(
                    name=form.new_subsubcategory.data).first():
                subcategory.add_subsubcategory(form.new_subsubcategory.data,
                                               subcategory.id)
            subsubcategory = SubSubCategory.query.filter_by(
                name=form.new_subsubcategory.data).first()

        elif form.subsubcategory.data:
            subsubcategory = form.subsubcategory.data
        else:
            if (not SubSubCategory.query.filter_by(
                    name="No Category").first()):
                subcategory.add_subsubcategory("No Category", subcategory.id)

            subsubcategory = SubSubCategory.query.filter_by(
                name="No Category").first()

        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data

        slug = slugify(title)
        post = Post(blog, author, title, body, maincategory.id, subcategory.id,
                    subsubcategory.id, filename, slug)

        # Now add the Keywords

        # Extract Keywords from form and add them to Table
        if form.keywords.data:
            keywordstring = form.keywords.data

            # Remove whitespaces and split
            keywords = keywordstring.replace(' ', '').split(",")

            post.add_keywords(keywords)

        db.session.add(post)
        db.session.commit()

        return redirect(url_for('blog_app.article', slug=slug))

    return render_template("admin/post.html",
                           form=form,
                           action='new',
                           blog=blog)