def update_post(id): body = request.form['body'] title = request.form['title'] title_pic = request.form['title_pic'] published = bool(int(request.form['published'])) post = Post.at(id).getone() published_old = bool(int(post.published)) if not post.published: # only non-published posts update this field post.published = published post.body = body post.title = title post.title_pic = title_pic rows_affected = post.save() if rows_affected >= 0: if not published_old and published: # published flashx.success('Published successfully') return redirect(url_for('post', id=post.id)) else: flashx.success('Saved successfully') if post.published: return redirect(url_for('post', id=post.id)) else: flashx.error('Something wrong when updating post') return redirect(url_for('edit', id=post.id))
def delete(id): query = Post.at(id).delete() rows_affected = query.execute() if rows_affected <= 0: flashx.error('Something wrong when deleting post') return redirect(url_for('edit', id=id)) else: flashx.success('Delete post successfully') return redirect(url_for('write'))
def post(id): post = Post.at(id).getone() if post is None: abort(404) setattr(post, 'html', markdown.render(post.body)) query = Post.where(Post.id._in( Post.where(Post.id > id).select(fn.min(Post.id)), Post.where(Post.id < id).select(fn.max(Post.id)), )).select(Post.id, Post.title) setattr(post, 'next', None) setattr(post, 'prev', None) for pst in query: # execute query if pst.id > id: post.next = pst elif pst.id < id: post.prev = pst return render_public('post.html', post=post)