Guest User

Untitled

a guest
Jul 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. def upload_path_handler(instance, filename):
  2. return filename
  3.  
  4. class SpectacleGallery(models.Model):
  5. image = models.ImageField(upload_to=upload_path_handler)
  6. def save(self, *args, **kwargs):
  7. Image.open(self.image)
  8. super(SpectacleGallery, self).save(*args, **kwargs)
  9.  
  10. IOError at /admin/index/spectacle/1/
  11. cannot identify image file
  12.  
  13. def save(self, *args, **kwargs):
  14. # set paths and additional variables
  15. self_pk = self.pk
  16. spectacle_id = self.spectacle_id
  17. spectacle_id_str = str(spectacle_id)
  18. create_gallery_spectacle_dir(spectacle_id)
  19.  
  20. new_filename = generate_image_name_hash()
  21. new_filename_main = new_filename + '.jpg'
  22. new_filename_thumb = new_filename + '_thumb.jpg'
  23. new_file_thumb_path = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_thumb
  24. new_file_thumb_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_thumb
  25. new_file_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_main
  26.  
  27. if self.image:
  28. #set new name and thum name
  29. self.image.name = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_main
  30. self.image_thumb = new_file_thumb_path
  31.  
  32. # image is in form and action is add call the "real" save() method.
  33. if self.image and not self_pk:
  34. super(SpectacleGallery, self).save(*args, **kwargs)
  35.  
  36. # image is in form and action is edit: get old image info, create variable with image field
  37. if self.image and self_pk:
  38. old_img = SpectacleGallery.objects.get(pk=self.pk)
  39. old_img_instance = old_img.image
  40.  
  41. if self.image:
  42. if self_pk:
  43. image = old_img_instance
  44. else:
  45. image = self.image
  46.  
  47. super(SpectacleGallery, self).save(*args, **kwargs) #Call the "real" save() method.
  48.  
  49. #if image in form
  50. if self.image:
  51. # open file with PIL and convert to RGB
  52. tmp_file = Image.open(self.image.path)
  53. if tmp_file.mode != 'RGB':
  54. tmp_file = tmp_file.convert('RGB')
  55.  
  56. #create and save thumbnail
  57. tmp_file.thumbnail(settings.SPECTACLE_GALLERY_THUMB_SIZE, Image.ANTIALIAS) #make thumbnail
  58. tmp_file.save(new_file_thumb_root_path, 'JPEG') #save thumbnail
  59.  
  60. # if edit delete old images
  61. if self_pk:
  62. delete_image_and_thumb(old_img.image, old_img.image_thumb)
  63. #open and resize original image
  64. image = Image.open(self.image.path)
  65. if image.mode != 'RGB':
  66. image = image.convert('RGB')
  67. image.thumbnail(settings.SPECTACLE_GALLERY_IMAGE_SIZE, Image.ANTIALIAS) #make thumbnail
  68. image.save(new_file_root_path,'JPEG', quality=100)
  69.  
  70. Image.open(self.image.path)
Add Comment
Please, Sign In to add comment