def test_find_tempfolder_create(self): from karl.models.tempfolder import TempFolder from karl.utils import find_tempfolder root = testing.DummyModel() tempfolder = find_tempfolder(root) self.failUnless(isinstance(tempfolder, TempFolder)) self.assertEqual(root['TEMP'], tempfolder)
def relocate_temp_images(doc, request): """ Finds any <img> tags in the document body which point to images in the temp folder, moves those images to the attachments folder of the document and then rewrites the img tags to point to the new location. """ attachments = doc.get_attachments() relocated_images = {} tempfolder = find_tempfolder(doc) def relocate(match): matchdict = match.groupdict() tempid = matchdict['tempid'] if tempid in relocated_images: # Already relocated this one url = relocated_images[tempid] else: # Move temp image to attachments folder image = tempfolder[tempid] del tempfolder[tempid] name = make_unique_name(attachments, image.filename) attachments[name] = image size = (int(matchdict['width']), int(matchdict['height'])) url = thumb_url(image, request, size) relocated_images[tempid] = url workflow = get_workflow(ICommunityFile, 'security', image) if workflow is not None: workflow.initialize(image) return ''.join([matchdict['pre'], url, matchdict['post'],]) doc.text = TMP_IMG_RE.sub(relocate, doc.text) tempfolder.cleanup()
def test_find_tempfolder_exists(self): from karl.utils import find_tempfolder root = testing.DummyModel() root['TEMP'] = tempfolder = testing.DummyModel() self.assertEqual(find_tempfolder(root), tempfolder)
def drawer_upload_view(context, request, check_upload_size=check_upload_size, get_image_info=get_image_info, batch_images=batch_images, ): """Drawer posts a file with the parameter name "file". """ ## XXX The rest is copied from add_file_view. A common denominator ## would be desirable. creator = authenticated_userid(request) fieldstorage = request.params.get('file') if not hasattr(fieldstorage, 'filename'): msg = 'You must select a file before clicking Upload.' return dict(error = msg) # For file objects, OSI's policy is to store the upload file's # filename as the objectid, instead of basing __name__ on the # title field). filename = basename_of_filepath(fieldstorage.filename) stream = fieldstorage.file # use parameter, as the title (or basename, if missing). title = request.params.get('title', filename) image = create_content(ICommunityFile, title=title, stream=stream, mimetype=get_upload_mimetype(fieldstorage), filename=fieldstorage.filename, creator=creator, ) # Check if it's an image. if not IImage.providedBy(image): msg = 'File %s is not an image' % filename return dict(error=msg) check_upload_size(context, image, 'file') if hasattr(context, 'get_attachments'): target_folder = context.get_attachments() if not has_permission('create', target_folder, request): msg = 'You do not have permission to upload files here.' return dict(error=msg) image.filename = filename name = make_name(target_folder, filename, raise_error=False) if not name: msg = 'The filename must not be empty' return dict(error=msg) # Is there a key in context with that filename? if name in target_folder: msg = 'Filename %s already exists in this folder' % filename return dict(error=msg) target_folder[name] = image workflow = get_workflow(ICommunityFile, 'security', context) if workflow is not None: workflow.initialize(image) # In cases where the image is to live in a piece of content which has not # yet been created (like when using an 'Add' form), the context is going # to be the eventual parent of the not yet created content, which will be # the eventual parent of the just now created image. Since we have nowhere # to put the image, we put it in a tempfolder and later move it over after # the content is created. Normal ContentAdded event handlers are not # called at this stage and workflow is not yet initialized. These will # occur when the content is moved over. else: image.modified = datetime.datetime.now() tempfolder = find_tempfolder(context) tempfolder.add_document(image) # Return info about the image uploaded return dict( upload_image_info=get_image_info(image, request, thumb_size=LARGE_THUMB_SIZE), )
def drawer_upload_view(context, request, check_upload_size=check_upload_size, get_image_info=get_image_info, batch_images=batch_images, ): """Drawer posts a file with the parameter name "file". """ ## XXX The rest is copied from add_file_view. A common denominator ## would be desirable. creator = authenticated_userid(request) fieldstorage = request.params.get('file') if not hasattr(fieldstorage, 'filename'): msg = 'You must select a file before clicking Upload.' return dict(error = msg) stream = fieldstorage.file title = fieldstorage.filename image = create_content(ICommunityFile, title=title, stream=stream, mimetype=get_upload_mimetype(fieldstorage), filename=fieldstorage.filename, creator=creator, ) check_upload_size(context, image, 'file') if hasattr(context, 'get_attachments'): target_folder = context.get_attachments() if not has_permission('create', target_folder, request): msg = 'You do not have permission to upload files here.' return dict(error=msg) # For file objects, OSI's policy is to store the upload file's # filename as the objectid, instead of basing __name__ on the # title field). filename = basename_of_filepath(fieldstorage.filename) image.filename = filename name = make_name(target_folder, filename, raise_error=False) if not name: msg = 'The filename must not be empty' return dict(error=msg) # Is there a key in context with that filename? if name in target_folder: msg = 'Filename %s already exists in this folder' % filename return dict(error=msg) target_folder[name] = image else: tempfolder = find_tempfolder(context) tempfolder.add_document(image) workflow = get_workflow(ICommunityFile, 'security', context) if workflow is not None: workflow.initialize(image) # Update the thumbnails return dict( upload_image_info=get_image_info(image, request), images_info=batch_images(context, request), )