Ejemplo n.º 1
0
def show_post(title):
    with session_context() as session:
        post = session.query(Post).options(
            subqueryload(Post.tags)
        ).filter(Post.title == title)

        if post.count() < 1:
            redirect('/')

        post = post.one()
        post.view_count += 1

        # get related posts
        # dont forget to filter self
        tag_ids = [_t.id for _t in post.tags]

        related_posts = session.query(Post).filter(
            Post.tags.any(Tag.id.in_(tag_ids))
        ).filter(
            Post.id != post.id
        ).order_by(Post.view_count.desc())

        session.commit()

    return {'post': post, 'title': post.title, 'related_posts': related_posts}
Ejemplo n.º 2
0
def new_post():
    key = request.forms.get('key')
    
    if not key_verified(key):
        return '<h2>Security Key NOT matched!</h2>'
    
    title = request.forms.get('title').strip()
    if not title:
        return "<h2>Error: title is empty!</h2>"

    tags_str = request.forms.get('tags').strip().strip(",")
    if not tags_str:
        return "<h2>Error: tag is empty!</h2>"
    tags = [t.strip() for t in tags_str.split(',')]
    
    md = request.forms.get('markdown')
    if not md:
        return "<h2>Error: no contents!</h2>"

    code = request.forms.get('code')
    html = Convert("md").convert(md)
    try:
        with session_context() as session:
            if not code:
                p = store_new_post(session, title, html, md, tags)
            else:
                p = edit_post(session, code, title, html, md, tags)
    except Exception as err:
        p = None
        msg = str(err)
    return template('jump2article', p = p, msg = '' if p else msg)
Ejemplo n.º 3
0
def filter_by_tag(tag):
    with session_context() as session:
        posts = session.query(Post).filter(Post.tags.any(Tag.name==tag)
                                        ).order_by(Post.create_at.desc())

    posts = group_posts(posts)
    return {'posts': posts}
Ejemplo n.º 4
0
def edit():
    
    key = request.forms.get('key')
    if not key_verified(key):
        return {"example": "Security key NOT matched!", "title": '', 'tags': '', 'code': ''}
    post_id = request.forms.get("blog_id")
    try:
        post_id_int = int(post_id)
    except ValueError as err:
        example = str(err)
    

    with session_context() as session:
        p = session.query(Post).filter(Post.id == post_id_int)
        if p.count > 0:
            p = p.one()
            title = p.title
            example = p.markdown
            tags_list = [t.name for t in p.tags]
            tags = ','.join(tags_list)
            code = p.code
        else:
            example = 'No blog found!'
            title = ''
            tags = ''
            code = ''

    return {"example": example, "title": title, "tags": tags, "code": code}
Ejemplo n.º 5
0
def detail(year, month, day, slug):
    year = int(year)
    month = int(month)
    day = int(day)
    with session_context() as session:
        post = session.query(Post).options(
            subqueryload(Post.tags)).filter(Post.slug == slug).filter(
                extract('year', Post.create_at) == year,
                extract('month', Post.create_at) == month,
                extract('day', Post.create_at) == day)
        try:
            if post.count() < 1:
                redirect('/blog')
        except:
            print "error occurred"
            redirect('/blog')
        post = post.first()
        post.view_count += 1
        # get related posts
        # dont forget to filter self
        tag_ids = [_t.id for _t in post.tags]
        related_posts = session.query(Post).filter(
            Post.tags.any(Tag.id.in_(tag_ids))
        ).filter(
            Post.id != post.id
        ).order_by(Post.view_count.desc())

        session.commit()
    return {'post': post, 'title': post.title, 'related_posts': related_posts}
Ejemplo n.º 6
0
def index_year(year):
    year = int(year)
    with session_context() as session:
        posts = session.query(Post).options(
            subqueryload(Post.tags)).filter(
                extract('year', Post.create_at) == year).order_by(
                    Post.create_at.desc()).limit(20)
    return {'posts': posts, 'index': True, 'is_by_year': year}
Ejemplo n.º 7
0
def archive_year(year):
    year = int(year)
    with session_context() as session:
        posts = session.query(Post).filter(
            extract('year', Post.create_at) == year).order_by(
                Post.create_at.desc())
    posts = group_posts(posts)
    return {'posts': posts}
Ejemplo n.º 8
0
def archive():
    with session_context() as session:
        posts = session.query(Post).order_by(Post.create_at.desc())

    posts = group_posts(posts)
    return {'posts': posts}
Ejemplo n.º 9
0
def index():
    with session_context() as session:
        posts = session.query(Post).order_by(Post.create_at.desc()).limit(3)

    return {'posts': posts, 'index': True}
Ejemplo n.º 10
0
    except:
        return 'Invalid content!\n'

    try:
        c = Convert(ext)
    except NotImplementedError:
        return 'This format not supported yet!\n'
    except Exception:
        return 'Error occurred!\n'

    try:
        html = c.convert(content)
    except ClassNotFound, e:
        return 'ClassNotFound, %s\n' % str(e)

    with session_context() as session:
        p = session.query(Post).filter(Post.title == title)
        if p.count() > 0:
            # update
            edit_post(session, p.one(), html, tags)
        else:
            # new blog
            store_new_post(session, title, html, tags)

        session.commit()

    # make rss
    make_rss()
    return 'Done\n'