Esempio n. 1
0
def api_delete_blog(blog_id):
    check_admin()
    blog = Blogs.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    blog.delete()
    return dict(id=blog_id)
Esempio n. 2
0
def blog(blog_id):
    blog = Blogs.get(blog_id)
    if blog is None:
        raise HttpError.notfound()
    blog.html_content = markdown.markdown(blog.content)
    comments = Comments.find_by(
        'where blog_id=? order by created_at desc limit 1000', blog_id)
    return dict(blog=blog, comments=comments, user=ctx.request.user)
Esempio n. 3
0
def api_create_blog():
    check_admin()
    i = ctx.request.input(name='', summary='', content='')
    name = i.name.strip()
    summary = i.summary.strip()
    content = i.content.strip()
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not summary:
        raise APIValueError('summary', 'summary cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    user = ctx.request.user
    blog = Blogs(user_id=user.id,
                 user_name=user.name,
                 name=name,
                 summary=summary,
                 content=content)
    blog.insert()
    return blog
Esempio n. 4
0
def manage_blogs_edit(blog_id):
    blog = Blogs.get(blog_id)
    if blog is None:
        raise HttpError.notfound()
    return dict(id=blog.id,
                name=blog.name,
                summary=blog.summary,
                content=blog.content,
                action=path_join(_MODULE, '/api/blogs/%s' % blog_id),
                redirect=path_join(_MODULE, '/manage/blogs'),
                user=ctx.request.user)
Esempio n. 5
0
def api_create_blog_comment(blog_id):
    user = ctx.request.user
    if user is None:
        raise APIPermissionError('Need signin.')
    blog = Blogs.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    content = ctx.request.input(content='').content.strip()
    if not content:
        raise APIValueError('content')
    c = Comments(blog_id=blog_id,
                 user_id=user.id,
                 user_name=user.name,
                 user_image=user.image,
                 content=content)
    c.insert()
    return dict(comment=c)
Esempio n. 6
0
def api_update_blog(blog_id):
    check_admin()
    i = ctx.request.input(name='', summary='', content='')
    name = i.name.strip()
    summary = i.summary.strip()
    content = i.content.strip()
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not summary:
        raise APIValueError('summary', 'summary cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    blog = Blogs.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    blog.name = name
    blog.summary = summary
    blog.content = content
    blog.update()
    return blog
Esempio n. 7
0
def _get_blogs_by_page():
    total = Blogs.count_all()
    page = Page(total, _get_page_index())
    blogs = Blogs.find_by('order by created_at desc limit ?,?', page.offset,
                          page.limit)
    return blogs, page.to_dict()
Esempio n. 8
0
def api_get_blog(blog_id):
    blog = Blogs.get(blog_id)
    if blog:
        return blog
    raise APIResourceNotFoundError('Blog')