Mercurial > public > sg101
annotate user_photos/forms.py @ 696:b2a8fde3173a
Got the image resizing and uploading working. It needs a lot of work though.
This commit is just to capture something that works.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 08 Sep 2013 19:06:54 -0500 |
parents | 2d35e5f97a99 |
children | 67f8d49a9377 |
rev | line source |
---|---|
bgneal@695 | 1 """Forms for the user_photos application.""" |
bgneal@695 | 2 from django import forms |
bgneal@695 | 3 |
bgneal@696 | 4 from user_photos.models import Photo |
bgneal@696 | 5 from user_photos.images import process_file |
bgneal@696 | 6 |
bgneal@695 | 7 |
bgneal@695 | 8 class UploadForm(forms.Form): |
bgneal@695 | 9 image_file = forms.ImageField() |
bgneal@696 | 10 |
bgneal@696 | 11 def __init__(self, *args, **kwargs): |
bgneal@696 | 12 self.user = kwargs.pop('user') |
bgneal@696 | 13 super(UploadForm, self).__init__(*args, **kwargs) |
bgneal@696 | 14 |
bgneal@696 | 15 def save(self): |
bgneal@696 | 16 """Processes the image and creates a new Photo object, which is saved to |
bgneal@696 | 17 the database. The new Photo instance is returned. |
bgneal@696 | 18 |
bgneal@696 | 19 This function should only be called if is_valid() returns True. |
bgneal@696 | 20 |
bgneal@696 | 21 """ |
bgneal@696 | 22 url, thumb_url = process_file(self.cleaned_data['image_file'], self.user) |
bgneal@696 | 23 photo = Photo(user=self.user, url=url, thumb_url=thumb_url) |
bgneal@696 | 24 photo.save() |
bgneal@696 | 25 return photo |