Esempio n. 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.

    :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
Esempio n. 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
Esempio n. 3
0
def blog_import(blog_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_publisher(user, blog)
    reason = auth.check_template_lock(blog, True)

    tags = template_tags(blog=blog, user=user)

    import os, settings
    import_path = os.path.join(settings.APPLICATION_PATH, "data",
                               "import.json")

    tags.status = reason

    if request.method == "POST":
        from core.models import db
        tpl = ''
        with db.atomic() as txn:
            import json
            from core.utils import string_to_date

            import_path = request.forms.getunicode('import_path')
            with open(import_path, 'r', encoding='utf8') as f:
                json_data = json.load(f)

            from core.models import page_status, MediaAssociation, Category
            from core.error import PageNotChanged
            from core.libs.peewee import InterfaceError
            from core.cms import media_filetypes
            format_str = "<b>{}</b> / (<i>{}</i>)"

            # TODO: go in chunks of 50 or something?
            # allow graceful disconnection?
            for n in json_data:
                q = []
                n_id = n['id']
                q.append("Checking {}".format(n_id))
                changed = False
                found = False
                match = Page.kv_get('legacy_id', n_id)
                if match.count() > 0:
                    if match[0].object_ref.blog == blog:
                        found = True
                        q.append(match[0].key + "/" + match[0].value +
                                 " / Exists: " +
                                 format_str.format(n['title'], n_id))
                        existing_entry = Page.load(match[0].objectid)
                        update = existing_entry.kv_get('update').count()
                        # raise Exception(update)
                        q.append('{} / {}'.format(
                            string_to_date(
                                n['modified_date']).replace(tzinfo=None),
                            existing_entry.modified_date))
                        if string_to_date(n['modified_date']).replace(
                                tzinfo=None
                        ) <= existing_entry.modified_date and update == 0:
                            q.append('Existing page {} not changed.'.format(
                                existing_entry.id))
                        else:
                            changed = True
                            q.append(
                                'Updating data for existing page {}.'.format(
                                    existing_entry.id))
                            existing_entry.title = n['title']
                            existing_entry.text = n['text']
                            existing_entry.basename = n['basename']
                            existing_entry.excerpt = n['excerpt']

                            existing_entry.created_date = string_to_date(
                                n['created_date']).replace(tzinfo=None)
                            existing_entry.modified_date = string_to_date(
                                n['modified_date']).replace(tzinfo=None)
                            existing_entry.publication_date = string_to_date(
                                n['publication_date']).replace(tzinfo=None)

                            try:
                                existing_entry.save(
                                    user, False, False,
                                    'New revision from import')
                            except PageNotChanged:
                                pass
                            except InterfaceError:
                                raise Exception(
                                    "Error saving {}. Check the JSON to make sure it's valid."
                                    .format(n_id))

                            for media in existing_entry.media:
                                media.kv_del()

                            existing_entry.clear_categories()
                            existing_entry.clear_kvs()
                            existing_entry.clear_tags()
                            existing_entry.clear_media()

                            entry = existing_entry

                if found is False:
                    q.append("Creating: " +
                             format_str.format(n['title'], n_id))
                    changed = True
                    new_entry = Page(
                        title=n['title'],
                        text=n['text'],
                        basename=n['basename'],
                        excerpt=n['excerpt'],
                        user=user,
                        blog=blog,
                        created_date=string_to_date(n['created_date']),
                        publication_date=string_to_date(n['publication_date']),
                        modified_date=string_to_date(n['modified_date']),
                    )

                    new_entry.modified_date = new_entry.publication_date

                    if n['status'] in ('Publish', 'Published', 'Live'):
                        new_entry.status = page_status.published

                    new_entry.save(user)

                    entry = new_entry

                    q.append("New ID: {}".format(entry.id))

                    # Everything from here on out is

                if changed:

                    # Register a legacy ID for the page

                    entry.kv_set("legacy_id", n["id"])
                    entry.kv_set("legacy_user", n["user_id"])

                    # Category assignments

                    categories = n['categories']
                    if categories == []:
                        saved_page_category = PageCategory.create(
                            page=entry,
                            category=blog.default_category,
                            primary=True).save()
                    else:
                        primary = True
                        for category in categories:
                            cat_exists = False

                            category_id = category['id']
                            existing_category = Category.kv_get(
                                'legacy_id', category_id)
                            if existing_category.count() > 0:
                                if existing_category[
                                        0].object_ref.blog == blog:
                                    cat_exists = True

                            if cat_exists is False:

                                q.append('Created new category {}/{}'.format(
                                    category_id, category['name']))
                                new_category = Category.create(
                                    blog=blog,
                                    title=category['name'],
                                    parent_category=getattr(
                                        category, 'parent', None))
                                new_category.save()

                                new_category.kv_set('legacy_id', category_id)
                            else:
                                new_category = Category.load(
                                    existing_category[0].objectid)
                                q.append(
                                    'Added to existing category {}/{}'.format(
                                        new_category.id, category['name']))

                            saved_page_category = PageCategory.create(
                                page=entry,
                                category=new_category,
                                primary=primary).save()
                            primary = False

                    # Check to make sure a default category exists for the whole blog.
                    # If not, assign one based on the lowest ID.
                    # This can always be reassigned later.

                    # Register tags

                    tags_added, tags_existing, _ = Tag.add_or_create(
                        n['tags'], page=entry)

                    q.append('Tags added: {}'.format(','.join(
                        n.tag for n in tags_added)))
                    q.append('Tags existing: {}'.format(','.join(
                        n.tag for n in tags_existing)))

                    # Register KVs

                    kvs = n['kvs']
                    for key in kvs:
                        if key != "":
                            value = kvs[key]
                            entry.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    # Register media

                    media = n['media']

                    for m in media:

                        if 'path' not in m:
                            continue

                        path = os.path.split(m['path'])

                        try:
                            new_media = Media.get(Media.url == m['url'])
                        except:
                            new_media = Media(filename=path[1],
                                              path=m['path'],
                                              url=m['url'],
                                              type=media_filetypes.image,
                                              created_date=string_to_date(
                                                  m['created_date']),
                                              modified_date=string_to_date(
                                                  m['modified_date']),
                                              friendly_name=m['friendly_name'],
                                              user=user,
                                              blog=blog,
                                              site=blog.site)

                        # TODO: RBF
                        try:
                            new_media.save()
                        except Exception:
                            continue

                        media_association = MediaAssociation(media=new_media,
                                                             page=entry)

                        media_association.save()

                        # Save legacy ID to KV on media

                        if 'id' in m:
                            new_media.kv_set('legacy_id', m['id'])

                        q.append('IMG: {}'.format(new_media.url))

                        # add tags for media

                        q.append('Tags: {}'.format(m['tags']))
                        new_tags = Tag.add_or_create(m['tags'],
                                                     media=new_media)

                        kvs = m['kvs']
                        for key in kvs:
                            value = kvs[key]
                            new_media.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    fileinfo.build_pages_fileinfos((entry, ))
                    fileinfo.build_archives_fileinfos((entry, ))

                tpl += ('<p>'.join(q)) + '<hr/>'
        return tpl

        # TODO:

        # Import or create categories as needed
        # Categories in export will need to have parent-child data
        # categories should have legacy identifiers where possible too

        # Import image files, assign those legacy KV identifiers
        # Modify URLs for imported images in posts
        # Make importing of image assets optional

    else:
        tpl = template(
            'ui/ui_blog_import',
            menu=generate_menu('blog_import', blog),
            # search_context=(search_context['blog'], blog),
            import_path=import_path,
            **tags.__dict__)

        return tpl
Esempio n. 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
Esempio n. 5
0
def blog_import (blog_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_publisher(user, blog)
    reason = auth.check_template_lock(blog, True)

    tags = template_tags(blog=blog,
        user=user)

    import os, settings
    import_path = os.path.join(
        settings.APPLICATION_PATH,
        "data",
        "import.json")

    tags.status = reason

    if request.method == "POST":
        from core.models import db
        tpl = ''
        with db.atomic() as txn:
            import json
            from core.utils import string_to_date

            import_path = request.forms.getunicode('import_path')
            with open(import_path, 'r', encoding='utf8') as f:
                json_data = json.load(f)

            from core.models import page_status, MediaAssociation, Category
            from core.error import PageNotChanged
            from core.libs.peewee import InterfaceError
            from core.cms import media_filetypes
            format_str = "<b>{}</b> / (<i>{}</i>)"

            # TODO: go in chunks of 50 or something?
            # allow graceful disconnection?
            for n in json_data:
                q = []
                n_id = n['id']
                q.append("Checking {}".format(n_id))
                changed = False
                found = False
                match = Page.kv_get('legacy_id', n_id)
                if match.count() > 0:
                    if match[0].object_ref.blog == blog:
                        found = True
                        q.append(match[0].key + "/" + match[0].value + " / Exists: " + format_str.format(n['title'], n_id))
                        existing_entry = Page.load(match[0].objectid)
                        update = existing_entry.kv_get('update').count()
                        # raise Exception(update)
                        q.append('{} / {}'.format(string_to_date(n['modified_date']).replace(tzinfo=None), existing_entry.modified_date
                            ))
                        if string_to_date(n['modified_date']).replace(tzinfo=None) <= existing_entry.modified_date and update == 0:
                            q.append('Existing page {} not changed.'.format(existing_entry.id))
                        else:
                            changed = True
                            q.append('Updating data for existing page {}.'.format(existing_entry.id))
                            existing_entry.title = n['title']
                            existing_entry.text = n['text']
                            existing_entry.basename = n['basename']
                            existing_entry.excerpt = n['excerpt']

                            existing_entry.created_date = string_to_date(n['created_date']).replace(tzinfo=None)
                            existing_entry.modified_date = string_to_date(n['modified_date']).replace(tzinfo=None)
                            existing_entry.publication_date = string_to_date(n['publication_date']).replace(tzinfo=None)

                            try:
                                existing_entry.save(user, False, False, 'New revision from import')
                            except PageNotChanged:
                                pass
                            except InterfaceError:
                                raise Exception("Error saving {}. Check the JSON to make sure it's valid.".format(n_id))

                            for media in existing_entry.media:
                                media.kv_del()

                            existing_entry.clear_categories()
                            existing_entry.clear_kvs()
                            existing_entry.clear_tags()
                            existing_entry.clear_media()

                            entry = existing_entry

                if found is False:
                    q.append("Creating: " + format_str.format(n['title'], n_id))
                    changed = True
                    new_entry = Page(
                        title=n['title'],
                        text=n['text'],
                        basename=n['basename'],
                        excerpt=n['excerpt'],
                        user=user,
                        blog=blog,
                        created_date=string_to_date(n['created_date']),
                        publication_date=string_to_date(n['publication_date']),
                        modified_date=string_to_date(n['modified_date']),
                    )

                    new_entry.modified_date = new_entry.publication_date

                    if n['status'] in ('Publish', 'Published', 'Live'):
                        new_entry.status = page_status.published

                    new_entry.save(user)

                    entry = new_entry

                    q.append("New ID: {}".format(entry.id))

                    # Everything from here on out is

                if changed:

                    # Register a legacy ID for the page

                    entry.kv_set("legacy_id", n["id"])
                    entry.kv_set("legacy_user", n["user_id"])

                    # Category assignments

                    categories = n['categories']
                    if categories == []:
                        saved_page_category = PageCategory.create(
                            page=entry,
                            category=blog.default_category,
                            primary=True).save()
                    else:
                        primary = True
                        for category in categories:
                            cat_exists = False

                            category_id = category['id']
                            existing_category = Category.kv_get('legacy_id', category_id)
                            if existing_category.count() > 0:
                                if existing_category[0].object_ref.blog == blog:
                                    cat_exists = True

                            if cat_exists is False:

                                q.append('Created new category {}/{}'.format(
                                    category_id, category['name']
                                    ))
                                new_category = Category.create(
                                    blog=blog,
                                    title=category['name'],
                                    parent_category=getattr(category, 'parent', None)
                                    )
                                new_category.save()

                                new_category.kv_set('legacy_id',
                                    category_id
                                    )
                            else:
                                new_category = Category.load(existing_category[0].objectid)
                                q.append('Added to existing category {}/{}'.format(
                                    new_category.id, category['name']
                                    ))

                            saved_page_category = PageCategory.create(
                                page=entry,
                                category=new_category,
                                primary=primary
                                ).save()
                            primary = False

                    # Check to make sure a default category exists for the whole blog.
                    # If not, assign one based on the lowest ID.
                    # This can always be reassigned later.

                    # Register tags

                    tags_added, tags_existing, _ = Tag.add_or_create(
                        n['tags'], page=entry)

                    q.append('Tags added: {}'.format(','.join(n.tag for n in tags_added)))
                    q.append('Tags existing: {}'.format(','.join(n.tag for n in tags_existing)))

                    # Register KVs

                    kvs = n['kvs']
                    for key in kvs:
                        if key != "":
                            value = kvs[key]
                            entry.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    # Register media

                    media = n['media']

                    for m in media:

                        if 'path' not in m:
                            continue

                        path = os.path.split(m['path'])

                        try:
                            new_media = Media.get(Media.url == m['url'])
                        except:
                            new_media = Media(
                                filename=path[1],
                                path=m['path'],
                                url=m['url'],
                                type=media_filetypes.image,
                                created_date=string_to_date(m['created_date']),
                                modified_date=string_to_date(m['modified_date']),
                                friendly_name=m['friendly_name'],
                                user=user,
                                blog=blog,
                                site=blog.site
                                )

                        # TODO: RBF
                        try:
                            new_media.save()
                        except Exception:
                            continue

                        media_association = MediaAssociation(
                            media=new_media,
                            page=entry)

                        media_association.save()

                        # Save legacy ID to KV on media

                        if 'id' in m:
                            new_media.kv_set('legacy_id', m['id'])

                        q.append('IMG: {}'.format(new_media.url))

                        # add tags for media

                        q.append('Tags: {}'.format(m['tags']))
                        new_tags = Tag.add_or_create(m['tags'], media=new_media)

                        kvs = m['kvs']
                        for key in kvs:
                            value = kvs[key]
                            new_media.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    fileinfo.build_pages_fileinfos((entry,))
                    fileinfo.build_archives_fileinfos((entry,))

                tpl += ('<p>'.join(q)) + '<hr/>'
        return tpl

        # TODO:

        # Import or create categories as needed
        # Categories in export will need to have parent-child data
        # categories should have legacy identifiers where possible too

        # Import image files, assign those legacy KV identifiers
        # Modify URLs for imported images in posts
        # Make importing of image assets optional

    else:
        tpl = template('ui/ui_blog_import',
            menu=generate_menu('blog_import', blog),
            # search_context=(search_context['blog'], blog),
            import_path=import_path,
            **tags.__dict__)

        return tpl