Example #1
0
def page_media_upload(page_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    overwrite = []

    for n in request.files:
        x = request.files.get(n)
        media_path = _join(page.blog.path, page.blog.media_path_generated)
        file_path = _join(media_path, x.filename)
        if _exists(file_path):
            from core.error import FileExistsError
            raise FileExistsError(
                "File '{}' already exists on the server.".format(
                    utils.html_escape(x.filename)))
        else:
            Media.register_media(x.filename, file_path, user, page=page)
            if not _exists(media_path):
                makedirs(media_path)
            x.save(file_path)

    tags = template_tags(page=page)

    return template('edit/page_media_list.tpl', **tags.__dict__)
Example #2
0
def item_pre_registration(request):

  image = None
  if request.method=='POST':
    try:
     uid = request.POST.get('uniqueid')
     category = request.POST.get('category')
     description = request.POST.get('description')
     tags = request.POST.get('tags')
     title = tags
     media = request.POST.get('media1')

     new_item = PreRegisteredItem()
     new_item.unique_id = uid
     new_item.title = title
     new_item.tags = tags
     new_item.description = description
     new_item.category = category 
     new_item.owner = request.user
     new_item.save()

     photo = Media()
     photo.of_item = new_item
     photo.media_type = "PHOTO" 
     save_base64image_to_media(photo, media)
     photo.save()
     image = photo.data
     
     return HttpResponse(json.dumps({'result': 'OK', 'image':image.url}), content_type="application/json")
    except Exception as e:
     traceback.print_exc()
  return HttpResponse(json.dumps({'result': 'ERROR'}), content_type="application/json")
Example #3
0
def page_media_upload(page_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    overwrite = []

    for n in request.files:
        x = request.files.get(n)
        media_path = _join(page.blog.path, page.blog.media_path_generated)
        file_path = _join(media_path, x.filename)
        if _exists(file_path):
            from core.error import FileExistsError
            raise FileExistsError("File '{}' already exists on the server.".format(
                utils.html_escape(x.filename)))
        else:
            Media.register_media(x.filename, file_path, user, page=page)
            if not _exists(media_path):
                makedirs(media_path)
            x.save(file_path)

    tags = template_tags(page=page)

    return template('edit/page_media_list.tpl',
        **tags.__dict__)
Example #4
0
def register_media(filename, path, user, **ka):

    media = Media(
        filename=filename,
        path=path,
        type=media_filetypes.types[os.path.splitext(filename)[1][1:]],
        user=user,
        friendly_name=ka.get('friendly_name', filename))

    media.save()

    if 'page' in ka:
        page = ka['page']
        association = MediaAssociation(media=media,
                                       page=page,
                                       blog=page.blog,
                                       site=page.blog.site)

        association.save()

        media.blog = page.blog
        media.site = page.blog.site
        media.url = page.blog.url + page.blog.media_path + "/" + media.filename
        media.save()

    return media
Example #5
0
def media_edit(blog_id, media_id, status=None):
    '''
    UI for editing a given media entry
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)
    permission = auth.is_media_owner(user, media)

    from core.ui import kv
    kv_ui_data = kv.ui(media.kv_list())

    tags = template_tags(blog=blog,
         media=media,
         status=status,
         user=user,
        )
    tags.sidebar = sidebar.render_sidebar(
            panel_set='edit_media',
            status_badge=status_badge,
            kv_object='Media',
            kv_objectid=media.id,
            kv_ui=kv_ui_data)

    return media_edit_output(tags)
Example #6
0
def tag_make_for_media(media_id=None, tag=None):

    user = auth.is_logged_in(request)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    if tag == None:
        tag_name = request.forms.getunicode('tag')
    else:
        tag_name = tag

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create((tag_name,),
        media=media)

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Example #7
0
def media_search_results(request, blog_id=None, site_id=None):

    try:
        search_terms = request.query['search']
    except KeyError:
        raise KeyError('No search field in query.')

    if search_terms == "":
        raise ValueError('Search field is empty.')

    search_terms_enc = utf8_escape(search_terms)

    from core.models import Media

    # TODO: move to DB.media_search for indexing

    media_searched = (Media.select(Media.id).where(
        Media.friendly_name.contains(search_terms_enc)
        | Media.filename.contains(search_terms_enc)).order_by(
            Media.id.desc()).tuples())

    if site_id is not None:
        media_searched.select().where(Media.site == site_id)
    if blog_id is not None:
        media_searched.select().where(Media.blog == blog_id)

    return media_searched, search_terms
Example #8
0
def media_edit(blog_id, media_id, status=None):
    '''
    UI for editing a given media entry
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)
    permission = auth.is_media_owner(user, media)

    from core.ui import kv
    kv_ui_data = kv.ui(media.kv_list())

    tags = template_tags(
        blog=blog,
        media=media,
        status=status,
        user=user,
    )
    tags.sidebar = sidebar.render_sidebar(panel_set='edit_media',
                                          status_badge=status_badge,
                                          kv_object='Media',
                                          kv_objectid=media.id,
                                          kv_ui=kv_ui_data)

    return media_edit_output(tags)
Example #9
0
def kv_ui(keys):
    
    kv_ui = []
    
    for n in keys:
        if n.is_schema is True:
            if n.value_type in "Media":
                kv_ui.append(blank_item.format(
                    "media",
                    n.value))
            if n.value_type == "User":
                kv_ui.append(blank_item.format(
                    "user",
                    n.value))
                
        else:
            if n.parent.value_type == "Media":
                from core.models import Media
                m = Media.get(Media.id == n.value)
                kv_ui.append(media_item.format(
                    m.preview_url,
                    n.parent.value,
                    m.link_format,
                    m.friendly_name))

    return ''.join(kv_ui)
Example #10
0
def media_search_results(request, blog_id=None, site_id=None):

    try:
        search_terms = request.query['search']
    except KeyError:
        raise KeyError('No search field in query.')

    if search_terms == "":
        raise ValueError('Search field is empty.')

    search_terms_enc = utf8_escape(search_terms)

    from core.models import Media

    # TODO: move to DB.media_search for indexing

    media_searched = (Media.select(Media.id)
        .where(Media.friendly_name.contains(search_terms_enc) |
            Media.filename.contains(search_terms_enc))
        .order_by(Media.id.desc()).tuples())

    if site_id is not None:
        media_searched.select().where(Media.site == site_id)
    if blog_id is not None:
        media_searched.select().where(Media.blog == blog_id)

    return media_searched, search_terms
Example #11
0
def media_edit_save(blog_id, media_id):
    '''
    Save changes to a media entry.
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    friendly_name = request.forms.getunicode('media_friendly_name')

    changes = False

    if friendly_name != media.friendly_name:
        changes = True
        media.friendly_name = friendly_name

    import datetime

    if changes is True:
        media.modified_date = datetime.datetime.utcnow()
        media.save()

        status = utils.Status(
            type='success',
            message='Changes to media <b>{}</b> saved successfully.'.format(
                media.for_display)
            )
    else:

        status = utils.Status(
            type='warning',
            no_sure=True,
            message='No discernible changes submitted for media <b>{}</b>.'.format(
                media.id, media.for_display)
            )

    logger.info("Media {} edited by user {}.".format(
        media.for_log,
        user.for_log))

    from core.ui import kv
    kv_ui_data = kv.ui(media.kv_list())

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

    tags.sidebar = sidebar.render_sidebar(
        panel_set='edit_media',
        status_badge=status_badge,
        kv_object='Media',
        kv_objectid=media.id,
        kv_ui=kv_ui_data)

    return media_edit_output(tags)
Example #12
0
class Message(BaseChatModel, Media(3)):
    __tablename__ = 'message'

    text = Column(String(500),  nullable=False)
    room_id = Column(Integer, ForeignKey('room.id', ondelete='CASCADE'))
    author_id = Column(Integer, ForeignKey('user.id'))

    def get_absolute_url(self):
        pass
Example #13
0
def media_preview(media_id):
    media = Media.load(media_id)
    try:
        root = media.path.rsplit(_sep, 1)[0]
    except:
        root = ''
    preview = static_file(media.filename, root=root)

    return preview
Example #14
0
def media_preview(media_id):
    media = Media.load(media_id)
    try:
        root = media.path.rsplit(_sep, 1)[0]
    except:
        root = ''
    preview = static_file(media.filename, root=root)

    return preview
Example #15
0
class Room(BaseChatModel, Media(1)):
    __tablename__ = 'room'

    messages = relationship('Message', backref='room', lazy='dynamic',  passive_deletes=True)

    author_id = Column(Integer, ForeignKey('user.id'))
    users = relationship('User', secondary=user_room, backref=backref('rooms', lazy='dynamic'))

    def get_absolute_url(self):
        pass
Example #16
0
 def lookup_tv(self, episode_info):
     try:
         episode_data = re.match(TITLE_PARSER, episode_info).groups()
     except AttributeError:
         pub.sendMessage('STD_OUT', 'TV Show titles must match SHOW SXXXEXXX')
         return False
     else:
         if not series_in_db(episode_data[0]):
             lookup_series(episode_data[0])
         if self.series:
             t = TVRage()
             try:
                 episode = t.lookup(**episode_data)
                 m = Media()
                 m.fromAPIMedia(episode)
                 return m
             except APIError:
                 pub.sendMessage('STD_OUT', 
                                 'NOT FOUND: %s S%sE%s' % **episode_data)
                 return False
Example #17
0
def item_registration(request):

  if request.method=='POST':
    try:
     uid = request.POST.get('uniqueid')
     title = request.POST.get('title')
     category = request.POST.get('category')
     description = request.POST.get('description')
     tags = request.POST.get('tags')
     location = request.POST.get('location')
     photos = json.loads(request.POST['media'])
     new_item = Item()
     new_item.title = title
     new_item.unique_id = uid
     new_item.tags = tags
     new_item.description = description
     new_item.location = request.POST.get('location')
     new_item.category = category
     new_item.date_field = datetime.datetime.now().strftime("%Y-%m-%d")
     new_item.time_field = datetime.datetime.now().strftime("%H:%M:%S") 
     new_item.found_by_user = request.user
     new_item.save()

     for media in photos:
       photo = Media()
       photo.of_item = new_item
       photo.media_type = "PHOTO" 
       save_base64image_to_media(photo, media)
       photo.save()

     call_command('update_index')
     
     return HttpResponse(json.dumps({'result': 'OK', 'pk':new_item.pk}), content_type="application/json")
    except Exception as e:
     traceback.print_exc()
     return HttpResponse(json.dumps({'result': 'ERROR'}), content_type="application/json")
  context = RequestContext(request,
                           {'request': request,
                            'user': request.user
                            })
  return render_to_response('public/registerfounditem.html', context_instance=context)
Example #18
0
def media_edit_save(blog_id, media_id):
    '''
    Save changes to a media entry.
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    friendly_name = request.forms.getunicode('media_friendly_name')

    changes = False

    if friendly_name != media.friendly_name:
        changes = True
        media.friendly_name = friendly_name

    import datetime

    if changes is True:
        media.modified_date = datetime.datetime.utcnow()
        media.save()

        status = utils.Status(
            type='success',
            message='Changes to media <b>{}</b> saved successfully.'.format(
                media.for_display))
    else:

        status = utils.Status(
            type='warning',
            no_sure=True,
            message='No discernible changes submitted for media <b>{}</b>.'.
            format(media.id, media.for_display))

    logger.info("Media {} edited by user {}.".format(media.for_log,
                                                     user.for_log))

    from core.ui import kv
    kv_ui_data = kv.ui(media.kv_list())

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

    tags.sidebar = sidebar.render_sidebar(panel_set='edit_media',
                                          status_badge=status_badge,
                                          kv_object='Media',
                                          kv_objectid=media.id,
                                          kv_ui=kv_ui_data)

    return media_edit_output(tags)
Example #19
0
def media_pages(blog_id, media_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)

    return listing(request, media,
                   media.pages.order_by(Page.publication_date.desc()),
                   'blog', 'blog_media_pages',
                   user=user,
                   search_ui='blog_media_pages',
                   search_context=media_search_results,
                   tags_data={'blog':blog}
                   )
Example #20
0
def page_media_delete(page_id, media_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id)
    media_reference = MediaAssociation.get(MediaAssociation.page == page,
                                           MediaAssociation.media == media)
    media_reference.delete_instance(recursive=True, delete_nullable=True)

    tags = template_tags(page=page)

    return template('edit/page_media_list.tpl', **tags.__dict__)
Example #21
0
def page_media_add(page_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media_list = Media.select().where(Media.blog == page.blog)

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

    return template('modal/modal_images.tpl',
                    media_list=media_list,
                    title="Select media",
                    buttons='',
                    **tags.__dict__)
Example #22
0
def media_pages(blog_id, media_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)

    return listing(request,
                   media,
                   media.pages.order_by(Page.publication_date.desc()),
                   'blog',
                   'blog_media_pages',
                   user=user,
                   search_ui='blog_media_pages',
                   search_context=media_search_results,
                   tags_data={'blog': blog})
Example #23
0
 def generate_xml(self, media_file_URI):
     # TODO: MASSIVE REFACTORING IN OUTPUT!!!
     try:
         media = list(Media.select(Media.q.file_URI==media_file_URI))[0]
     except IndexError:
         msg = '%s is not found' % media_file_URI
         pub.sendMessage('STD_OUT', msg)
     else:
         self.generator_type = get_setting('output_type')
         try:
             generator = eval(self.generator_type)(media)
         except NameError:
             msg = '%s generator not available.' % generator_type
             pub.sendMessage('STD_OUT', msg)
         else:
             self.output.append(generator)
Example #24
0
def page_media_add(page_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media_list = Media.select().where(
        Media.blog == page.blog)

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

    return template('modal/modal_images.tpl',
        media_list=media_list,
        title="Select media",
        buttons='',
        **tags.__dict__)
Example #25
0
def page_add_media_with_template(page_id, media_id, template_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id, page.blog)

    media_template = Template.get(Template.id == template_id)

    from core.template import tpl
    generated_template = tpl(media_template.body, blog=page.blog, media=media)

    if media not in page.media:
        media.associate(page)

    return generated_template
Example #26
0
def page_media_delete(page_id, media_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id)
    media_reference = MediaAssociation.get(
        MediaAssociation.page == page,
        MediaAssociation.media == media)
    media_reference.delete_instance(recursive=True,
        delete_nullable=True)

    tags = template_tags(page=page)

    return template('edit/page_media_list.tpl',
        **tags.__dict__)
Example #27
0
class User(SimpleSerializer, BaseChatModel, Media(1)):
    __tablename__ = 'user'
    serialize_fields = ['username', 'about_me', 'last_activity']

    password = Column(String(66), nullable=False)
    username = Column(String(50), nullable=False)
    about_me = Column(String(124))
    last_activity = Column(DateTime, default=datetime.utcnow)
    messages = relationship('Message', backref='author', lazy='dynamic')
    created_rooms = relationship('Room', backref='author', lazy='dynamic')

    is_staff = Column(Boolean, default=False)
    is_superuser = Column(Boolean, default=False)

    def get_absolute_url(self):
        pass

    def set_password(self, password):
        pass
Example #28
0
def page_add_media_with_template(page_id, media_id, template_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id, page.blog)

    media_template = Template.get(
        Template.id == template_id)

    from core.template import tpl
    generated_template = tpl(media_template.body,
        blog=page.blog,
        media=media)

    if media not in page.media:
        media.associate(page)

    return generated_template
Example #29
0
def page_get_media_templates(page_id, media_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id, page.blog)

    media_templates = Template.select().where(
        Template.blog == page.blog,
        Template.template_type == template_type.media).order_by(Template.title)

    buttons = media_buttons.format('onclick="add_template();"', 'Apply')

    tpl = template('modal/modal_image_templates',
                   media=media,
                   templates=media_templates,
                   buttons=buttons,
                   title='Choose a template for {}'.format(media.for_log))

    return tpl
Example #30
0
def item_registration(request):

    if request.method == 'POST':
        try:
            uid = request.POST.get('uniqueid')
            title = request.POST.get('title')
            category = request.POST.get('category')
            description = request.POST.get('description')
            tags = request.POST.get('tags')
            location = request.POST.get('location')
            photos = json.loads(request.POST['media'])
            new_item = Item()
            new_item.title = title
            new_item.unique_id = uid
            new_item.tags = tags
            new_item.description = description
            new_item.location = request.POST.get('location')
            new_item.category = category
            new_item.date_field = datetime.datetime.now().strftime("%Y-%m-%d")
            new_item.time_field = datetime.datetime.now().strftime("%H:%M:%S")
            new_item.found_by_user = request.user
            new_item.save()

            for media in photos:
                photo = Media()
                photo.of_item = new_item
                photo.media_type = "PHOTO"
                save_base64image_to_media(photo, media)
                photo.save()

            call_command('update_index')

            return HttpResponse(json.dumps({
                'result': 'OK',
                'pk': new_item.pk
            }),
                                content_type="application/json")
        except Exception as e:
            traceback.print_exc()
            return HttpResponse(json.dumps({'result': 'ERROR'}),
                                content_type="application/json")
    context = RequestContext(request, {
        'request': request,
        'user': request.user
    })
    return render_to_response('public/registerfounditem.html',
                              context_instance=context)
Example #31
0
 def rewind(self):
     try:
         media = list(Media.select())
     except SQLObjectNotFound:
         msg = 'No media found with which to rewind'
         log.info(msg)
         sys.exit(msg)
     else:                
         for medium in media:
             if medium.file_URI:                    
                 if medium.original_file_URI:
                     log.debug('Moving: %s to %s' % (medium.file_URI, 
                                                 medium.original_file_URI))
                     shutil.move(medium.file_URI, medium.original_file_URI)
                 else:
                     log.debug('Original file location does not exist')
                     source_path = data.get_setting('source_path')
                     media_directory = medium.media_type
                     try:
                         log.debug("Franchise: %s" % medium.franchise.name)
                         new_title = medium.franchise.name
                     except SQLObjectNotFound:
                         log.debug('No franchise: %s' % medium.title)                            
                         new_title = medium.title
                     if medium.media_type == data.media_types[data.TV]:
                         filename = '%s S%sE%s.%s' % (new_title, 
                                                     medium.season_number,
                                                     medium.episode_number,
                                                     medium.codec)
                     else:
                         filename = '%s.%s' % (new_title, medium.codec)
                     dest = fjoin(source_path, media_directory, filename)
                     log.debug('Moving: %s to %s' % (medium.file_URI, dest))
                     shutil.move(medium.file_URI, dest)
                     medium.file_URI = dest
             else:
                 msg = "%s can't be rewound." % medium.title
                 log.error(msg)
                 pub.sendMessage('STD_OUT', msg=msg)
Example #32
0
def tag_make_for_media(media_id=None, tag=None):

    user = auth.is_logged_in(request)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    if tag == None:
        tag_name = request.forms.getunicode('tag')
    else:
        tag_name = tag

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create((tag_name, ), media=media)

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Example #33
0
def page_get_media_templates(page_id, media_id):

    user = auth.is_logged_in(request)
    page = Page.load(page_id)
    permission = auth.is_page_editor(user, page)

    media = Media.load(media_id, page.blog)

    media_templates = Template.select().where(
        Template.blog == page.blog,
        Template.template_type == template_type.media).order_by(Template.title)

    buttons = media_buttons.format(
        'onclick="add_template();"',
        'Apply')

    tpl = template('modal/modal_image_templates',
        media=media,
        templates=media_templates,
        buttons=buttons,
        title='Choose a template for {}'.format(
            media.for_log))

    return tpl
Example #34
0
def item_pre_registration(request):

    image = None
    if request.method == 'POST':
        try:
            uid = request.POST.get('uniqueid')
            category = request.POST.get('category')
            description = request.POST.get('description')
            tags = request.POST.get('tags')
            title = tags
            media = request.POST.get('media1')

            new_item = PreRegisteredItem()
            new_item.unique_id = uid
            new_item.title = title
            new_item.tags = tags
            new_item.description = description
            new_item.category = category
            new_item.owner = request.user
            new_item.save()

            photo = Media()
            photo.of_item = new_item
            photo.media_type = "PHOTO"
            save_base64image_to_media(photo, media)
            photo.save()
            image = photo.data

            return HttpResponse(json.dumps({
                'result': 'OK',
                'image': image.url
            }),
                                content_type="application/json")
        except Exception as e:
            traceback.print_exc()
    return HttpResponse(json.dumps({'result': 'ERROR'}),
                        content_type="application/json")
Example #35
0
def media_delete(blog_id, media_id, confirm='N'):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)
    permission = auth.is_media_owner(user, media)

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

    # report_txt = []

    from core.utils import Status

    if request.forms.getunicode('confirm') == user.logout_nonce:

        from os import remove

        try:
            remove(media.path)
        except:
            pass

        media.delete_instance(recursive=True, delete_nullable=True)

        confirmed = Struct()
        confirmed.message = 'Media {} successfully deleted'.format(
            media.for_log)
        confirmed.url = '{}/blog/{}/media'.format(BASE_URL, blog.id)
        confirmed.action = 'Return to the media listing'

        tags.status = Status(type='success',
                             message=confirmed.message,
                             action=confirmed.action,
                             url=confirmed.url,
                             close=False)

    else:
        s1 = (
            'You are about to delete media object <b>{}</b> from blog <b>{}</b>.'
            .format(media.for_display, blog.for_display))

        #         used_in = []
        #
        #         for n in media.pages:
        #             used_in.append("<li>{}</li>".format(n.for_display))

        media_page_count = media.pages.count()

        if media_page_count > 0:

            s2 = '''
<p><b>There are still <a target="_blank" href="{}/blog/{}/media/{}/pages">{} pages</a> associated with this tag.</b></p>
<p>Deleting the object will remove it from these pages as well.</p>
<p>Any references to these images in text will show as broken.</p>
'''.format(BASE_URL, blog.id, media.id, media_page_count)

        else:
            s2 = '''
<p>This media object is not currently used in any pages.</p>
<p>However, if it is linked directly in a page without a media reference, any such links will break.</p>
'''

        yes = {
            'label': 'Yes, delete this media',
            'id': 'delete',
            'name': 'confirm',
            'value': user.logout_nonce
        }
        no = {
            'label': 'No, return to media properties',
            'url': '../{}/edit'.format(media.id)
        }

        tags.status = Status(type='warning',
                             close=False,
                             message=s1 + '<hr>' + s2,
                             yes=yes,
                             no=no)

    tags.icons = icons

    return report(tags, 'blog_delete_media', media)
Example #36
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
Example #37
0
 def post(self, request):
     media = Media(file=request.data['file'])
     media.save()
     data = Serializer(media, model=Media).data
     return Response(data, status=status.HTTP_200_OK)
Example #38
0
def media_delete(blog_id, media_id, confirm='N'):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    is_member = auth.is_blog_member(user, blog)
    media = Media.load(media_id, blog)
    permission = auth.is_media_owner(user, media)

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

    # report_txt = []

    from core.utils import Status

    if request.forms.getunicode('confirm') == user.logout_nonce:

        from os import remove

        try:
            remove(media.path)
        except:
            pass

        media.delete_instance(recursive=True,
            delete_nullable=True)

        confirmed = Struct()
        confirmed.message = 'Media {} successfully deleted'.format(
            media.for_log)
        confirmed.url = '{}/blog/{}/media'.format(BASE_URL, blog.id)
        confirmed.action = 'Return to the media listing'

        tags.status = Status(
            type='success',
            message=confirmed.message,
            action=confirmed.action,
            url=confirmed.url,
            close=False)

    else:
        s1 = ('You are about to delete media object <b>{}</b> from blog <b>{}</b>.'.format(
            media.for_display,
            blog.for_display))

#         used_in = []
#
#         for n in media.pages:
#             used_in.append("<li>{}</li>".format(n.for_display))

        media_page_count = media.pages.count()

        if media_page_count > 0:

            s2 = '''
<p><b>There are still <a target="_blank" href="{}/blog/{}/media/{}/pages">{} pages</a> associated with this tag.</b></p>
<p>Deleting the object will remove it from these pages as well.</p>
<p>Any references to these images in text will show as broken.</p>
'''.format(BASE_URL, blog.id, media.id, media_page_count)

        else:
            s2 = '''
<p>This media object is not currently used in any pages.</p>
<p>However, if it is linked directly in a page without a media reference, any such links will break.</p>
'''

        yes = {
            'label':'Yes, delete this media',
            'id':'delete',
            'name':'confirm',
            'value':user.logout_nonce
            }
        no = {
            'label':'No, return to media properties',
            'url':'../{}/edit'.format(media.id)
            }

        tags.status = Status(
            type='warning',
            close=False,
            message=s1 + '<hr>' + s2,
            yes=yes,
            no=no
            )

    tags.icons = icons

    return report(tags, 'blog_delete_media', media)
Example #39
0
	def _create_items(self):

		call_command('migrate', 'auth')
		sys.stdout.write("\n==========Auth App Migrated===========\n")

		call_command('migrate')
		sys.stdout.write("\n==========Other Apps Migrated===========\n")

		call_command('syncdb', interactive=True)

		Item.objects.all().delete()
		CustomUser.objects.all().delete()
		CustomUser.objects.create_superuser(username='******', password='******', email='*****@*****.**')
		
		user1 = CustomUser()
		user1.username = "******"
		user1.first_name = "Nikolaus"
		user1.last_name = "Mickaelson"
		user1.email = "*****@*****.**"
		user1.prefered_way_of_contact = "IBF"
		user1.phone_number = "12345673"
		user1.set_password('nick')
		user1.save()

		user2 = CustomUser()
		user2.username = "******"
		user2.first_name = "Mark"
		user2.last_name = "Johnson"
		user2.email = "*****@*****.**"
		user2.prefered_way_of_contact = "PHONE"
		user2.phone_number = "122456141"
		user2.set_password("mark")
		user2.save()

		pitem = PreRegisteredItem()
		pitem.unique_id = ''
		pitem.title = "Green Adidas Bag "
		pitem.tags = "Bag"
		pitem.description = 'Green Bag lost near Southampton'
		pitem.location = "Southampton"
		pitem.category = "Bag"
		pitem.owner = CustomUser.objects.filter(username='******')[0]
		pitem.lost = True
		pitem.save()

		photo = Media()
		photo.of_item = pitem
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://www.fashionvortex.com/image/cache/data/Medici/MF-2475-Gr-a-600x600.jpg')
		photo.save()

		tphone = Item()
		tphone.unique_id = '123456789'
		tphone.title = "Black Samsung Galaxy S6 34GB"
		tphone.tags = "Black Samsung Galaxy S6 34GB"
		tphone.description = 'Black Samsung Galaxy S6 found in Stile'
		tphone.location = "Southampton"
		tphone.category = "Electronics"
		tphone.date_field = "2015-09-15"
		tphone.time_field = "14:33::22"
		tphone.found_by_user = user1
		tphone.save()

		tbag = Item()
		tbag.description = 'Green bag found on the poll edge at "Summer Time"'
		tbag.title = "Bag Green"
		tbag.tags = "Bag Green"
		tbag.location = "london"
		tbag.category = "Bag"
		tbag.date_field = "2016-09-09"
		tbag.time_field = "10:33::22"
		tbag.found_by_user = user1
		tbag.save()

		photo = Media()
		photo.of_item = tbag
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://www.suggestcamera.com/wp-content/uploads/2015/08/81K-jtyW82L._SL1500_.jpg')
		photo.save()

		tbag = Item()
		tbag.description = 'Green bag found on the poll edge at "Summer Time"'
		tbag.title = "Big Bag"
		tbag.tags = "Bag"
		tbag.location = "london"
		tbag.category = "Bag"
		tbag.date_field = "2016-09-09"
		tbag.time_field = "10:33::22"
		tbag.found_by_user = user1
		tbag.save()

		photo = Media()
		photo.of_item = tbag
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://i.dailymail.co.uk/i/pix/2013/11/13/article-2505060-0B22502B000005DC-342_634x422.jpg')
		photo.save()

		tLeptop = Item()
		tLeptop.unique_id = '098765432'
		tLeptop.description = '15 inch Dell found in Winchester"'
		tLeptop.title = "Dell Leptop Inspiron"
		tLeptop.tags = "Leptop Dell Black"
		tLeptop.location = "london"
		tLeptop.category = "Electronics"
		tLeptop.date_field = "2015-09-18"
		tLeptop.time_field = "10:33::22"
		tLeptop.found_by_user = user1
		tLeptop.save()

		tLaptop = Item()
		tLaptop.unique_id = '123456788'
		tLaptop.description = 'Apple MacBook 15" found at Hartley Library'
		tLaptop.title = "Apple MacBook"
		tLaptop.tags = "Apple MacBook"
		tLaptop.location = "Southampton"
		tLaptop.category = "Electronics"
		tLaptop.date_field = "2015-11-16"
		tLaptop.time_field = "22:35::22"
		tLaptop.found_by_user = user1
		tLaptop.save()

		photo = Media()
		photo.of_item = tLaptop
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://static.trustedreviews.com/94/9736c1/5242/15168-crw398s.jpg')
		photo.save()

		tIDCard = Item()
		tIDCard.unique_id = '123459876'
		tIDCard.description = 'Passport found outside Sprinkles'
		tIDCard.title = "UK EU e-passport"
		tIDCard.tags = "Passport UK EU e-passport"
		tIDCard.location = "Southampton"
		tIDCard.category = "ID/Cards"
		tIDCard.date_field = "2015-07-23"
		tIDCard.time_field = "12:07::22"
		tIDCard.found_by_user = user1
		tIDCard.save()

		photo = Media()
		photo.of_item = tIDCard
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://i.telegraph.co.uk/multimedia/archive/01595/mp-passport-pa_1595880b.jpg')
		photo.save()

		tBook = Item()
		tBook.unique_id = '121212123'
		tBook.description = 'Dan Brown The Lost Symbol paperback edition'
		tBook.title = "The Lost Symbol Paperback"
		tBook.tags = "Dan Brown The Lost Symbol Paperback "
		tBook.location = "Bournemouth"
		tBook.category = "Books"
		tBook.date_field = "2015-09-30"
		tBook.time_field = "17:53:28"
		tBook.found_by_user = user2
		tBook.save()

		photo = Media()
		photo.of_item = tBook
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://thumbs1.ebaystatic.com/d/l225/m/mIuB9Oannj3xR0YhYCIiEZg.jpg')
		photo.save()

		tScarf = Item()
		tScarf.unique_id = '666777888'
		tScarf.description = 'Grey Scarf with Dark Grey Stripes'
		tScarf.title = "Scarf"
		tScarf.tags = "Scarf Grey Dark Grey Stripes "
		tScarf.location = "Surrey"
		tScarf.category = "Clothes"
		tScarf.date_field = "2015-10-28"
		tScarf.time_field = "13:53:28"
		tScarf.found_by_user = user2
		tScarf.save()

		photo = Media()
		photo.of_item = tScarf
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://assets3.howtospendit.ft-static.com/images/52/46/d7/5246d742-1619-46b4-83c8-9e9d726203da_three_eighty.png')
		photo.save()

		tNecklace = Item()
		tNecklace.unique_id = '898998989'
		tNecklace.title = 'Black Leather necklace'
		tNecklace.tags = 'Black Leather necklace'
		tNecklace.description = "leather necklace black men unisex"
		tNecklace.location = "Glasgow"
		tNecklace.category = "Accessories"
		tNecklace.date_field = "2015-11-28"
		tNecklace.time_field = "13:27:28"
		tNecklace.found_by_user = user2
		tNecklace.save()

		photo = Media()
		photo.of_item = tNecklace
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://cdn.notonthehighstreet.com/system/product_images/images/001/615/301/original_mens-leather-necklace.jpg')
		photo.save()

		tHobbit = Item()
		tHobbit.unique_id = '454647489'
		tHobbit.title = 'J R R Tolkien -'
		tHobbit.tags = 'J R R Tolkien - The Hobbit Hard Cover'
		tHobbit.description = "tolkien hobbit the hobbit hardcover"
		tHobbit.location = "Eastleigh"
		tHobbit.category = "Books"
		tHobbit.date_field = "2015-10-30"
		tHobbit.time_field = "10:41:28"
		tHobbit.found_by_user = user2
		tHobbit.save()

		photo = Media()
		photo.of_item = tHobbit
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'https://i.ytimg.com/vi/X75pnPtqhvE/maxresdefault.jpg')
		photo.save()

		tPlayer = Item()
		tPlayer.unique_id = '145897123'
		tPlayer.title = 'Sony Walkman MP4 Player Black'
		tPlayer.tags = 'Sony Walkman MP4 Player Black'
		tPlayer.description = "sony walkman mp4 player mp3 black "
		tPlayer.location = "London"
		tPlayer.category = "Electronics"
		tPlayer.date_field = "2015-10-30"
		tPlayer.time_field = "10:41:28"
		tPlayer.found_by_user = user2
		tPlayer.save()

		photo = Media()
		photo.of_item = tPlayer
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'https://i.ytimg.com/vi/PI_nQ3MSSHI/maxresdefault.jpg')
		photo.save()

		tDog = Item()
		tDog.unique_id = '321654987'
		tDog.title = 'Chihuahua'
		tDog.tags = 'Lost Chihuahua found on Portswood Road'
		tDog.description = "chihuahua dog portswood southampton lost "
		tDog.location = "Southampton"
		tDog.category = "Animal"
		tDog.date_field = "2015-11-17"
		tDog.time_field = "22:41:28"
		tDog.found_by_user = user2
		tDog.save()

		photo = Media()
		photo.of_item = tDog
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'https://canophilia.files.wordpress.com/2014/04/chihuahua_4.jpg')
		photo.save()

		tHobbit = Item()
		tHobbit.unique_id = '125678991'
		tHobbit.title = 'Adele - Rolling in the Deep'
		tHobbit.tags = 'Adele - Rolling in the Deep CD Album'
		tHobbit.description = "adele rolling in the deep cd album"
		tHobbit.location = "Manchester"
		tHobbit.category = "Other"
		tHobbit.date_field = "2015-09-27"
		tHobbit.time_field = "13:44:28"
		tHobbit.found_by_user = user2
		tHobbit.save()

		photo = Media()
		photo.of_item = tHobbit
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'http://thumbs2.ebaystatic.com/d/l225/m/mQTzqU9kSL8uIcBHIkfwOqA.jpg')
		photo.save()

		tMug = Item()
		tMug.unique_id = '123654897'
		tMug.description = 'Found this mug at the Solent Library, 2nd Level'
		tMug.title = "Mug"
		tMug.tags = "mug white solent southampton"
		tMug.location = "Southampton"
		tMug.category = "Other"
		tMug.date_field = "2015-10-06"
		tMug.time_field = "09:13:28"
		tMug.found_by_user = user2
		tMug.save()

		photo = Media()
		photo.of_item = tMug
		photo.media_type = "PHOTO" 
		save_url_to_image(photo, 'https://s-media-cache-ak0.pinimg.com/736x/7c/01/a9/7c01a9440c8e8afde4b11ab4acbfcd3d.jpg')
		photo.save()

		sys.stdout.write("\n==========Database Re-populated===========\n")

		call_command('rebuild_index')
Example #40
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