diff user_photos/views.py @ 710:13a1713d05b5

Work in progress on adding ability to delete user photos.
author Brian Neal <bgneal@gmail.com>
date Tue, 17 Sep 2013 17:54:21 -0500
parents 809d27b385f2
children bf5340705d0c
line wrap: on
line diff
--- a/user_photos/views.py	Sun Sep 15 17:00:52 2013 -0500
+++ b/user_photos/views.py	Tue Sep 17 17:54:21 2013 -0500
@@ -4,7 +4,9 @@
 from django.contrib.auth.decorators import login_required
 from django.shortcuts import render, redirect, get_object_or_404
 from django.views.generic import ListView
+from django.views.decorators.http import require_POST
 from django.utils.decorators import method_decorator
+from django.contrib import messages
 
 from user_photos.forms import UploadForm
 from user_photos.models import Photo
@@ -62,3 +64,27 @@
     @method_decorator(login_required)
     def dispatch(self, *args, **kwargs):
         return super(GalleryView, self).dispatch(*args, **kwargs)
+
+
+@login_required
+@require_POST
+def delete(request):
+    photo_ids = []
+    for photo_id in request.POST.getlist('photo_id'):
+        try:
+            n = int(photo_id)
+        except ValueError:
+            continue
+        photo_ids.append(n)
+
+    count = 0
+    if photo_ids:
+        qs = Photo.objects.filter(user=request.user, pk__in=photo_ids)
+        count = qs.count()
+        qs.delete()
+
+    if count:
+        msg = "{} photo{} deleted".format(count, '' if count == 1 else 's')
+        messages.add_message(request, messages.INFO, msg)
+
+    return redirect('user_photos-gallery', username=request.user.username)