Mercurial > public > sg101
diff 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 |
line wrap: on
line diff
--- a/user_photos/views.py Sat Sep 14 18:43:10 2013 -0500 +++ b/user_photos/views.py Sun Sep 15 15:13:02 2013 -0500 @@ -1,9 +1,13 @@ """Views for the user_photos application.""" from django.conf import settings +from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required -from django.shortcuts import render, redirect +from django.shortcuts import render, redirect, get_object_or_404 +from django.views.generic import ListView +from django.utils.decorators import method_decorator from user_photos.forms import UploadForm +from user_photos.models import Photo @login_required @@ -35,3 +39,26 @@ 'form': form, }, status=200 if uploads_enabled else 503) + + +class GalleryView(ListView): + """A ListView for displaying a user's photos""" + + template_name = 'user_photos/gallery.html' + context_object_name = 'photos' + paginate_by = 50 + allow_empty = True + + def get_queryset(self): + self.gallery_owner = get_object_or_404(get_user_model(), + username=self.kwargs['username']) + return Photo.objects.filter(user=self.gallery_owner).order_by('-upload_date') + + def get_context_data(self, **kwargs): + context = super(GalleryView, self).get_context_data(**kwargs) + context['gallery_owner'] = self.gallery_owner + return context + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(GalleryView, self).dispatch(*args, **kwargs)