Clean up photos so that it does 404s
[sommitrealweird.git] / sommitrealweird / photo / views.py
1 from django.views.generic import ListView, DetailView
2 from django.http import Http404
3 from models import Album, Photo
4
5 class AlbumListView(ListView):
6     model = Album
7     queryset = Album.objects.order_by('-name')
8     paginate_by = 20
9
10 class PhotoListView(ListView):
11     model = Photo
12     paginate_by = 20
13     order_by = 'order, image'
14
15     def get_queryset(self):
16         try:
17             album = Album.objects.get(slug__exact=self.kwargs['slug'])
18             return Photo.objects.filter(album=album)
19         except:
20             raise Http404
21
22 class PhotoView(DetailView):
23
24     def get_queryset(self, **kwargs):
25         return Photo.objects.get(id=self.kwargs['id']).get_queryset()
26
27     def get_object(self, **kwargs):
28         try:
29             photo = Photo.objects.get(id=self.kwargs['id'])
30             return photo
31         except:
32             raise Http404
33
34     def get_context_data(self, **kwargs):
35         context = super(PhotoView, self).get_context_data(**kwargs)
36         album = Album.objects.get(slug__exact=self.kwargs['slug'])
37         photos = Photo.objects.filter(album=album).order_by('order', 'image')
38
39         found_prev = False
40         prev_photo = None
41         found_next = False
42         next_photo = None
43
44         for photo in photos:
45             if not found_prev:
46                 if not photo.id == int(self.kwargs['id']):
47                     prev_photo = photo
48                 else:
49                     found_prev = True
50             if found_next:
51                 next_photo = photo
52                 break
53             if photo.id == int(self.kwargs['id']):
54                 found_next = True
55
56         context['next_photo'] = next_photo
57         context['prev_photo'] = prev_photo
58
59         return context