Esempio n. 1
0
def new_post():
    author = User.get_current()
    title, content = request.form['title'], request.form['content']
    tag_names = parse_tag_names(request.form['tags'])

    f = request.files['featuredImage']
    if f.filename == '':
        featuredImage = None
    else:
        featuredImage = Attachment(f.filename, data=f.stream)
    if featuredImage and not allowed_file(featuredImage.extension):
        flash('warning', 'Upload a proper image.')
        return redirect(url_for('post_form'))

    post = Post()
    post.title = title
    post.content = content
    post.markedContent = markdown(content)
    post.author = author
    if featuredImage:
        post.featuredImage = featuredImage

    acl = ACL()
    acl.set_public_read_access(True)
    acl.set_write_access(author.id, True)
    post.set_acl(acl)

    post.save()

    tags = []
    for name in tag_names:
        tag = Tag.get_by_name(name)
        if not tag:
            tag = Tag()
            tag.name = name
        tags.append(tag)
    for tag in tags:
        m = TagPostMap()
        m.set({'tag': tag, 'post': post})
        m.save()
        tag.increment('post_count')
    Tag.save_all(tags)

    return redirect(url_for('show_post', post_id=post.id))