コード例 #1
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
コード例 #2
0
ファイル: cms.py プロジェクト: syegulalp/mercury
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.

    :param page:
        Page object whose data is to be saved. If this is None, then it is assumed that we are
        creating a new page.
    :param user:
        The user object associated with the save action for this page. If this is a newly-created page,
        the page's user will be set to this.
    :param blog:
        The blog object under which the page will be created, if this is a newly-created page.

    '''

    getunicode = request.forms.getunicode

    # invalidate_cache()

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

    original_page_status = page_status.unpublished
    new_basename = getunicode('basename')

    if page is None:

        # CREATE NEW PAGE ENTRY

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

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

        time_now = datetime.datetime.utcnow()

        page.publication_date = time_now
        page.created_date = time_now

    else:

        # UPDATE EXISTING ENTRY

        # Queue neighbor actions for page BEFORE modification

        if page.status == page_status.published:
            if not (save_action & save_action_list.UNPUBLISH_PAGE):
                queue_page_actions((page.next_page, page.previous_page),no_neighbors=True, no_archive=True)
                queue_page_archive_actions(page)

        original_page_status = page.status
        original_page_basename = page.basename

        page.modified_date = datetime.datetime.utcnow()

        change_basename = False

        if new_basename is not None:
            if new_basename == "":
                change_basename = True
                new_basename = create_basename(getunicode('page_title'),
                    page.blog)
            if new_basename != original_page_basename:
                change_basename = True

        new_publication_date = datetime.datetime.strptime(
            request.forms.get('publication_date'), DATE_FORMAT)

        if change_basename:
            page.basename = create_basename(new_basename, page.blog)

        page.publication_date = page._date_to_utc(page.blog.timezone, new_publication_date).replace(tzinfo=None)

    page.title = getunicode('page_title')
    page.text = getunicode('page_text')
    page.status = page_status.modes[int(request.forms.get('publication_status'))]
    page.tag_text = getunicode('page_tag_text')
    page.excerpt = getunicode('page_excerpt')

    change_note = getunicode('change_note')

    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)  # set a published page to draft
        ):

        unpublish_page(page)
        msg.append("Page <b>{}</b> unpublished successfully.")

    # SET UNPUBLISHED TO PUBLISHED

    elif original_page_status == page_status.unpublished and (save_action & save_action_list.UPDATE_LIVE_PAGE):
        page.status = page_status.published
        msg.append("Set to publish.")

    # SAVE DRAFT

    if (save_action & save_action_list.SAVE_TO_DRAFT):

        try:
            save_result = page.save(user, False, False, change_note)
        except PageNotChanged:
            save_result = (None, None)

        msg.append("Page <b>{}</b> saved successfully.")

        # Assign categories for page

        categories = []

        for n in request.forms.allitems():
            if n[0][:8] == 'cat-sel-':
                try:
                    category_id = int(n[0][8:])
                except ValueError:
                    category_id = None
                else:
                    categories.append(category_id)

        if not categories:
            categories.append(blog.default_category.id)
            msg.append(" Default category auto-assigned for page.")

        page_categories = []

        primary = None

        for n in page.categories:
            if n.category.id not in categories:
                delete_category = PageCategory.delete().where(
                    PageCategory.id == n.id)
                delete_category.execute()
            else:
                page_categories.append(n.category.id)
                if n.primary is True:
                    primary = n

        for n in categories:
            if n not in page_categories:
                new_page_category = PageCategory.create(
                    page=page,
                    category=Category.load(n, blog_id=page.blog.id),
                    primary=False)

        if primary is None:
            n = page.categories[0]
            n.primary = True
            n.save()

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

    # UPDATE TAGS

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

    # QUEUE CHANGES FOR PUBLICATION (if any)

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

        queue_ssi_actions(page.blog)
        queue_page_actions((page,))
        queue_index_actions(page.blog)

        msg.append(" Live page updated.")

    # DETECT ANY PAGE CHANGES

    if (
        (save_action & (save_action_list.SAVE_TO_DRAFT + save_action_list.UPDATE_LIVE_PAGE))
        and (save_result[1]) is None):

        msg.append(" (Page unchanged.)")

    # RETURN REPORT

    tags = template_tags(page=page, user=user)

    status = Status(
        type='success',
        message=' / '.join(msg),
        vals=(page.for_log,)
        )

    tags.status = status
    tags._save_action = save_action
    tags._save_action_list = save_action_list

    return tags
コード例 #3
0
ファイル: cms.py プロジェクト: ra2003/mercury
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.

    :param page:
        Page object whose data is to be saved. If this is None, then it is assumed that we are
        creating a new page.
    :param user:
        The user object associated with the save action for this page. If this is a newly-created page,
        the page's user will be set to this.
    :param blog:
        The blog object under which the page will be created, if this is a newly-created page.

    '''

    getunicode = request.forms.getunicode

    # invalidate_cache()

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

    original_page_status = page_status.unpublished
    new_basename = getunicode('basename')

    if page is None:

        # CREATE NEW PAGE ENTRY

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

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

        time_now = datetime.datetime.utcnow()

        page.publication_date = time_now
        page.created_date = time_now

    else:

        # UPDATE EXISTING ENTRY

        # Queue neighbor actions for page BEFORE modification

        if page.status == page_status.published:
            if not (save_action & save_action_list.UNPUBLISH_PAGE):
                queue_page_actions((page.next_page, page.previous_page),
                                   no_neighbors=True,
                                   no_archive=True)
                queue_page_archive_actions(page)

        original_page_status = page.status
        original_page_basename = page.basename

        page.modified_date = datetime.datetime.utcnow()

        change_basename = False

        if new_basename is not None:
            if new_basename == "":
                change_basename = True
                new_basename = create_basename(getunicode('page_title'),
                                               page.blog)
            if new_basename != original_page_basename:
                change_basename = True

        new_publication_date = datetime.datetime.strptime(
            request.forms.get('publication_date'), DATE_FORMAT)

        if change_basename:
            page.basename = create_basename(new_basename, page.blog)

        page.publication_date = page._date_to_utc(
            page.blog.timezone, new_publication_date).replace(tzinfo=None)

    page.title = getunicode('page_title')
    page.text = getunicode('page_text')
    page.status = page_status.modes[int(
        request.forms.get('publication_status'))]
    page.tag_text = getunicode('page_tag_text')
    page.excerpt = getunicode('page_excerpt')

    change_note = getunicode('change_note')

    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
         )  # set a published page to draft
        ):

        unpublish_page(page)
        msg.append("Page <b>{}</b> unpublished successfully.")

    # SET UNPUBLISHED TO PUBLISHED

    elif original_page_status == page_status.unpublished and (
            save_action & save_action_list.UPDATE_LIVE_PAGE):
        page.status = page_status.published
        msg.append("Set to publish.")

    # SAVE DRAFT

    if (save_action & save_action_list.SAVE_TO_DRAFT):

        try:
            save_result = page.save(user, False, False, change_note)
        except PageNotChanged:
            save_result = (None, None)

        msg.append("Page <b>{}</b> saved successfully.")

        # Assign categories for page

        categories = []

        for n in request.forms.allitems():
            if n[0][:8] == 'cat-sel-':
                try:
                    category_id = int(n[0][8:])
                except ValueError:
                    category_id = None
                else:
                    categories.append(category_id)

        if not categories:
            categories.append(blog.default_category.id)
            msg.append(" Default category auto-assigned for page.")

        page_categories = []

        primary = None

        for n in page.categories:
            if n.category.id not in categories:
                delete_category = PageCategory.delete().where(
                    PageCategory.id == n.id)
                delete_category.execute()
            else:
                page_categories.append(n.category.id)
                if n.primary is True:
                    primary = n

        for n in categories:
            if n not in page_categories:
                new_page_category = PageCategory.create(
                    page=page,
                    category=Category.load(n, blog_id=page.blog.id),
                    primary=False)

        if primary is None:
            n = page.categories[0]
            n.primary = True
            n.save()

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

    # UPDATE TAGS

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

    # QUEUE CHANGES FOR PUBLICATION (if any)

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

        queue_ssi_actions(page.blog)
        queue_page_actions((page, ))
        queue_index_actions(page.blog)

        msg.append(" Live page updated.")

    # DETECT ANY PAGE CHANGES

    if ((save_action &
         (save_action_list.SAVE_TO_DRAFT + save_action_list.UPDATE_LIVE_PAGE))
            and (save_result[1]) is None):

        msg.append(" (Page unchanged.)")

    # RETURN REPORT

    tags = template_tags(page=page, user=user)

    status = Status(type='success',
                    message=' / '.join(msg),
                    vals=(page.for_log, ))

    tags.status = status
    tags._save_action = save_action
    tags._save_action_list = save_action_list

    return tags