Пример #1
0
Файл: page.py Проект: hit9/zhiz
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)
Пример #2
0
Файл: post.py Проект: hit9/zhiz
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))
Пример #3
0
Файл: post.py Проект: hit9/zhiz
def edit(id):
    post = Post.findone(id=id)

    if post is None:
        flashx.error("Request post not found")
        abort(404)
    return render_template("edit.html", post=post, active_tab="eidt")
Пример #4
0
Файл: post.py Проект: hit9/zhiz
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"))
Пример #5
0
Файл: post.py Проект: hit9/zhiz
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)
Пример #6
0
Файл: post.py Проект: hit9/zhiz
def create():
    body = request.form["body"]
    title = request.form["title"]
    title_pic = request.form["title_pic"]
    published = bool(int(request.form["published"]))

    if not title:
        flashx.warning("Empty title")
        return redirect(url_for("write"))

    post = Post.create(title=title, title_pic=title_pic, body=body, datetime=datetime.now(), published=published)
    if published:
        flashx.success("Published successfully")
        return redirect(url_for("post", id=post.id))
    else:
        flashx.success("Saved to drafts successfully")
        return redirect(url_for("edit", id=post.id))  # jump to edit url
Пример #7
0
Файл: post.py Проект: hit9/zhiz
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)