Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
703:d7a0aaabc06c | 704:809d27b385f2 |
---|---|
1 """Views for the user_photos application.""" | 1 """Views for the user_photos application.""" |
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.decorators import login_required | 4 from django.contrib.auth.decorators import login_required |
4 from django.shortcuts import render, redirect | 5 from django.shortcuts import render, redirect, get_object_or_404 |
6 from django.views.generic import ListView | |
7 from django.utils.decorators import method_decorator | |
5 | 8 |
6 from user_photos.forms import UploadForm | 9 from user_photos.forms import UploadForm |
10 from user_photos.models import Photo | |
7 | 11 |
8 | 12 |
9 @login_required | 13 @login_required |
10 def upload(request): | 14 def upload(request): |
11 """This view function receives an uploaded image file from a user. | 15 """This view function receives an uploaded image file from a user. |
33 return render(request, 'user_photos/upload_form.html', { | 37 return render(request, 'user_photos/upload_form.html', { |
34 'enabled': uploads_enabled, | 38 'enabled': uploads_enabled, |
35 'form': form, | 39 'form': form, |
36 }, | 40 }, |
37 status=200 if uploads_enabled else 503) | 41 status=200 if uploads_enabled else 503) |
42 | |
43 | |
44 class GalleryView(ListView): | |
45 """A ListView for displaying a user's photos""" | |
46 | |
47 template_name = 'user_photos/gallery.html' | |
48 context_object_name = 'photos' | |
49 paginate_by = 50 | |
50 allow_empty = True | |
51 | |
52 def get_queryset(self): | |
53 self.gallery_owner = get_object_or_404(get_user_model(), | |
54 username=self.kwargs['username']) | |
55 return Photo.objects.filter(user=self.gallery_owner).order_by('-upload_date') | |
56 | |
57 def get_context_data(self, **kwargs): | |
58 context = super(GalleryView, self).get_context_data(**kwargs) | |
59 context['gallery_owner'] = self.gallery_owner | |
60 return context | |
61 | |
62 @method_decorator(login_required) | |
63 def dispatch(self, *args, **kwargs): | |
64 return super(GalleryView, self).dispatch(*args, **kwargs) |