From 3ad6651044c33c8dcf258573ef279b67d9bf53b1 Mon Sep 17 00:00:00 2001 From: Aaron Gutierrez Date: Sun, 21 Oct 2018 10:10:56 -0700 Subject: [PATCH] Rotate vertical images --- fincom/settings.py | 2 +- items/models.py | 51 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/fincom/settings.py b/fincom/settings.py index 15450aa..9ccb7f7 100644 --- a/fincom/settings.py +++ b/fincom/settings.py @@ -139,7 +139,7 @@ AUTH_PASSWORD_VALIDATORS = [ SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ['SOCIAL_AUTH_GOOGLE_OAUTH2_KEY'] SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ['SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET'] SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['andrew.cmu.edu', 'alumni.cmu.edu'] -SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'hd': 'andrew.cmu.edu' } +SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'hd': '*' } SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/items/' LOGIN_URL = '/login/google-oauth2/' diff --git a/items/models.py b/items/models.py index 71a195b..6b40a2b 100644 --- a/items/models.py +++ b/items/models.py @@ -1,15 +1,61 @@ from __future__ import unicode_literals +from __future__ import with_statement + +from datetime import datetime, date from django.contrib.auth.models import User +from django.core.files.base import ContentFile from django.core.mail import send_mail from django.template import loader +from PIL import Image from storages.backends.s3boto3 import S3Boto3Storage from stdimage.models import StdImageField -from datetime import datetime, date +from stdimage.utils import render_variations from committee.models import Committee from django.db import models +#EXIF key index for the orientation value +ORIENTATION_KEY = 274 +ROTATE_VALUES = { + 3: Image.ROTATE_180, + 6: Image.ROTATE_270, + 8: Image.ROTATE_90, +} + +# helper to fix "soft" rotated images +def resizeAndRotate(file_name, variations, storage): + rotated = False + with storage.open(file_name) as f: + try: + image = Image.open(f) + except: + return False + else: + with image: + try: + file_format = image.format + exif = image._getexif() + + if exif and ORIENTATION_KEY in exif: + orientation = exif[ORIENTATION_KEY] + + if orientation in ROTATE_VALUES: + image = image.transpose(ROTATE_VALUES[orientation]) + rotated = True + + if rotated: + with BytesIO() as file_buffer: + image.save(file_buffer, file_format) + f = ContentFile(file_buffer.getValue()) + storage.delete(file_name) + storage.save(file_name, f) + except: + return True + + render_variations(file_name, variations, storage=storage) + return False + class Item(models.Model): S3 = S3Boto3Storage() @@ -25,6 +71,7 @@ class Item(models.Model): (REJECTED, 'Rejected'), ) + desc = models.CharField(max_length=200) event = models.CharField(max_length=200) committee = models.ForeignKey(Committee) @@ -32,6 +79,7 @@ class Item(models.Model): cost = models.DecimalField(max_digits=7, decimal_places=2) date_purchased = models.DateField('date purchased') image = StdImageField(upload_to='images/%Y/%m/%d', + render_variations=resizeAndRotate, variations={'thumbnail': (600, 600)} ) @@ -40,6 +88,7 @@ class Item(models.Model): date_filed = models.DateTimeField('date filed') status = models.CharField(max_length=2, choices=STATUS) + def approved(self): return self.status == Item.PREAPPROVED