Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
717:846cf9a06a04 | 718:bf5340705d0c |
---|---|
8 from django.utils.decorators import method_decorator | 8 from django.utils.decorators import method_decorator |
9 from django.contrib import messages | 9 from django.contrib import messages |
10 | 10 |
11 from user_photos.forms import UploadForm | 11 from user_photos.forms import UploadForm |
12 from user_photos.models import Photo | 12 from user_photos.models import Photo |
13 from user_photos.s3 import delete_photos | |
13 | 14 |
14 | 15 |
15 @login_required | 16 @login_required |
16 def upload(request): | 17 def upload(request): |
17 """This view function receives an uploaded image file from a user. | 18 """This view function receives an uploaded image file from a user. |
67 | 68 |
68 | 69 |
69 @login_required | 70 @login_required |
70 @require_POST | 71 @require_POST |
71 def delete(request): | 72 def delete(request): |
73 """A view function to allow a user to delete their own photos.""" | |
74 | |
75 ret_view, username = 'user_photos-gallery', request.user.username | |
76 | |
77 if not settings.USER_PHOTOS_ENABLED: | |
78 messages.error(request, "This function is disabled temporarily") | |
79 return redirect(ret_view, username) | |
80 | |
72 photo_ids = [] | 81 photo_ids = [] |
73 for photo_id in request.POST.getlist('photo_id'): | 82 for photo_id in request.POST.getlist('photo_id'): |
74 try: | 83 try: |
75 n = int(photo_id) | 84 n = int(photo_id) |
76 except ValueError: | 85 except ValueError: |
78 photo_ids.append(n) | 87 photo_ids.append(n) |
79 | 88 |
80 count = 0 | 89 count = 0 |
81 if photo_ids: | 90 if photo_ids: |
82 qs = Photo.objects.filter(user=request.user, pk__in=photo_ids) | 91 qs = Photo.objects.filter(user=request.user, pk__in=photo_ids) |
83 count = qs.count() | 92 count = len(qs) |
84 qs.delete() | 93 if count: |
94 delete_photos(qs) | |
95 qs.delete() | |
85 | 96 |
86 if count: | 97 msg = "{} photo{} deleted".format(count, '' if count == 1 else 's') |
87 msg = "{} photo{} deleted".format(count, '' if count == 1 else 's') | 98 messages.add_message(request, |
88 messages.add_message(request, messages.INFO, msg) | 99 messages.SUCCESS if count > 0 else messages.WARNING, |
100 msg) | |
89 | 101 |
90 return redirect('user_photos-gallery', username=request.user.username) | 102 return redirect(ret_view, username) |