diff user_photos/views.py @ 718:bf5340705d0c

Completed view to delete user photos. Still need to modify the admin to delete not just the model instance but the S3 bucket keys.
author Brian Neal <bgneal@gmail.com>
date Wed, 18 Sep 2013 21:34:05 -0500
parents 13a1713d05b5
children 71d17d267e27
line wrap: on
line diff
--- a/user_photos/views.py	Wed Sep 18 18:33:52 2013 -0500
+++ b/user_photos/views.py	Wed Sep 18 21:34:05 2013 -0500
@@ -10,6 +10,7 @@
 
 from user_photos.forms import UploadForm
 from user_photos.models import Photo
+from user_photos.s3 import delete_photos
 
 
 @login_required
@@ -69,6 +70,14 @@
 @login_required
 @require_POST
 def delete(request):
+    """A view function to allow a user to delete their own photos."""
+
+    ret_view, username = 'user_photos-gallery', request.user.username
+
+    if not settings.USER_PHOTOS_ENABLED:
+        messages.error(request, "This function is disabled temporarily")
+        return redirect(ret_view, username)
+
     photo_ids = []
     for photo_id in request.POST.getlist('photo_id'):
         try:
@@ -80,11 +89,14 @@
     count = 0
     if photo_ids:
         qs = Photo.objects.filter(user=request.user, pk__in=photo_ids)
-        count = qs.count()
-        qs.delete()
+        count = len(qs)
+        if count:
+            delete_photos(qs)
+            qs.delete()
 
-    if count:
-        msg = "{} photo{} deleted".format(count, '' if count == 1 else 's')
-        messages.add_message(request, messages.INFO, msg)
+    msg = "{} photo{} deleted".format(count, '' if count == 1 else 's')
+    messages.add_message(request,
+            messages.SUCCESS if count > 0 else messages.WARNING,
+            msg)
 
-    return redirect('user_photos-gallery', username=request.user.username)
+    return redirect(ret_view, username)