コード例 #1
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.body.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('动态已发布!')
        return redirect(url_for('blog.index'))

    page = request.args.get('page', 1, type=int)
    per = current_app.config['POSTS_PER_PAGE']

    postz = current_user.likes_and_self_posts()
    posts = postz.paginate(page, per, False)

    next_url = url_for('blog.index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('blog.index',
                       page=posts.prev_num) if posts.has_prev else None
    return render_template("blog/index.html",
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
コード例 #2
0
ファイル: views.py プロジェクト: patallen/patallen.me
def addPost():
    """Return add post form or process new blog post"""
    form = PostForm()

    if form.validate_on_submit():
        post = Post(title=form.title.data)
        form.populate_obj(post)
        post.author = current_user
        db.session.add(post)
        db.session.commit()

        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form)
コード例 #3
0
def addPost():
    """Return add post form or process new blog post"""
    form = PostForm()

    if form.validate_on_submit():
        post = Post(title=form.title.data)
        form.populate_obj(post)
        post.author = current_user
        db.session.add(post)
        db.session.commit()

        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form)
コード例 #4
0
ファイル: views.py プロジェクト: patallen/patallen.me
def editPost(post_slug):
    """Edit an existing blog post"""
    post = get_or_404(Post, slug=post_slug)

    if current_user != post.author:
        raise Unauthorized("You don't have permission to edit this post.")

    form = PostForm(obj=post)

    if form.validate_on_submit():
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form, post=post)
コード例 #5
0
def editPost(post_slug):
    """Edit an existing blog post"""
    post = get_or_404(Post, slug=post_slug)

    if current_user != post.author:
        raise Unauthorized("You don't have permission to edit this post.")

    form = PostForm(obj=post)

    if form.validate_on_submit():
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form, post=post)
コード例 #6
0
def update_post(post_id):
    if not Admin.query.filter_by(email=current_user.email).first():
        abort(403)
    title = 'Update Post'
    post = BlogPost.query.get_or_404(post_id)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Post has been updated.', 'success')
        return redirect(url_for('blog.view_post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.author.data = post.author
        form.content.data = post.content

    return render_template('blog/edit_post.html', title=title, form=form)
コード例 #7
0
def new_post():
    if not Admin.query.filter_by(email=current_user.email).first():
        abort(403)
    title = 'New Post'
    form = PostForm()
    if form.validate_on_submit():
        post = BlogPost(author=form.author.data,
                        title=form.title.data,
                        content=form.content.data)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created.', 'success')
        return redirect(url_for('blog.view_post', post_id=post.id))
    elif request.method == 'GET':
        if request.args.get('author'):
            form.author.data = request.args.get('author')

    return render_template('blog/edit_post.html', title=title, form=form)