示例#1
0
文件: urls.py 项目: zhu327/blog
def api_modify_blog(blog_id):
    check_admin()
    blog = Blogs.get(blog_id)
    if not blog:
        raise APIValueError(blog_id, 'blog is not exist.')
    i= ctx.request.input(title='', tags='', content='')
    title = i['title'].strip()
    tags = i['tags'].strip()
    content = i['content'].strip()
    if not title:
        raise APIValueError('title', 'title can not be empty.')
    if not content:
        raise APIValueError('content', 'content can not be empty.')
    summary = _get_summary(content)
    blog.title = title
    blog.summary = summary
    blog.content = content
    blog.tags = tags
    blog.update()
    db.execute('delete from `tags` where `blog`=?', blog_id)
    tags = tags.split(',')
    if tags:
        sql = "INSERT INTO `tags` (`tag`, `blog`) VALUES {}".format(', '.join(map(
            lambda x: "('{}', '{}')".format(x, blog_id), tags
        )))
        db.execute(sql)
    return dict(id=blog_id)
示例#2
0
def api_delete_blog(blog_id):
    check_admin()
    blog = Blogs.get(blog_id)
    if blog is None:
        raise APIResourceNotFound('Blog')
    blog.delete()
    return dict(id=blog_id)
示例#3
0
文件: urls.py 项目: 1007650105/blog
def api_modify_blog(blog_id):
    check_admin()
    blog = Blogs.get(blog_id)
    if not blog:
        raise APIValueError(blog_id, 'blog is not exist.')
    i= ctx.request.input(title='', tags='', content='')
    title = i['title'].strip()
    tags = i['tags'].strip()
    content = i['content'].strip()
    if not title:
        raise APIValueError('title', 'title can not be empty.')
    if not content:
        raise APIValueError('content', 'content can not be empty.')
    summary = _get_summary(content)
    blog.title = title
    blog.summary = summary
    blog.content = content
    blog.tags = tags
    blog.update()
    db.execute('delete from `tags` where `blog`=?', blog_id)
    if tags:
        for tag in tags.split(','):
            tag = Tags(tag=tag, blog=blog_id)
            tag.insert()
    return dict(id=blog_id)
示例#4
0
文件: urls.py 项目: zhu327/blog
def api_delete_blog(blog_id):
    check_admin()
    blog = Blogs.get(blog_id)
    if not blog:
        raise APIValueError(blog_id, 'blog is not exist.')
    blog.delete()
    db.execute('delete from `tags` where `blog`=?', blog_id)
    return dict(id=blog_id)
示例#5
0
文件: urls.py 项目: zhu327/blog
def blog(blog_id):
    blog = Blogs.get(blog_id)
    if not blog:
        raise notfound()
    if blog.tags:
        blog.xtags = blog.tags.split(',')
    rps = Blogs.find_by('order by created desc limit ?', 3)
    return dict(blog=blog, rps=rps)
示例#6
0
def blog(blog_id):
    blog = Blogs.get(blog_id)
    if blog is None:
        raise notfound()
    blog.html_content = markdown2.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)
示例#7
0
def manage_blogs_edit(blog_id):
    blog = Blogs.get(blog_id)
    if blog is None:
        raise notfound()
    return dict(id=blog.id,
                name=blog.name,
                summary=blog.summary,
                content=blog.content,
                action='/api/blogs/%s' % blog_id,
                redirect='/manage/blogs',
                user=ctx.request.user)
示例#8
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 APIResourceNotFound('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)
示例#9
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 APIResourceNotFound('Blog')
    blog.name = name
    blog.summary = summary
    blog.content = content
    blog.update()
    return blog
示例#10
0
文件: urls.py 项目: zhu327/blog
def api_get_blog(blog_id):
    blog = Blogs.get(blog_id)
    if not blog:
        raise APIValueError(blog_id, 'blog is not exist.')
    return blog
示例#11
0
文件: urls.py 项目: zhu327/blog
def manage_blog_modify(blog_id):
    blog = Blogs.get(blog_id)
    if not blog:
        raise notfound()
    return dict(id=blog.id, action='/api/blogs/%s' % blog_id, redirect='/manage/blogs')
示例#12
0
def api_get_blog(blog_id):
    blog = Blogs.get(blog_id)
    if blog:
        return blog
    raise APIResourceNotFound('Blog')