annotate 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 |
rev |
line source |
bgneal@695
|
1 """Forms for the user_photos application."""
|
bgneal@695
|
2 from django import forms
|
bgneal@697
|
3 from django.conf import settings
|
bgneal@695
|
4
|
bgneal@696
|
5 from user_photos.models import Photo
|
bgneal@696
|
6 from user_photos.images import process_file
|
bgneal@697
|
7 from user_photos.s3 import S3Bucket
|
bgneal@696
|
8
|
bgneal@695
|
9
|
bgneal@695
|
10 class UploadForm(forms.Form):
|
bgneal@695
|
11 image_file = forms.ImageField()
|
bgneal@696
|
12
|
bgneal@696
|
13 def __init__(self, *args, **kwargs):
|
bgneal@696
|
14 self.user = kwargs.pop('user')
|
bgneal@696
|
15 super(UploadForm, self).__init__(*args, **kwargs)
|
bgneal@696
|
16
|
bgneal@696
|
17 def save(self):
|
bgneal@696
|
18 """Processes the image and creates a new Photo object, which is saved to
|
bgneal@696
|
19 the database. The new Photo instance is returned.
|
bgneal@696
|
20
|
bgneal@696
|
21 This function should only be called if is_valid() returns True.
|
bgneal@696
|
22
|
bgneal@696
|
23 """
|
bgneal@697
|
24 bucket = S3Bucket(settings.USER_PHOTOS_ACCESS_KEY,
|
bgneal@697
|
25 settings.USER_PHOTOS_SECRET_KEY,
|
bgneal@697
|
26 settings.USER_PHOTOS_BUCKET)
|
bgneal@697
|
27 url, thumb_url = process_file(self.cleaned_data['image_file'],
|
bgneal@697
|
28 self.user,
|
bgneal@697
|
29 bucket)
|
bgneal@696
|
30 photo = Photo(user=self.user, url=url, thumb_url=thumb_url)
|
bgneal@696
|
31 photo.save()
|
bgneal@696
|
32 return photo
|