示例#1
0
def api_wikis_update(wid):
    ' update wiki name, description, content by id. '
    wiki = _get_wiki(wid)
    i = ctx.request.input()
    update = False
    if 'name' in i:
        wiki.name = assert_not_empty(i.name, 'name')
        update = True
    if 'description' in i:
        wiki.description = i.description.strip()
        update = True
    if 'content' in i:
        content = assert_not_empty(i.content, 'content')
        wiki.content = content
        update = True
    old_cover_id = ''
    if 'cover' in i:
        f = i.cover
        if f:
            # update cover:
            old_cover_id = wiki.cover_id
            atta = uploaders.upload_cover(wiki.name, f.file.read())
            wiki.cover_id = atta._id
            update = True
    if hasattr(wiki, 'content'):
        wiki.content_id = texts.set(wiki._id, wiki.content)
    if update:
        wiki.update()
    if old_cover_id:
        uploaders.delete_attachment(old_cover_id)
    return dict(result=True)
示例#2
0
def api_create_article():
    i = ctx.request.input(name='', summary='', category_id='', tags='', draft='', publish_time='', cover=None, content='')
    if not i.cover:
        raise APIValueError('cover', 'Cover cannot be empty.')
    name = assert_not_empty(i.name, 'name')
    summary = assert_not_empty(i.summary, 'summary')
    category_id = _check_category_id(i.category_id)
    content = assert_not_empty(i.content, 'content')
    draft = i.draft.lower()=='true'
    if draft:
        publish_time = time.time() + TIME_FEATURE
    else:
        publish_time = time2timestamp(i.publish_time) if i.publish_time else time.time()

    f = i.cover
    atta = uploaders.upload_cover(name, f.file.read())

    article_id = db.next_id()
    article = Articles( \
        _id = article_id, \
        user_id = ctx.user._id, \
        cover_id = atta._id, \
        category_id = category_id, \
        content_id = texts.set(article_id, content), \
        publish_time = publish_time, \
        draft = draft, \
        user_name = ctx.user.name, \
        name = name, \
        summary = summary, \
        tags = texts.format_tags(i.tags) \
    ).insert()
    return dict(_id=article._id)
示例#3
0
def api_update_page(pid):
    page = Pages.get_by_id(pid)
    if page is None:
        raise notfound()
    i = ctx.request.input()
    update = False
    if 'name' in i:
        page.name = assert_not_empty(i.name, 'name')
        update = True
    if 'tags' in i:
        page.tags = texts.format_tags(i.tags)
        update = True
    if 'draft' in i:
        draft = i.draft.lower() == 'true'
        if draft != page.draft:
            page.draft = draft
            update = True
    if 'content' in i:
        content = assert_not_empty(i.content, 'content')
        page.content = content
        update = True

    if hasattr(page, 'content'):
        page.content_id = texts.set(page._id, page.content)
    if update:
        page.update()
    return dict(_id=page._id)
示例#4
0
def api_create_navigation():
    i = ctx.request.input(name='', url='')
    name = assert_not_empty(i.name, 'name')
    url = assert_not_empty(i.url, 'url')
    max_display = db.select_one('select max(display_order) as max from navigations').max
    nav = Navigations(_id=db.next_id(), name=name, url=url, display_order=max_display+1).insert()
    _clear_navigations_cache()
    return nav
示例#5
0
def api_update_navigation(nid):
    nav = Navigations.get_by_id(nid)
    if nav is None:
        raise notfound()
    i = ctx.request.input(name='', url='')
    nav.name = assert_not_empty(i.name, 'name')
    nav.url = assert_not_empty(i.url, 'url')
    nav.update()
    _clear_navigations_cache()
    return dict(result=True)
示例#6
0
def api_update_navigation(nid):
    nav = Navigations.get_by_id(nid)
    if nav is None:
        raise notfound()
    i = ctx.request.input(name='', url='')
    nav.name = assert_not_empty(i.name, 'name')
    nav.url = assert_not_empty(i.url, 'url')
    nav.update()
    _clear_navigations_cache()
    return dict(result=True)
示例#7
0
def api_update_article(aid):
    article = Articles.get_by_id(aid)
    if article is None:
        raise notfound()
    i = ctx.request.input()
    update = False
    if 'name' in i:
        article.name = assert_not_empty(i.name, 'name')
        update = True
    if 'summary' in i:
        article.summary = assert_not_empty(i.summary, 'summary')
        update = True
    if 'category_id' in i:
        article.category_id = _check_category_id(i.category_id)
        update = True
    if 'tags' in i:
        article.tags = texts.format_tags(i.tags)
        update = True
    # update draft first:
    if 'draft' in i:
        if i.draft.lower() == 'true':
            if not article.draft:  # change False to True:
                article.draft = True
                if article.publish_time < TIME_FEATURE:
                    article.publish_time = article.publish_time + TIME_FEATURE
                update = True
        else:
            if article.draft:  # change True to False:
                article.draft = False
                # update publish time:
                if 'publish_time' in i and i.publish_time.strip():
                    article.publish_time = time2timestamp(i.publish_time)
                else:
                    article.publish_time = time.time()
                update = True
    if 'content' in i:
        content = assert_not_empty(i.content, 'content')
        article.content = content
        update = True
    old_cover_id = ''
    if 'cover' in i:
        f = i.cover
        if f:
            # update cover:
            old_cover_id = article.cover_id
            atta = uploaders.upload_cover(article.name, f.file.read())
            article.cover_id = atta._id
            update = True
    if hasattr(article, 'content'):
        article.content_id = texts.set(article._id, article.content)
    if update:
        article.update()
    if old_cover_id:
        uploaders.delete_attachment(old_cover_id)
    return dict(_id=article._id)
示例#8
0
def api_update_article(aid):
    article = Articles.get_by_id(aid)
    if article is None:
        raise notfound()
    i = ctx.request.input()
    update = False
    if 'name' in i:
        article.name = assert_not_empty(i.name, 'name')
        update = True
    if 'summary' in i:
        article.summary = assert_not_empty(i.summary, 'summary')
        update = True
    if 'category_id' in i:
        article.category_id = _check_category_id(i.category_id)
        update = True
    if 'tags' in i:
        article.tags = texts.format_tags(i.tags)
        update = True
    # update draft first:
    if 'draft' in i:
        if i.draft.lower()=='true':
            if not article.draft: # change False to True:
                article.draft = True
                if article.publish_time < TIME_FEATURE:
                    article.publish_time = article.publish_time + TIME_FEATURE
                update = True
        else:
            if article.draft: # change True to False:
                article.draft = False
                # update publish time:
                if 'publish_time' in i and i.publish_time.strip():
                    article.publish_time = time2timestamp(i.publish_time)
                else:
                    article.publish_time = time.time()
                update = True
    if 'content' in i:
        content = assert_not_empty(i.content, 'content')
        article.content = content
        update = True
    old_cover_id = ''
    if 'cover' in i:
        f = i.cover
        if f:
            # update cover:
            old_cover_id = article.cover_id
            atta = uploaders.upload_cover(article.name, f.file.read())
            article.cover_id = atta._id
            update = True
    if hasattr(article, 'content'):
        article.content_id = texts.set(article._id, article.content)
    if update:
        article.update()
    if old_cover_id:
        uploaders.delete_attachment(old_cover_id)
    return dict(_id=article._id)
示例#9
0
def api_create_wiki_page(wid):
    i = ctx.request.input(name='', content='')
    if not 'parent_id' in i:
        raise APIValueError('parent_id', 'bad parameter: parent_id')
    name = assert_not_empty(i.name, 'name')
    content = assert_not_empty(i.content, 'content')
    wiki = _get_wiki(wid)
    parent_id = i.parent_id.strip()
    if parent_id:
        p_page = _get_wikipage(parent_id, wiki._id)
    num = WikiPages.count('where wiki_id=? and parent_id=?', wiki._id, parent_id)
    return _create_wiki_page(wiki._id, parent_id, num, name, content)
示例#10
0
def api_create_navigation():
    i = ctx.request.input(name='', url='')
    name = assert_not_empty(i.name, 'name')
    url = assert_not_empty(i.url, 'url')
    max_display = db.select_one(
        'select max(display_order) as max from navigations').max
    nav = Navigations(_id=db.next_id(),
                      name=name,
                      url=url,
                      display_order=max_display + 1).insert()
    _clear_navigations_cache()
    return nav
示例#11
0
def api_update_wikipage(wpid):
    page = _get_wikipage(wpid)
    i = ctx.request.input()
    update = False
    if 'name' in i:
        page.name = assert_not_empty(i.name, 'name')
        update = True
    if 'content' in i:
        content = assert_not_empty(i.content, 'content')
        page.content_id = texts.set(wpid, content)
        update = True
    if update:
        page.update()
    return dict(result=True)
示例#12
0
def api_create_wikipage_comment(wpid):
    u = ctx.user
    if u is None:
        raise APIPermissionError()
    i = ctx.request.input(content='')
    content = assert_not_empty(i.content, 'content')
    wikipage = _get_wikipage(wpid)
    return comments.create_comment('wikipage', wpid, content)
示例#13
0
def api_create_wiki():
    ' create a new wiki. '
    i = ctx.request.input(name='', description='', content='', cover=None)
    if not i.cover:
        raise APIValueError('cover', 'Cover cannot be empty.')
    name = assert_not_empty(i.name, 'name')
    description = i.description.strip()
    content = assert_not_empty(i.content, 'content')
    f = i.cover
    atta = uploaders.upload_cover(name, f.file.read())
    wiki_id = db.next_str()
    wiki = Wikis( \
        _id=wiki_id, \
        cover_id = atta._id, \
        name=name, \
        description=description, \
        content_id=texts.set(wiki_id, content)).insert()
    return wiki
示例#14
0
def api_create_article_comment(aid):
    u = ctx.user
    if u is None:
        raise APIPermissionError()
    i = ctx.request.input(content='')
    content = assert_not_empty(i.content, 'content')
    a = Articles.get_by_id(aid)
    if a is None:
        raise notfound()
    return comments.create_comment('article', aid, content)
示例#15
0
def api_create_category():
    '''
    Create a new category.
    '''
    i = ctx.request.input(name='', description='')
    name = assert_not_empty(i.name, 'name')
    description = i.description.strip()
    c = Categories(name=name, description=description, display_order=len(_get_categories())).insert()
    _clear_categories_cache()
    return c
示例#16
0
def api_create_article_comment(aid):
    u = ctx.user
    if u is None:
        raise APIPermissionError()
    i = ctx.request.input(content='')
    content = assert_not_empty(i.content, 'content')
    a = Articles.get_by_id(aid)
    if a is None:
        raise notfound()
    return comments.create_comment('article', aid, content)
示例#17
0
def api_create_page():
    i = ctx.request.input(alias='', name='', tags='', draft='', content='')
    alias = assert_not_empty(i.alias, 'alias').lower()
    if _RE_ALIAS.match(alias) is None:
        raise APIValueError('alias', 'Invalid alias.')
    if Pages.select_one('where alias=?', alias):
        raise APIValueError('alias', 'Alias already exist.')
    name = assert_not_empty(i.name, 'name')
    content = assert_not_empty(i.content, 'content')
    draft = i.draft.lower() == 'true'
    page_id = db.next_id()
    page = Pages( \
        _id = page_id, \
        alias = alias, \
        content_id = texts.set(page_id, content), \
        draft = draft, \
        name = name, \
        tags = texts.format_tags(i.tags) \
    ).insert()
    return dict(_id=page._id)
示例#18
0
def api_create_category():
    '''
    Create a new category.
    '''
    i = ctx.request.input(name='', description='')
    name = assert_not_empty(i.name, 'name')
    description = i.description.strip()
    c = Categories(name=name,
                   description=description,
                   display_order=len(_get_categories())).insert()
    _clear_categories_cache()
    return c
示例#19
0
def api_create_article():
    i = ctx.request.input(name='',
                          summary='',
                          category_id='',
                          tags='',
                          draft='',
                          publish_time='',
                          cover=None,
                          content='')
    if not i.cover:
        raise APIValueError('cover', 'Cover cannot be empty.')
    name = assert_not_empty(i.name, 'name')
    summary = assert_not_empty(i.summary, 'summary')
    category_id = _check_category_id(i.category_id)
    content = assert_not_empty(i.content, 'content')
    draft = i.draft.lower() == 'true'
    if draft:
        publish_time = time.time() + TIME_FEATURE
    else:
        publish_time = time2timestamp(
            i.publish_time) if i.publish_time else time.time()

    f = i.cover
    atta = uploaders.upload_cover(name, f.file.read())

    article_id = db.next_id()
    article = Articles( \
        _id = article_id, \
        user_id = ctx.user._id, \
        cover_id = atta._id, \
        category_id = category_id, \
        content_id = texts.set(article_id, content), \
        publish_time = publish_time, \
        draft = draft, \
        user_name = ctx.user.name, \
        name = name, \
        summary = summary, \
        tags = texts.format_tags(i.tags) \
    ).insert()
    return dict(_id=article._id)
示例#20
0
def api_update_category(cid):
    cat = Categories.get_by_id(cid)
    if cat is None:
        raise APIValueError('_id', 'category not found.')
    i = ctx.request.input()
    update = False
    if 'name' in i:
        cat.name = assert_not_empty(i.name.strip(), 'name')
        update = True
    if 'description' in i:
        cat.description = i.description.strip()
        update = True
    if update:
        cat.update()
        _clear_categories_cache()
    return dict(result=True)
示例#21
0
def api_update_category(cid):
    cat = Categories.get_by_id(cid)
    if cat is None:
        raise APIValueError('_id', 'category not found.')
    i = ctx.request.input()
    update = False
    if 'name' in i:
        cat.name = assert_not_empty(i.name.strip(), 'name')
        update = True
    if 'description' in i:
        cat.description = i.description.strip()
        update = True
    if update:
        cat.update()
        _clear_categories_cache()
    return dict(result=True)