comparison user_photos/forms.py @ 964:51a2051588f5

Image uploading now expects a file. Refactor image uploading to not expect a Django UploadedFile and use a regular file instead. This will be needed for the future feature of being able to save and upload images from the Internet.
author Brian Neal <bgneal@gmail.com>
date Wed, 02 Sep 2015 20:50:08 -0500
parents b6e98717690b
children 4f265f61874b
comparison
equal deleted inserted replaced
963:4619290d171d 964:51a2051588f5
1 """Forms for the user_photos application.""" 1 """Forms for the user_photos application."""
2 import datetime 2 import datetime
3 import hashlib 3 import hashlib
4 import os.path
4 5
5 from django import forms 6 from django import forms
6 from django.conf import settings 7 from django.conf import settings
7 8
8 from core.s3 import S3Bucket 9 from core.s3 import S3Bucket
9 from core.image_uploader import upload 10 from core.image_uploader import upload
11 from core.functions import TemporaryFile
10 from core.services import get_redis_connection 12 from core.services import get_redis_connection
11 from user_photos.models import Photo 13 from user_photos.models import Photo
12 14
13 15
14 def rate_limit(key, limit, seconds): 16 def rate_limit(key, limit, seconds):
68 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY, 70 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
69 secret_key=settings.USER_PHOTOS_SECRET_KEY, 71 secret_key=settings.USER_PHOTOS_SECRET_KEY,
70 base_url=settings.USER_PHOTOS_BASE_URL, 72 base_url=settings.USER_PHOTOS_BASE_URL,
71 bucket_name=settings.USER_PHOTOS_BUCKET) 73 bucket_name=settings.USER_PHOTOS_BUCKET)
72 74
73 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') 75 # Trying to use PIL (or Pillow) on a Django UploadedFile is often
74 metadata = {'user': self.user.username, 'date': now} 76 # problematic because the file is often an in-memory file if it is under
77 # a certain size. This complicates matters and many of the operations we try
78 # to perform on it fail if this is the case. To get around these issues,
79 # we make a copy of the file on the file system and operate on the copy.
75 80
76 url, thumb_url = upload(fp=self.cleaned_data['image_file'], 81 fp = self.cleaned_data['image_file']
77 bucket=bucket, 82 ext = os.path.splitext(fp.name)[1]
78 metadata=metadata, 83
79 new_size=settings.USER_PHOTOS_MAX_SIZE, 84 # Write the UploadedFile to a temporary file on disk
80 thumb_size=settings.USER_PHOTOS_THUMB_SIZE) 85 with TemporaryFile(suffix=ext) as t:
86 for chunk in fp.chunks():
87 t.file.write(chunk)
88 t.file.close()
89
90 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
91 metadata = {'user': self.user.username, 'date': now}
92
93 url, thumb_url = upload(filename=t.filename,
94 bucket=bucket,
95 metadata=metadata,
96 new_size=settings.USER_PHOTOS_MAX_SIZE,
97 thumb_size=settings.USER_PHOTOS_THUMB_SIZE)
81 98
82 photo = Photo(user=self.user, url=url, thumb_url=thumb_url, 99 photo = Photo(user=self.user, url=url, thumb_url=thumb_url,
83 signature=signature) 100 signature=signature)
84 photo.save() 101 photo.save()
85 return photo 102 return photo