Exemplo n.º 1
0
def post_id(id):
    try:
        post = Post.query.filter_by(id=id).first()
        postForm = PostForm(obj=post)
    except:
        return redirect('/admin/posts/')

    if postForm.validate_on_submit():
        post.link = slugify_unique(postForm.data['link'], Post, post)
        post.title = postForm.data['title']
        post.body = postForm.data['body']

        if postForm.data['id_category']:
            post.id_category = postForm.data['id_category'].id
        else:
            post.id_category = None

        for tag in post.tags:
            post.tags.remove(tag)

        db.session.commit()

        tags = string.split(postForm.data['tags'], ',')
        for tag in tags:
            try:
                existing = Tag.query.filter_by(link=slugify(tag)).one()
                post.tags.append(existing)
            except:
                new_tag = Tag(name=tag, link=slugify(tag))
                db.session.add(new_tag)
                db.session.commit()
                post.tags.append(new_tag)

        db.session.commit()
        return redirect('/admin/posts/' + str(id) + '/')
    else:
        postForm.tags.data = post.tagsTxt
        postForm.id_category.data = post.category

    fp['breadcrumb'] = [{
        'a': '/admin/posts/',
        'v': 'Posts'
    }, {
        'v': post.title
    }]

    return render_template('admin_post.html',
                           fp=fp,
                           post=post,
                           postForm=postForm)
Exemplo n.º 2
0
def posts():
    fp['breadcrumb'] = [{'v': 'Posts'}]
    posts = Post.query.all()
    postForm = PostForm()
    postMultiForm = PostMultiForm(prefix="multi_")

    if request.args.get('q') == 'add' and postForm.validate_on_submit():
        post = Post(id_category=postForm.data['id_category'].id,
                    link=postForm.data['link'],
                    title=postForm.data['title'],
                    body=postForm.data['body'])
        tags = string.split(postForm.data['tags'], ',')
        post.link = slugify_unique(post.link, Post)
        db.session.add(post)
        db.session.commit()

        for tag in tags:
            try:
                existing = Tag.query.filter_by(link=slugify(tag)).one()
                post.tags.append(existing)
            except:
                new_tag = Tag(name=tag, link=slugify(tag))
                db.session.add(new_tag)
                db.session.commit()
                post.tags.append(new_tag)

        db.session.commit()

        return redirect('/admin/posts/')

    if request.args.get('q') == 'multi':
        if request.form['multi_actions'] == 'D':
            ids = request.form.getlist('selected')
            for id in ids:
                to_del = Post.query.filter_by(id=id).first()
                db.session.delete(to_del)

            db.session.commit()

        return redirect('/admin/posts/')

    return render_template('admin_posts.html',
                           fp=fp,
                           posts=posts,
                           postForm=postForm,
                           postMultiForm=postMultiForm)