annotate user_photos/views.py @ 704:809d27b385f2

For user uploaded photos (#50), add a simple gallery view.
author Brian Neal <bgneal@gmail.com>
date Sun, 15 Sep 2013 15:13:02 -0500
parents b2a8fde3173a
children 13a1713d05b5
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@704 7 from django.utils.decorators import method_decorator
bgneal@695 8
bgneal@695 9 from user_photos.forms import UploadForm
bgneal@704 10 from user_photos.models import Photo
bgneal@695 11
bgneal@695 12
bgneal@695 13 @login_required
bgneal@695 14 def upload(request):
bgneal@695 15 """This view function receives an uploaded image file from a user.
bgneal@695 16 The photo will be resized if necessary and a thumbnail image will be
bgneal@695 17 created. The image and thumbnail will then be uploaded to the Amazon
bgneal@695 18 S3 service for storage.
bgneal@695 19
bgneal@695 20 TODO: rate limiting
bgneal@695 21 pass off the processing to a celery task
bgneal@695 22 ajax version of this view
bgneal@695 23
bgneal@695 24 """
bgneal@695 25 form = None
bgneal@696 26 uploads_enabled = settings.USER_PHOTOS_ENABLED
bgneal@695 27
bgneal@695 28 if uploads_enabled:
bgneal@695 29 if request.method == 'POST':
bgneal@696 30 form = UploadForm(request.POST, request.FILES, user=request.user)
bgneal@695 31 if form.is_valid():
bgneal@696 32 photo = form.save()
bgneal@696 33 return redirect(photo)
bgneal@695 34 else:
bgneal@696 35 form = UploadForm(user=request.user)
bgneal@695 36
bgneal@695 37 return render(request, 'user_photos/upload_form.html', {
bgneal@695 38 'enabled': uploads_enabled,
bgneal@695 39 'form': form,
bgneal@695 40 },
bgneal@695 41 status=200 if uploads_enabled else 503)
bgneal@704 42
bgneal@704 43
bgneal@704 44 class GalleryView(ListView):
bgneal@704 45 """A ListView for displaying a user's photos"""
bgneal@704 46
bgneal@704 47 template_name = 'user_photos/gallery.html'
bgneal@704 48 context_object_name = 'photos'
bgneal@704 49 paginate_by = 50
bgneal@704 50 allow_empty = True
bgneal@704 51
bgneal@704 52 def get_queryset(self):
bgneal@704 53 self.gallery_owner = get_object_or_404(get_user_model(),
bgneal@704 54 username=self.kwargs['username'])
bgneal@704 55 return Photo.objects.filter(user=self.gallery_owner).order_by('-upload_date')
bgneal@704 56
bgneal@704 57 def get_context_data(self, **kwargs):
bgneal@704 58 context = super(GalleryView, self).get_context_data(**kwargs)
bgneal@704 59 context['gallery_owner'] = self.gallery_owner
bgneal@704 60 return context
bgneal@704 61
bgneal@704 62 @method_decorator(login_required)
bgneal@704 63 def dispatch(self, *args, **kwargs):
bgneal@704 64 return super(GalleryView, self).dispatch(*args, **kwargs)