diff 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
line wrap: on
line diff
--- a/user_photos/forms.py	Mon Sep 09 20:53:08 2013 -0500
+++ b/user_photos/forms.py	Wed Sep 11 20:31:23 2013 -0500
@@ -1,10 +1,12 @@
 """Forms for the user_photos application."""
+import datetime
+
 from django import forms
 from django.conf import settings
 
+from core.s3 import S3Bucket
+from core.image_uploader import upload
 from user_photos.models import Photo
-from user_photos.images import process_file
-from user_photos.s3 import S3Bucket
 
 
 class UploadForm(forms.Form):
@@ -21,12 +23,20 @@
         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)
+        bucket = S3Bucket(access_key=settings.USER_PHOTOS_ACCESS_KEY,
+                          secret_key=settings.USER_PHOTOS_SECRET_KEY,
+                          base_url=settings.USER_PHOTOS_BASE_URL,
+                          bucket_name=settings.USER_PHOTOS_BUCKET)
+
+        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+        metadata = {'user': self.user.username, 'date': now}
+
+        url, thumb_url = upload(fp=self.cleaned_data['image_file'],
+                                bucket=bucket,
+                                metadata=metadata,
+                                new_size=settings.USER_PHOTOS_MAX_SIZE,
+                                thumb_size=settings.USER_PHOTOS_THUMB_SIZE)
+
         photo = Photo(user=self.user, url=url, thumb_url=thumb_url)
         photo.save()
         return photo