Exemple #1
0
def month_context(fileinfo, original_page, tag_context, date_counter):

    if date_counter["year"] is False:
        raise ArchiveMappingFormatException("An archive mapping was encountered that had a month value before a year value.", Exception)

    if fileinfo is None:
        month_context = original_page.publication_date_tz.month
        blog = original_page.blog
    else:
        month_context = fileinfo.month
        blog = fileinfo.template_mapping.template.blog

    month_start = datetime.datetime(
        year=date_counter["year"],
        month=month_context,
        day=1)

    month_start_tz = Page._date_to_utc(None, blog.timezone,
        month_start)

    month_end = datetime.datetime(
        year=date_counter["year"],
        month=month_context,
        day=last_day_of_month(month_start).day,
        hour=23,
        minute=59,
        second=59)

    month_end_tz = Page._date_to_utc(None, blog.timezone,
        month_end)

    tag_context_next = tag_context.select().where(
        Page.publication_date >= month_start_tz,
        Page.publication_date <= month_end_tz
        )

    date_counter["month"] = month_context

    return tag_context_next, date_counter
Exemple #2
0
def year_context(fileinfo, original_page, tag_context, date_counter):

    if fileinfo is None:
        year_context = original_page.publication_date_tz.year
        blog = original_page.blog
    else:
        year_context = fileinfo.year
        blog = fileinfo.template_mapping.template.blog

    year_start = datetime.datetime(
        year=year_context,
        month=1,
        day=1,
        )

    year_start_tz = Page._date_to_utc(None, blog.timezone,
        year_start)

    year_end = datetime.datetime(
        year=year_context,
        month=12,
        day=31,
        hour=23,
        minute=59,
        second=59,
        )

    year_end_tz = Page._date_to_utc(None, blog.timezone,
        year_end)

    tag_context_next = tag_context.select().where(
        Page.publication_date >= year_start_tz,
        Page.publication_date <= year_end_tz
        )

    date_counter["year"] = year_context

    return tag_context_next, date_counter
Exemple #3
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.

    :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
Exemple #4
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.

    :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