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 = _image_to_png(up_file) # Finally save the image along with uploading the file. image.file.save(os.path.splitext(up_file.name)[0] + '.png', 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 = _image_to_png(up_file) # Finally save the image along with uploading the file. image.file.save(os.path.splitext(up_file.name)[0] + '.png', 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 process_image(self, image_data): """ Takes the dictionary representing an image and saves it to an instance of the gallery.Image model. """ author_name = image_data['account_url'] if author_name: new = Image() new.title = image_data['title'] new.size = image_data['size'] new.hash = image_data['hash'] new.ext = image_data['ext'] # TODO: make sure all these methods are implemented correctly new.author = self.get_or_create_author(author_name) new.timestamp = self.parse_datetime(image_data['timestamp']) new.build_urls() new.save() else: print "Image %(hash)s didn't have an author" % image_data
def process_image(self, image_data): """ Takes the dictionary representing an image and saves it to an instance of the gallery.Image model. """ author_name = image_data['account_url'] if author_name: new = Image() new.title = image_data['title'] new.size = image_data['size'] new.hash = image_data['hash'] new.ext = image_data['ext'] # TODO: make sure all these methods are implemented correctly new.author = self.get_or_create_author(author_name) new.timestamp = self.parse_datetime(image_data['timestamp']) new.build_urls(hash) new.save() /* TO SAVE THE FILES IN DOWNLOADS FOLDER */ filename_charset = string.ascii_letters + string.digits file_save_dir = MEDIA_ROOT urllib.urlretrieve ("http://i.imgur.com/"+(new.hash),os.path.join(file_save_dir, (new.hash) + '.jpg'))