def store_attachments(attachments_folder, params, creator): """Given some request data, pick apart and store attachments""" # Get the attachments out of the form data. We do iteritems # becauser there might be multiple with the name prefixed by # attachment. new_attachments = [] for key, value in params.iteritems(): if key.startswith('attachment') and value != '': new_attachments.append(value) # Iterate through the new attachments and create content to store in # the attachments folder. for attachment in new_attachments: filename = make_unique_name(attachments_folder, basename_of_filepath(attachment.filename)) attachments_folder[filename] = obj = create_content( ICommunityFile, title = filename, stream = attachment.file, mimetype = attachment.type, filename = filename, creator = creator, ) check_upload_size(attachments_folder, obj, 'attachment')
def handle_submit(self, converted): context = self.context request = self.request workflow = self.workflow # *will be* modified event objectEventNotify(ObjectWillBeModifiedEvent(context)) if workflow is not None: if 'security_state' in converted: workflow.transition_to_state(context, request, converted['security_state']) context.title = converted['title'] f = converted['file'] if f.filename: context.upload(f.file) context.mimetype = get_mimetype(f.mimetype, f.filename) context.filename = f.filename check_upload_size(context, context, 'file') else: meta = f.metadata if meta.get('remove'): raise ValidationError(file='Must supply a file') # Tags, attachments, alerts set_tags(context, request, converted['tags']) # modified context.modified_by = authenticated_userid(request) objectEventNotify(ObjectModifiedEvent(context)) self.filestore.clear() location = model_url(context, request, query={'status_message':'File changed'}) return HTTPFound(location=location)
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), )