示例#1
0
def create_category():
    """``POST`` |API_URL_BASE|/category/:category_id

    Create a category.

    :param id: **JSON Param**, required
    :param name: **JSON Param**, required

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            category: {
                id: string,
                name: string,
                article_count: integer
            }
        }

        // failed
        {
            $errors: {
                id: 'this id is invalid.',
                name: 'this name is invalid.',
                id: 'this id is duplicated.'
            }
        }

    Permission: ``CREATE_CATEGORY``
    """
    json = request.get_json()

    name = json.get('name', '').strip()
    if not name:
        return {'name': '请输入有效的分类名'}

    id = json.get('id', '')
    if not re_match(app_config['CATEGORY_ID_PATTERN'], id):
        return {'id': app_config['CATEGORY_ID_DESCRIPTION']}

    if Category.select().where(Category.id == id).count() == 1:
        return {'id': '该分类ID %s 已存在' % id}

    if Category.select().where(Category.name == name).count() == 1:
        return {'name': '该分类名 %s 已存在' % name}

    new_category = Category.create(id=id, name=name)

    signals.event_emitted.send(current_app._get_current_object(),
                               type='Category: Create',
                               description='category(%s) has been create.' %
                               new_category.id)

    return None, {'category': new_category.to_dict()}
示例#2
0
def category_context(fileinfo, original_page, tag_context, date_counter):

    if fileinfo is None:
        category_context = PageCategory.select(PageCategory.category).where(
            PageCategory.page == original_page)
    else:
        category_context = PageCategory.select(PageCategory.category).where(
            PageCategory.category == Category.select().where(Category.id == fileinfo.category).get())

    page_constraint = PageCategory.select(PageCategory.page).where(PageCategory.category << category_context)
    tag_context_next = tag_context.select().where(Page.id << page_constraint)

    return tag_context_next, date_counter