changeset 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 a6c664e04649
children f68694669023
files sg101/templates/user_photos/gallery.html user_photos/urls.py user_photos/views.py
diffstat 3 files changed, 58 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/sg101/templates/user_photos/gallery.html	Sun Sep 15 17:00:52 2013 -0500
+++ b/sg101/templates/user_photos/gallery.html	Tue Sep 17 17:54:21 2013 -0500
@@ -6,6 +6,12 @@
 <h2>Photo Gallery for {{ gallery_owner.username }}</h2>
 
 {% if user == gallery_owner %}
+{% if messages %}
+   <ul class="user-messages">
+    {% for message in messages %}
+       <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
+    {% endfor %}
+{% endif %}
 <p>
 You have uploaded {{ paginator.count }} photo{{ paginator.count|pluralize }}.
 Would you like to <a href="{% url 'user_photos-upload' %}">upload a photo</a>?
@@ -17,13 +23,31 @@
 {% endif %}
 
 {% if photos %}
-   {% for photo in photos %}
-      <div style="padding: 1em; display: inline;">
-         <a href="{{ photo.get_absolute_url }}">
-            <img src="{{ photo.thumb_url }}" alt="thumbnail" 
-               title="{{ photo.upload_date|date }}" /></a>
-      </div>
-   {% endfor %}
+   {% if user == gallery_owner %}
+      <form action="{% url 'user_photos-delete' %}" method="post">{% csrf_token %}
+      {% for photo in photos %}
+         <div style="margin: 1em; display: inline-block; border: 1px solid gray;">
+            <a href="{{ photo.get_absolute_url }}">
+               <img src="{{ photo.thumb_url }}" alt="thumbnail" 
+                  title="{{ photo.upload_date|date }}" style="margin:auto;width:100%" /></a>
+            <br />
+            <div class="centered">
+            <input type="checkbox" name="photo_id" value="{{ photo.id }}"></input>
+            </div>
+         </div>
+      {% endfor %}
+      <br />
+      <input type="submit" name="submit" value="Delete Checked Photos" />
+      </form>
+   {% else %}
+      {% for photo in photos %}
+         <div style="margin: 1em; display: inline;">
+            <a href="{{ photo.get_absolute_url }}">
+               <img src="{{ photo.thumb_url }}" alt="thumbnail" 
+                  title="{{ photo.upload_date|date }}" /></a>
+         </div>
+      {% endfor %}
+   {% endif %}
 
    {% if page_obj %}
       <hr style="margin-top: 1.5em;" />
--- a/user_photos/urls.py	Sun Sep 15 17:00:52 2013 -0500
+++ b/user_photos/urls.py	Tue Sep 17 17:54:21 2013 -0500
@@ -14,4 +14,5 @@
     url(r'^gallery/(?P<username>[\w.@+-]{1,30})/$',
         GalleryView.as_view(),
         name='user_photos-gallery'),
+    url(r'^delete/$', 'user_photos.views.delete', name='user_photos-delete'),
 )
--- 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)