def create_imageattachment(files, user, obj): """ Given an uploaded file, a user and an object, it creates an ImageAttachment owned by `user` and attached to `obj`. """ up_file = files.values()[0] check_file_size(up_file, settings.IMAGE_MAX_FILESIZE) (up_file, is_animated) = _image_to_png(up_file) image = ImageAttachment(content_object=obj, creator=user) image.file.save(up_file.name, File(up_file), save=True) # Compress and generate thumbnail off thread generate_thumbnail.delay(image, 'file', 'thumbnail') if not is_animated: compress_image.delay(image, 'file') (width, height) = _scale_dimensions(image.file.width, image.file.height) # The filename may contain html in it. Escape it. name = bleach.clean(up_file.name) return {'name': name, 'url': image.file.url, 'thumbnail_url': image.thumbnail_if_set().url, 'width': width, 'height': height, 'delete_url': image.get_delete_url()}
def create_imageattachment(files, user, obj): """ Given an uploaded file, a user and an object, it creates an ImageAttachment owned by `user` and attached to `obj`. """ up_file = list(files.values())[0] check_file_size(up_file, settings.IMAGE_MAX_FILESIZE) (up_file, is_animated) = _image_to_png(up_file) image = ImageAttachment(content_object=obj, creator=user) image.file.save(up_file.name, File(up_file), save=True) # Compress and generate thumbnail off thread generate_thumbnail.delay(image, "file", "thumbnail") if not is_animated: compress_image.delay(image, "file") # Refresh because the image may have been changed by tasks. image.refresh_from_db() (width, height) = _scale_dimensions(image.file.width, image.file.height) # The filename may contain html in it. Escape it. name = escape(up_file.name) return { "name": name, "url": image.file.url, "thumbnail_url": image.thumbnail_if_set().url, "width": width, "height": height, "delete_url": image.get_delete_url(), }
def create_imageattachment(files, user, obj): """ Given an uploaded file, a user and an object, it creates an ImageAttachment owned by `user` and attached to `obj`. """ up_file = files.values()[0] check_file_size(up_file, settings.IMAGE_MAX_FILESIZE) (up_file, is_animated) = _image_to_png(up_file) image = ImageAttachment(content_object=obj, creator=user) image.file.save(up_file.name, File(up_file), save=True) # Compress and generate thumbnail off thread generate_thumbnail.delay(image, 'file', 'thumbnail') if not is_animated: compress_image.delay(image, 'file') (width, height) = _scale_dimensions(image.file.width, image.file.height) # The filename may contain html in it. Escape it. name = bleach.clean(up_file.name) return { 'name': name, 'url': image.file.url, 'thumbnail_url': image.thumbnail_if_set().url, 'width': width, 'height': height, 'delete_url': image.get_delete_url() }