Example #1
0
def api_create_blog():
    check_admin()
    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 = Blogs(title=title, tags=tags, summary=summary, content=content)
    id = blog.insert_id()
    if tags:
        for tag in tags.split(','):
            tag = Tags(tag=tag, blog=id)
            tag.insert()
    return dict(id=id)
Example #2
0
File: urls.py Project: zhu327/blog
def api_create_blog():
    check_admin()
    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 = Blogs(title=title, tags=tags, summary=summary, content=content)
    id = blog.insert_id()
    tags = tags.split(',')
    if tags:
        sql = "INSERT INTO `tags` (`tag`, `blog`) VALUES {}".format(', '.join(map(
            lambda x: "('{}', '{}')".format(x, id), tags
        )))
        db.execute(sql)
    return dict(id=id)