bgneal@695: """Forms for the user_photos application.""" bgneal@695: from django import forms bgneal@697: from django.conf import settings bgneal@695: bgneal@696: from user_photos.models import Photo bgneal@696: from user_photos.images import process_file bgneal@697: from user_photos.s3 import S3Bucket bgneal@696: bgneal@695: bgneal@695: class UploadForm(forms.Form): bgneal@695: image_file = forms.ImageField() bgneal@696: bgneal@696: def __init__(self, *args, **kwargs): bgneal@696: self.user = kwargs.pop('user') bgneal@696: super(UploadForm, self).__init__(*args, **kwargs) bgneal@696: bgneal@696: def save(self): bgneal@696: """Processes the image and creates a new Photo object, which is saved to bgneal@696: the database. The new Photo instance is returned. bgneal@696: bgneal@696: This function should only be called if is_valid() returns True. bgneal@696: bgneal@696: """ bgneal@697: bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY, bgneal@697: settings.USER_PHOTOS_SECRET_KEY, bgneal@697: settings.USER_PHOTOS_BUCKET) bgneal@697: url, thumb_url = process_file(self.cleaned_data['image_file'], bgneal@697: self.user, bgneal@697: bucket) bgneal@696: photo = Photo(user=self.user, url=url, thumb_url=thumb_url) bgneal@696: photo.save() bgneal@696: return photo