def handle_uploaded_photos(self, field): original, final = self.files.get(field), None if original: try: final = UploadedPictureHandler().encode_filename( original, 'travels/') return Photo.objects.create(fichier_origine=original, image=final) except IOError: self.errors[field] = ErrorList([_("Unknown type of image")]) return None
def clean_field_picture_banner(self): retour = self.files.get('field_picture_banner') if retour: try: retour = UploadedPictureHandler().encode_filename( retour, 'banners/', (400, 400)) # (!) 2 types of exceptions : IOError and KeyError # (PIL, PIL/Image.py line 1649 checks extension is ok): except (KeyError, IOError): retour = None self.errors['field_picture_banner'] =\ ErrorList([_("Unknown type of image")]) return retour
def refresh_gravatar(): print("refresh_gravatar") gravatar_url = Gravatar(self.user.email).get_image() if 'http://' in gravatar_url: gravatar_url = 'https://{}'.format(gravatar_url[7:]) response = requests.get(gravatar_url) img = Image.open(BytesIO(response.content)) # filename example: 'www.gravatar.com-avatar-f976af5e922e17a4.png' filename = '{}.{}'.format(gravatar_url[8:].replace('/', '-'), img.format.lower()) try: final = UploadedPictureHandler().generate_everything( BytesIO(response.content), 'gravatar/', thumbnail_dimensions=(60, 60), uploaded_filename=filename) with transaction.atomic(): self.gravatar = GravatarFile.objects.create( description='gravatar', filename_original=gravatar_url, file_field=final) self.save() except IOError: pass