Make photo albums work
[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     def get_context_data(self, **kwargs):
11         context = super(AlbumListView, self).get_context_data(**kwargs)
12         return context
13
14 class PhotoListView(ListView):
15     model = Photo
16     paginate_by = 20
17     order_by = 'order, image'
18
19     def get_queryset(self):
20         album = Album.objects.get(slug__exact=self.kwargs['slug'])
21         return Photo.objects.filter(album=album)
22
23 class PhotoView(DetailView):
24     model = Photo
25
26     def get_object(self, **kwargs):
27         return Photo.objects.get(id=self.kwargs['id'])
28
29     def get_context_data(self, **kwargs):
30         context = super(PhotoView, self).get_context_data(**kwargs)
31         album = Album.objects.get(slug__exact=self.kwargs['slug'])
32         photos = Photo.objects.filter(album=album).order_by('order', 'image')
33
34         found_prev = False
35         prev_photo = None
36         found_next = False
37         next_photo = None
38
39         for photo in photos:
40             if not found_prev:
41                 if not photo.id == int(self.kwargs['id']):
42                     prev_photo = photo
43                 else:
44                     found_prev = True
45             if found_next:
46                 next_photo = photo
47                 break
48             if photo.id == int(self.kwargs['id']):
49                 found_next = True
50
51         context['next_photo'] = next_photo
52         context['prev_photo'] = prev_photo
53
54         return context
55