def save(self, *a, **kw): """Attach a non-monotonic permalink to each photo, post-save.""" super(Photo, self).save(*a, **kw) h = prf() h.update(str(self.id)) h.update(str(self.photo.name)) self.permalink = h.hexdigest()[0:8] super(Photo, self).save()
def save_with(self, filelike): """Saves the model instance with a filelike object representing photograph.""" checksum = prf() while True: blob = filelike.read(1024) if not blob: break checksum.update(blob) filelike.seek(0) thumb = Image.open(filelike) width, height = thumb.size if width > height: size = 128, height / (width / 128) else: size = width / (height / 128), 128 thumb.thumbnail(size, Image.ANTIALIAS) # Create a file-like object to write thumb data (thumb data previously # created using PIL, and stored in variable 'thumb') thumb_io = StringIO() thumb.save(thumb_io, format='JPEG') # Create a new Django file-like object to be used in models as # ImageField using InMemoryUploadedFile. If you look at the source in # Django, a SimpleUploadedFile is essentially instantiated similarly # to what is shown here thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo.jpg', 'image/jpeg', thumb_io.len, None) self.thumbnail.save(checksum.hexdigest() + '_t.jpg', File(thumb_file), save=False) self.photo.save(checksum.hexdigest() + '.jpg', File(filelike), save=True)
def save_with(self, filelike): """Saves the model instance with a filelike object representing photograph.""" checksum = prf() while True: blob = filelike.read(1024) if not blob: break checksum.update(blob) filelike.seek(0) thumb = Image.open(filelike) width, height = thumb.size if width > height: size = 128, height/(width/128) else: size = width/(height/128), 128 thumb.thumbnail(size, Image.ANTIALIAS) # Create a file-like object to write thumb data (thumb data previously # created using PIL, and stored in variable 'thumb') thumb_io = StringIO() thumb.save(thumb_io, format='JPEG') # Create a new Django file-like object to be used in models as # ImageField using InMemoryUploadedFile. If you look at the source in # Django, a SimpleUploadedFile is essentially instantiated similarly # to what is shown here thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo.jpg', 'image/jpeg', thumb_io.len, None) self.thumbnail.save(checksum.hexdigest() + '_t.jpg', File(thumb_file), save=False) self.photo.save(checksum.hexdigest() + '.jpg', File(filelike), save=True)