Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
708:a6c664e04649 | 710:13a1713d05b5 |
---|---|
2 from django.conf import settings | 2 from django.conf import settings |
3 from django.contrib.auth import get_user_model | 3 from django.contrib.auth import get_user_model |
4 from django.contrib.auth.decorators import login_required | 4 from django.contrib.auth.decorators import login_required |
5 from django.shortcuts import render, redirect, get_object_or_404 | 5 from django.shortcuts import render, redirect, get_object_or_404 |
6 from django.views.generic import ListView | 6 from django.views.generic import ListView |
7 from django.views.decorators.http import require_POST | |
7 from django.utils.decorators import method_decorator | 8 from django.utils.decorators import method_decorator |
9 from django.contrib import messages | |
8 | 10 |
9 from user_photos.forms import UploadForm | 11 from user_photos.forms import UploadForm |
10 from user_photos.models import Photo | 12 from user_photos.models import Photo |
11 | 13 |
12 | 14 |
60 return context | 62 return context |
61 | 63 |
62 @method_decorator(login_required) | 64 @method_decorator(login_required) |
63 def dispatch(self, *args, **kwargs): | 65 def dispatch(self, *args, **kwargs): |
64 return super(GalleryView, self).dispatch(*args, **kwargs) | 66 return super(GalleryView, self).dispatch(*args, **kwargs) |
67 | |
68 | |
69 @login_required | |
70 @require_POST | |
71 def delete(request): | |
72 photo_ids = [] | |
73 for photo_id in request.POST.getlist('photo_id'): | |
74 try: | |
75 n = int(photo_id) | |
76 except ValueError: | |
77 continue | |
78 photo_ids.append(n) | |
79 | |
80 count = 0 | |
81 if photo_ids: | |
82 qs = Photo.objects.filter(user=request.user, pk__in=photo_ids) | |
83 count = qs.count() | |
84 qs.delete() | |
85 | |
86 if count: | |
87 msg = "{} photo{} deleted".format(count, '' if count == 1 else 's') | |
88 messages.add_message(request, messages.INFO, msg) | |
89 | |
90 return redirect('user_photos-gallery', username=request.user.username) |