Esempio n. 1
0
def api_update_article():
    time.sleep(1);
    i = ctx.request.input(id='')
    if not i.id:
        raise APIValueError('id', 'id cannot be empty.')
    article = _get_article(i.id)
    kw = {}
    if 'name' in i:
        name = i.name.strip()
        if not name:
            raise APIValueError('name', 'name cannot be empty.')
        kw['name'] = name
    if 'content' in i:
        content = i.content.strip()
        if not content:
            raise APIValueError('content', 'content cannot be empty.')
        html_content, summary = html.parse_md(content, 1000)
        kw['content'] = content
        kw['summary'] = summary
    if 'category_id' in i:
        category_id = i.category_id
        cat = _get_category(category_id)
        kw['category_id'] = category_id
    if 'tags' in i:
        kw['tags'] = _format_tags(i.tags)
    if 'draft' in i:
        draft = boolean(i.draft)
        if not draft and ctx.user.role_id==ROLE_CONTRIBUTORS:
            raise APIPermissionError('cannot publish article for contributors.')
        kw['draft'] = draft
    if kw:
        kw['modified_time'] = time.time()
        kw['version'] = article.version + 1
        db.update_kw('articles', 'id=?', i.id, **kw)
    return True
Esempio n. 2
0
def api_create_article():
    i = ctx.request.input(category_id='', name='', tags='', content='', draft='')
    name = i.name.strip()
    content = i.content.strip()
    category_id = i.category_id
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    if not category_id:
        raise APIValueError('category_id', 'category_id cannot be empty.')
    cat = _get_category(category_id)
    draft = True
    if ctx.user.role_id < ROLE_CONTRIBUTORS:
        draft = True if i.draft else False
    current = time.time()
    html_content, summary = html.parse_md(content, 800)
    article = Dict( \
        id=db.next_str(), \
        website_id=ctx.website.id, \
        user_id=ctx.user.id, \
        user_name=ctx.user.name, \
        category_id=category_id, \
        draft=draft, \
        name=name, \
        tags=_format_tags(i.tags), \
        read_count=0, \
        summary=summary, \
        content=content, \
        creation_time=current, \
        modified_time=current, \
        version=0)
    db.insert('articles', **article)
    return article
Esempio n. 3
0
def api_create_article():
    i = ctx.request.input(category_id='',
                          name='',
                          tags='',
                          content='',
                          draft='')
    name = i.name.strip()
    content = i.content.strip()
    category_id = i.category_id
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    if not category_id:
        raise APIValueError('category_id', 'category_id cannot be empty.')
    cat = _get_category(category_id)
    draft = True
    if ctx.user.role_id < ROLE_CONTRIBUTORS:
        draft = True if i.draft else False
    current = time.time()
    html_content, summary = html.parse_md(content, 800)
    article = Dict( \
        id=db.next_str(), \
        website_id=ctx.website.id, \
        user_id=ctx.user.id, \
        user_name=ctx.user.name, \
        category_id=category_id, \
        draft=draft, \
        name=name, \
        tags=_format_tags(i.tags), \
        read_count=0, \
        summary=summary, \
        content=content, \
        creation_time=current, \
        modified_time=current, \
        version=0)
    db.insert('articles', **article)
    return article
Esempio n. 4
0
def api_update_article():
    time.sleep(1)
    i = ctx.request.input(id='')
    if not i.id:
        raise APIValueError('id', 'id cannot be empty.')
    article = _get_article(i.id)
    kw = {}
    if 'name' in i:
        name = i.name.strip()
        if not name:
            raise APIValueError('name', 'name cannot be empty.')
        kw['name'] = name
    if 'content' in i:
        content = i.content.strip()
        if not content:
            raise APIValueError('content', 'content cannot be empty.')
        html_content, summary = html.parse_md(content, 1000)
        kw['content'] = content
        kw['summary'] = summary
    if 'category_id' in i:
        category_id = i.category_id
        cat = _get_category(category_id)
        kw['category_id'] = category_id
    if 'tags' in i:
        kw['tags'] = _format_tags(i.tags)
    if 'draft' in i:
        draft = boolean(i.draft)
        if not draft and ctx.user.role_id == ROLE_CONTRIBUTORS:
            raise APIPermissionError(
                'cannot publish article for contributors.')
        kw['draft'] = draft
    if kw:
        kw['modified_time'] = time.time()
        kw['version'] = article.version + 1
        db.update_kw('articles', 'id=?', i.id, **kw)
    return True