Rotate vertical images

This commit is contained in:
2018-10-21 10:10:56 -07:00
parent 695a4b9af6
commit 3ad6651044
2 changed files with 51 additions and 2 deletions

View File

@@ -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/'

View File

@@ -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