def page(page_number): if page_number <= 0: abort(404) n = 9 query = Post.where(published=True).orderby( Post.datetime, desc=True).limit(n, offset=n * (page_number - 1) ).select() results = query.execute() count = results.count if count < 0: # no posts abort(404) query = Post.where(published=True).select(fn.count(Post.id)) result = query.execute() total_count = result.tuples()[0][0] is_first_page = True if page_number == 1 else False is_last_page = True if n * page_number >= total_count else False posts = tuple(results.all()) page = dict( number=page_number, posts=posts, first=is_first_page, last=is_last_page ) return render_public('page.html', page=page)
def preview(): title = request.form["title"] title_pic = request.form["title_pic"] body = request.form["body"] if not title: flashx.warning("Title is empty!") post = dict( title=title, title_pic=title_pic, html=markdown.render(body), datetime=datetime.now(), id=None # !preview ) return render_public("post.html", post=post)
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)