annotate 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
rev   line source
bgneal@695 1 """Views for the user_photos application."""
bgneal@695 2 from django.conf import settings
bgneal@704 3 from django.contrib.auth import get_user_model
bgneal@695 4 from django.contrib.auth.decorators import login_required
bgneal@704 5 from django.shortcuts import render, redirect, get_object_or_404
bgneal@704 6 from django.views.generic import ListView
bgneal@710 7 from django.views.decorators.http import require_POST
bgneal@704 8 from django.utils.decorators import method_decorator
bgneal@710 9 from django.contrib import messages
bgneal@695 10
bgneal@695 11 from user_photos.forms import UploadForm
bgneal@704 12 from user_photos.models import Photo
bgneal@695 13
bgneal@695 14
bgneal@695 15 @login_required
bgneal@695 16 def upload(request):
bgneal@695 17 """This view function receives an uploaded image file from a user.
bgneal@695 18 The photo will be resized if necessary and a thumbnail image will be
bgneal@695 19 created. The image and thumbnail will then be uploaded to the Amazon
bgneal@695 20 S3 service for storage.
bgneal@695 21
bgneal@695 22 TODO: rate limiting
bgneal@695 23 pass off the processing to a celery task
bgneal@695 24 ajax version of this view
bgneal@695 25
bgneal@695 26 """
bgneal@695 27 form = None
bgneal@696 28 uploads_enabled = settings.USER_PHOTOS_ENABLED
bgneal@695 29
bgneal@695 30 if uploads_enabled:
bgneal@695 31 if request.method == 'POST':
bgneal@696 32 form = UploadForm(request.POST, request.FILES, user=request.user)
bgneal@695 33 if form.is_valid():
bgneal@696 34 photo = form.save()
bgneal@696 35 return redirect(photo)
bgneal@695 36 else:
bgneal@696 37 form = UploadForm(user=request.user)
bgneal@695 38
bgneal@695 39 return render(request, 'user_photos/upload_form.html', {
bgneal@695 40 'enabled': uploads_enabled,
bgneal@695 41 'form': form,
bgneal@695 42 },
bgneal@695 43 status=200 if uploads_enabled else 503)
bgneal@704 44
bgneal@704 45
bgneal@704 46 class GalleryView(ListView):
bgneal@704 47 """A ListView for displaying a user's photos"""
bgneal@704 48
bgneal@704 49 template_name = 'user_photos/gallery.html'
bgneal@704 50 context_object_name = 'photos'
bgneal@704 51 paginate_by = 50
bgneal@704 52 allow_empty = True
bgneal@704 53
bgneal@704 54 def get_queryset(self):
bgneal@704 55 self.gallery_owner = get_object_or_404(get_user_model(),
bgneal@704 56 username=self.kwargs['username'])
bgneal@704 57 return Photo.objects.filter(user=self.gallery_owner).order_by('-upload_date')
bgneal@704 58
bgneal@704 59 def get_context_data(self, **kwargs):
bgneal@704 60 context = super(GalleryView, self).get_context_data(**kwargs)
bgneal@704 61 context['gallery_owner'] = self.gallery_owner
bgneal@704 62 return context
bgneal@704 63
bgneal@704 64 @method_decorator(login_required)
bgneal@704 65 def dispatch(self, *args, **kwargs):
bgneal@704 66 return super(GalleryView, self).dispatch(*args, **kwargs)
bgneal@710 67
bgneal@710 68
bgneal@710 69 @login_required
bgneal@710 70 @require_POST
bgneal@710 71 def delete(request):
bgneal@710 72 photo_ids = []
bgneal@710 73 for photo_id in request.POST.getlist('photo_id'):
bgneal@710 74 try:
bgneal@710 75 n = int(photo_id)
bgneal@710 76 except ValueError:
bgneal@710 77 continue
bgneal@710 78 photo_ids.append(n)
bgneal@710 79
bgneal@710 80 count = 0
bgneal@710 81 if photo_ids:
bgneal@710 82 qs = Photo.objects.filter(user=request.user, pk__in=photo_ids)
bgneal@710 83 count = qs.count()
bgneal@710 84 qs.delete()
bgneal@710 85
bgneal@710 86 if count:
bgneal@710 87 msg = "{} photo{} deleted".format(count, '' if count == 1 else 's')
bgneal@710 88 messages.add_message(request, messages.INFO, msg)
bgneal@710 89
bgneal@710 90 return redirect('user_photos-gallery', username=request.user.username)