Example #1
0
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()
    func = result.one()
    total_count = func.count

    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)
Example #2
0
def drafts():
    query = Post.where(
        published=False
    ).orderby(Post.datetime, desc=True).select(
        Post.title, Post.datetime, Post.id
    )
    results = query.execute()
    posts = tuple(results.all())
    return render_template('drafts.html', active_tab='drafts', posts=posts)
Example #3
0
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)