def create_image(files, user): """Given an uploaded file, a user, and other data, it creates an Image""" up_file = files.values()[0] check_file_size(up_file, settings.IMAGE_MAX_FILESIZE) try: image = Image.objects.filter(creator=user, is_draft=True) # Delete other drafts, if any: image.exclude(pk=image[0].pk).delete() image = image[0] except IndexError: # No drafts, create one image = Image(creator=user, is_draft=True) # Async uploads fallback to these defaults. image.title = get_draft_title(user) image.description = u'Autosaved draft.' image.locale = settings.WIKI_DEFAULT_LANGUAGE (up_file, is_animated) = _image_to_png(up_file) # Finally save the image along with uploading the file. image.file.save(up_file.name, File(up_file), save=True) (width, height) = _scale_dimensions(image.file.width, image.file.height) delete_url = reverse('gallery.delete_media', args=['image', image.id]) return { 'name': up_file.name, 'url': image.get_absolute_url(), 'thumbnail_url': image.thumbnail_url_if_set(), 'width': width, 'height': height, 'delete_url': delete_url }
def create_image(files, user): """Given an uploaded file, a user, and other data, it creates an Image""" up_file = files.values()[0] check_file_size(up_file, settings.IMAGE_MAX_FILESIZE) try: image = Image.objects.filter(creator=user, is_draft=True) # Delete other drafts, if any: image.exclude(pk=image[0].pk).delete() image = image[0] except IndexError: # No drafts, create one image = Image(creator=user, is_draft=True) # Async uploads fallback to these defaults. image.title = get_draft_title(user) image.description = u'Autosaved draft.' image.locale = settings.WIKI_DEFAULT_LANGUAGE (up_file, is_animated) = _image_to_png(up_file) # Finally save the image along with uploading the file. image.file.save(up_file.name, File(up_file), save=True) (width, height) = _scale_dimensions(image.file.width, image.file.height) delete_url = reverse('gallery.delete_media', args=['image', image.id]) return {'name': up_file.name, 'url': image.get_absolute_url(), 'thumbnail_url': image.thumbnail_url_if_set(), 'width': width, 'height': height, 'delete_url': delete_url}
def image(file_and_save=True, **kwargs): """Return a saved image.""" u = None if 'creator' not in kwargs: u = user(save=True) defaults = { 'title': 'Some title %s' % str(datetime.now()), 'description': 'Some description %s' % str(datetime.now()), 'creator': u } defaults.update(kwargs) img = Image(**defaults) if not file_and_save: return img if 'file' not in kwargs: with open('kitsune/upload/tests/media/test.jpg') as f: up_file = File(f) img.file.save(up_file.name, up_file, save=True) return img