def save(self, force_update=False, force_insert=False, thumb_size=(IMAGE_MAX_HEIGHT, IMAGE_MAX_WIDTH)): """Save the article. This will save thumbnail on disk and then save the model in database. """ self.slug = slugify(self.title) if has_changed(self, 'image') and self.image: # TODO : delete old image image = Image.open(self.image) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(thumb_size, Image.ANTIALIAS) # save the thumbnail to memory temp_handle = StringIO() image.save(temp_handle, 'png') temp_handle.seek(0) # rewind the file # save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.image.name)[-1], temp_handle.read(), content_type='image/png') self.thumbnail.save('{}.png'.format(suf.name), suf, save=False) # save the image object super().save(force_update, force_insert) else: super().save()
def save(self, *args, **kwargs): """Save tutorial instance. If tutorial's image was changed, it will update its thumbnail field resizing it. Then it will call normal saving of the model. """ is_new = self.pk is None thumb_size = (IMAGE_MAX_HEIGHT, IMAGE_MAX_WIDTH) # Save the tutorial first if it was created in order to have a pk from # the database. if is_new: super().save(*args, **kwargs) # Compute tutorial's slug from its title self.slug = slugify(self.title) # Update the thumbnail if necessary if self.image and (is_new or has_changed(self, 'image')): # TODO : delete old image image = Image.open(self.image) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(thumb_size, Image.ANTIALIAS) # Save the thumbnail to memory temp_handle = io.BytesIO() image.save(temp_handle, 'png') temp_handle.seek(0) # rewind the file # Save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.image.name)[-1], temp_handle.read(), content_type='image/png') self.thumbnail.save(u'{}.png'.format(suf.name), suf, save=False) # Save the tutorial super().save(*args, **kwargs)
def save(self, force_update=False, force_insert=False, thumb_size=(IMAGE_MAX_HEIGHT, IMAGE_MAX_WIDTH)): """Save the chapter. This will update the chapter's thumbnail if its image was changed and then save the model. """ self.slug = slugify(self.title) if has_changed(self, 'image') and self.image: # TODO : delete old image image = Image.open(self.image) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(thumb_size, Image.ANTIALIAS) # save the thumbnail to memory temp_handle = io.StringIO() image.save(temp_handle, 'png') temp_handle.seek(0) # rewind the file # save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.image.name)[-1], temp_handle.read(), content_type='image/png') self.thumbnail.save('{}.png'.format(suf.name), suf, save=False) # save the image object super().save(force_update, force_insert) else: super().save()