Created
February 10, 2012 12:46
-
-
Save SmileyChris/1789453 to your computer and use it in GitHub Desktop.
Playing around with creating an image with thumb field (haven't even run this code, so don't blame me if it doesn't work)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from django.db import models | |
| from easy_thumbnails.fields import ThumbnailerImageField | |
| from easy_thumbnails.files import Thumbnailer, ThumbnailerImageFieldFile | |
| import uuid | |
| def _get_file_path(instance, filename): | |
| filename = str(uuid.uuid4()) + os.splitext(filename) | |
| return '/'.join('photo', filename) | |
| class WithThumbFile(ThumbnailerImageFieldFile): | |
| @property | |
| def thumb(self): | |
| """ | |
| Return the thumbnail. | |
| """ | |
| if not self.field.thumb_options: | |
| return | |
| return SubfolderThumbnailer( | |
| file=self, | |
| name=self.name, | |
| source_storage=self.field.storage, | |
| thumbnail_storage=self.field.storage, | |
| ).get_thumbnail(self.field.thumb_options) | |
| def save(self, name, content, *args, **kwargs): | |
| """ | |
| Save the source file, and save the thumb. | |
| """ | |
| super(WithThumbFile, self).save(name, content, *args, **kwargs) | |
| # Retrieve (and therefore save) the thumbnail. | |
| self.thumb | |
| class SubfolderThumbnailer(Thumbnailer): | |
| def get_thumbnail_name(self, thumbnail_options, transparent=False): | |
| """ | |
| Get the thumbnail name by offsetting the current image path with | |
| a thumbnail subdirectory. | |
| """ | |
| source_extension = os.path.splitext(self.name)[1] | |
| # Would be nice if someone abstracted the extension generation to another | |
| # method, hint hint. | |
| if self.thumbnail_preserve_extensions == True or \ | |
| (self.thumbnail_preserve_extensions and \ | |
| source_extension.lower() in self.thumbnail_preserve_extensions): | |
| extension = source_extension | |
| elif transparent: | |
| extension = self.thumbnail_transparency_extension | |
| else: | |
| extension = self.thumbnail_extension | |
| extension = extension or 'jpg' | |
| name = self.name | |
| if extension != source_extension: | |
| name += extension | |
| base, name = os.path.split(name) | |
| return os.path.join(base, 'thumbs', name) | |
| class ImageWithThumb(ThumbnailerImageField): | |
| attr_class = WithThumbFile | |
| def __init__(self, *args, **kwargs): | |
| self.thumb_options = kwargs.pop('thumb', None) | |
| super(ImageWithThumb, self).__init__(*args, **kwargs) | |
| class Photo(models.Model): | |
| """ | |
| Access the thumbnail via photo_instance.image.thumb | |
| """ | |
| image = ImageWithThumb(upload_to=_get_file_path, thumb={'size': (800, 300)}) | |
| description = models.CharField(max_length=200, blank=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment