示例#1
0
def get_a_category(id):
    """``GET`` |API_URL_BASE|/category/:category_id

    Get a category.

    Response JSON:

    .. code-block:: javascript

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

    Permission: ``READ_CATEGORY``
    """
    try:
        category = Category.get(Category.id == id)
    except Category.DoesNotExist:
        return {'id': '该分类不存在'}
    return None, {'category': category.to_dict()}
示例#2
0
def save_page(page, user, blog=None):
    '''
    Saves edits to a page in the CMS.
    Note that this function does _not_ perform permission checking. In other words, it doesn't
    verify if the user described in the `user` parameter does in fact have permissions to
    edit the page in question.
    '''

    save_action = int(request.forms.get('save'))

    blog_new_page = False
    original_page_status = page_status.unpublished

    if page is None:

        blog_new_page = True

        page = Page()
        page.user = user.id
        page.blog = blog.id

        page.basename = create_basename(request.forms.getunicode('page_title'),
                                        page.blog)
        original_page_basename = page.basename

        page.publication_date = datetime.datetime.now()
        page.created_date = datetime.datetime.now()

    else:

        original_page_status = page.status
        original_page_basename = page.basename

        page.modified_date = datetime.datetime.now()

        if request.forms.getunicode('basename') is not None:
            if request.forms.getunicode('basename') != "":
                if original_page_basename != request.forms.getunicode(
                        'basename'):
                    page.basename = create_basename(
                        request.forms.getunicode('basename'), page.blog)

            else:
                page.basename = create_basename(
                    request.forms.getunicode('page_title'), page.blog)

    if original_page_basename != page.basename:
        delete_page_fileinfo(page)

    if page.basename == "":
        page.basename = create_basename(request.forms.getunicode('page_title'),
                                        page.blog)
        original_page_basename = page.basename

    page.title = request.forms.getunicode('page_title')
    page.text = request.forms.getunicode('page_text')
    page.status = page_status.modes[int(
        request.forms.get('publication_status'))]
    page.publication_date = datetime.datetime.strptime(
        request.forms.get('publication_date'), '%Y-%m-%d %H:%M:%S')
    page.tag_text = request.forms.getunicode('page_tag_text')
    page.excerpt = request.forms.getunicode('page_excerpt')

    change_note = request.forms.getunicode('change_note')

    # Save to draft only
    # Save and publish
    # Save and exit
    # Republish and exit
    # Unpublish (and exit)
    # Delete (and unpublish) (and exit)

    msg = ""

    # UNPUBLISH
    if ((save_action & save_action_list.UNPUBLISH_PAGE
         and page.status == page_status.published)
            or  # unpublished a published page
        (original_page_status == page_status.published
         and page.status == page_status.unpublished)
            or  # set a published page to draft
        (save_action & save_action_list.DELETE_PAGE
         )  # delete a page, regardless of status
        ):

        pass

    # DELETE; IMPLIES UNPUBLISH
    if (save_action & save_action_list.DELETE_PAGE):

        pass

    # UNPUBLISHED TO PUBLISHED
    if original_page_status == page_status.unpublished and (
            save_action & save_action_list.UPDATE_LIVE_PAGE):

        page.status = page_status.published

    # SAVE DRAFT
    if (save_action & save_action_list.SAVE_TO_DRAFT):

        backup_only = True if request.forms.getunicode(
            'draft') == "Y" else False
        try:
            save_result = page.save(user, False, backup_only, change_note)
        except PageNotChanged:
            save_result = (None, None)

        if blog_new_page:

            default_blog_category = Category.get(Category.blog == blog.id,
                                                 Category.default == True)

            saved_page_category = PageCategory.create(
                page=page, category=default_blog_category, primary=True)

        msg += ("Page <b>{}</b> saved.")

    # SET TAGS

    # when to do this?
    # what happens when we delete a page?
    # all tags for a page have to be deassigned.

    if request.forms.getunicode('tag_text') is not None:
        tag_text = json.loads(request.forms.getunicode('tag_text'))
        add_tags_to_page(tag_text, page)
        delete_orphaned_tags()

    # BUILD FILEINFO IF NO DELETE ACTION
    if not (save_action & save_action_list.DELETE_PAGE):

        build_pages_fileinfos((page, ))
        build_archives_fileinfos((page, ))

    # PUBLISH CHANGES
    if (save_action & save_action_list.UPDATE_LIVE_PAGE) and (
            page.status == page_status.published):

        queue_page_actions(page)
        queue_index_actions(page.blog)

        msg += (" Live page updated.")

    if (save_action &
        (save_action_list.SAVE_TO_DRAFT +
         save_action_list.UPDATE_LIVE_PAGE)) and (save_result[1]) is None:
        msg += (" (Page unchanged.)")

    tags = template_tags(page_id=page.id, user=user)

    status = Status(type='success', message=msg, vals=(page.title, ))

    tags.status = status

    return tags
示例#3
0
def edit_category(id):
    """``PATCH`` |API_URL_BASE|/category/:category_id

    Edit a category. Currently only support modify category name.

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

    Response JSON:

    .. code-block:: javascript

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

        // failed
        {
            $errors: {
                permission: 'you are not allowed to modify category '
                            'created by other author',
                name: 'this name is invalid.'
            }
        }

    Permission:

        * ``EDIT_CATEGORY``
        * ``EDIT_OTHERS_CATEGORY`` (if attempt to edit
          category created by other.)
    """
    json = request.get_json()
    new_name = json.get('name', '').strip()
    if not new_name:
        return {'name': '请输入有效的分类名'}

    try:
        this_category = Category.get(Category.id == id)
    except Category.DoesNotExist:
        return {'id': '该分类 %s 不存在' % id}

    if this_category.create_by_id != current_user.get_id() \
            and not current_user.can(Permission.EDIT_OTHERS_CATEGORY):
        reason = ('You are not allowed to edit category '
                  'created by other author.')
        return {'permission': reason}

    old_name = this_category.name
    this_category.name = new_name
    this_category.save()

    signals.event_emitted.send(
        current_app._get_current_object(),
        type='Category: Modify',
        description='category(%s) changed from `%s` to `%s`.' %
        (id, old_name, new_name))

    return None, {'category': this_category.to_dict()}