view user_photos/forms.py @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents b2a8fde3173a
children e888d627928f
line wrap: on
line source
"""Forms for the user_photos application."""
from django import forms
from django.conf import settings

from user_photos.models import Photo
from user_photos.images import process_file
from user_photos.s3 import S3Bucket


class UploadForm(forms.Form):
    image_file = forms.ImageField()

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(UploadForm, self).__init__(*args, **kwargs)

    def save(self):
        """Processes the image and creates a new Photo object, which is saved to
        the database. The new Photo instance is returned.

        This function should only be called if is_valid() returns True.

        """
        bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY,
                          settings.USER_PHOTOS_SECRET_KEY,
                          settings.USER_PHOTOS_BUCKET)
        url, thumb_url = process_file(self.cleaned_data['image_file'],
                                      self.user,
                                      bucket)
        photo = Photo(user=self.user, url=url, thumb_url=thumb_url)
        photo.save()
        return photo