comparison user_photos/forms.py @ 700:e888d627928f

Refactored the processing of image uploads. I suspect I will use this general algorithm in other places (like POTD), so I made it reusable.
author Brian Neal <bgneal@gmail.com>
date Wed, 11 Sep 2013 20:31:23 -0500
parents 67f8d49a9377
children 094492e66eb9
comparison
equal deleted inserted replaced
699:d33bedc3be74 700:e888d627928f
1 """Forms for the user_photos application.""" 1 """Forms for the user_photos application."""
2 import datetime
3
2 from django import forms 4 from django import forms
3 from django.conf import settings 5 from django.conf import settings
4 6
7 from core.s3 import S3Bucket
8 from core.image_uploader import upload
5 from user_photos.models import Photo 9 from user_photos.models import Photo
6 from user_photos.images import process_file
7 from user_photos.s3 import S3Bucket
8 10
9 11
10 class UploadForm(forms.Form): 12 class UploadForm(forms.Form):
11 image_file = forms.ImageField() 13 image_file = forms.ImageField()
12 14
19 the database. The new Photo instance is returned. 21 the database. The new Photo instance is returned.
20 22
21 This function should only be called if is_valid() returns True. 23 This function should only be called if is_valid() returns True.
22 24
23 """ 25 """
24 bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY, 26 bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
25 settings.USER_PHOTOS_SECRET_KEY, 27 secret_key=settings.USER_PHOTOS_SECRET_KEY,
26 settings.USER_PHOTOS_BUCKET) 28 base_url=settings.USER_PHOTOS_BASE_URL,
27 url, thumb_url = process_file(self.cleaned_data['image_file'], 29 bucket_name=settings.USER_PHOTOS_BUCKET)
28 self.user, 30
29 bucket) 31 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
32 metadata = {'user': self.user.username, 'date': now}
33
34 url, thumb_url = upload(fp=self.cleaned_data['image_file'],
35 bucket=bucket,
36 metadata=metadata,
37 new_size=settings.USER_PHOTOS_MAX_SIZE,
38 thumb_size=settings.USER_PHOTOS_THUMB_SIZE)
39
30 photo = Photo(user=self.user, url=url, thumb_url=thumb_url) 40 photo = Photo(user=self.user, url=url, thumb_url=thumb_url)
31 photo.save() 41 photo.save()
32 return photo 42 return photo