Example #1
0
def blog_post(kword_or_id):
    session = DBSession()
    post = session.query(Post).filter(Post.url_kword == kword_or_id).first()
    if not post and kword_or_id.isdigit():
        post = session.query(Post).get(int(kword_or_id))
    if not post:
        raise HTTPNotFound
    post.content_title = post.title or _title_from_content(post.content)
    parts = publish_parts(post.content, writer_name="html")

    html_body = parts["html_body"]

    prev_post = session.query(Post).filter(Post.id < post.id).order_by(desc(Post.id))
    if prev_post.count() > 0:
        prev_post = prev_post[0]
        prev_post.content_title = prev_post.title or _title_from_content(prev_post.content)
    else:
        prev_post = None

    next_post = session.query(Post).filter(Post.id > post.id).order_by(asc(Post.id))
    if next_post.count() > 0:
        next_post = next_post[0]
        next_post.content_title = next_post.title or _title_from_content(next_post.content)
    else:
        next_post = None

    post.html_content = html_body
    return {"post": post, "recent_posts": recent_posts(), "prev_post": prev_post, "next_post": next_post}
Example #2
0
def recent_posts():
    "return recent 5 posts."
    session = DBSession()
    posts = session.query(Post).order_by(desc(Post.timestamp))
    posts = posts.slice(0, 5)
    result = []
    for p in posts:
        p.url = p.url_kword or unicode(p.id)
        p.content_title = p.title or _title_from_content(p.content)
        result.append(p)
    return result
Example #3
0
def blog_archive():
    session = DBSession()
    posts = session.query(Post).order_by(desc(Post.id))

    title = u"ARCHIVES"
    post_by_year = []
    for post in posts:
        post.url = post.url_kword or unicode(post.id)
        if not post_by_year or (post.date.year != post_by_year[-1][0]):
            post_by_year.append([post.date.year, [post]])
        else:
            post_by_year[-1][1].append(post)
    return dict(post_by_year=post_by_year, recent_posts=recent_posts(), month_name=month_name, title=title)
Example #4
0
def blog_tag(tag_name):
    session = DBSession()
    tag = session.query(Tag).filter(Tag.name == tag_name)[0]
    posts = session.query(Post).filter(Post.tags.contains(tag)).order_by(desc(Post.date))

    title = u"Tag: %s" % tag_name
    post_by_year = []
    for post in posts:
        post.url = post.url_kword or unicode(post.id)
        if not post_by_year or (post.date.year != post_by_year[-1][0]):
            post_by_year.append([post.date.year, [post]])
        else:
            post_by_year[-1][1].append(post)
    return dict(post_by_year=post_by_year, recent_posts=recent_posts(), month_name=month_name, title=title)
Example #5
0
def rss(host):
    session = DBSession()
    posts = session.query(Post).order_by(desc(Post.timestamp))[:20]
    rss = PyRSS2Gen.RSS2(
        title="Vincent's Blog feed",
        link=u"http://" + host,
        description="Vincent's blog...",
        lastBuildDate=datetime.datetime.utcnow(),
        items=[_rss_item(host, p) for p in posts],
    )
    f = StringIO.StringIO()
    rss.write_xml(f, encoding="utf8")
    response = Response(f.getvalue())
    response.content_type = "application/rss+xml"
    return response
Example #6
0
def blog_index(page):
    posts_1page = 9
    session = DBSession()
    posts = session.query(Post).order_by(desc(Post.timestamp))
    _posts = []
    for post in posts[page * posts_1page : (page + 1) * posts_1page]:
        post.content_title = post.title or _title_from_content(post.content)
        post.summary_html = publish_parts(post.summary, writer_name="html")["html_body"]
        post.url = post.url_kword or unicode(post.id)
        _posts.append(post)
    return {
        "posts": _posts,
        "page_num": page,
        "max_page_num": posts.count() / posts_1page,
        "recent_posts": recent_posts(),
    }
Example #7
0
def post(request):
    id = request.params.get("id")
    session = DBSession()
    if request.method == "POST":
        title, content = request.POST["title"], request.POST["content"]
        tags = request.POST["tags"]
        summary = request.POST["summary"]
        url_kword = request.POST["url_kword"]
        assert title not in ("", None), "Missing title"
        assert url_kword not in ("", None), "Missing url_kword"
        assert content not in ("", None), "Missing content"
        p = None
        if id:
            p = session.query(Post).get(id)
        else:
            p = Post(timestamp=time.time(), date=datetime.date.today())
            session.add(p)
        p.title = title
        p.content = content
        p.summary = summary
        p.url_kword = url_kword
        if tags:
            tags = tags.split(",")
            tags = [v.strip() for v in tags]
            tags_db = []
            for tag in tags:
                t = session.query(Tag).filter_by(name=tag).first()
                if not t:
                    t = Tag(name=tag)
                tags_db.append(t)
            for i in range(len(p.tags)):
                p.tags.pop()
            for t in tags_db:
                p.tags.append(t)

        session.flush()
        return HTTPFound(location=request.route_url("blog_home"))
    else:
        entry = None
        if id:
            entry = session.query(Post).get(id)
        start = entry.id - 2 if entry else 0
        entries = session.query(Post).filter(Post.id >= start).limit(5)
        return dict(entry=entry, entries=entries, recent_posts=recent_posts())
Example #8
0
def delete(request):
    pid = request.params.get("post_id")
    if pid:
        session = DBSession()
        p = session.query(Post).get(pid)
        session.delete(p)
        session.flush()
    else:
        return Response("Not find")
    return Response("Delete done!")