]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/photo/views.py
Fix photo ordering
[sommitrealweird.git] / sommitrealweird / photo / views.py
1 from django.views.generic import ListView, DetailView
2 from django.http import Http404
3 from photo.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
14     def order_by(self):
15         return self.ordering
16
17     def get_queryset(self):
18         try:
19             album = Album.objects.get(slug__exact=self.kwargs['slug'])
20             return Photo.objects.filter(album=album).order_by('order', 'image')
21         except:
22             raise Http404
23
24 class PhotoView(DetailView):
25
26     def get_queryset(self, **kwargs):
27         return Photo.objects.get(id=self.kwargs['id']).get_queryset()
28
29     def get_object(self, **kwargs):
30         try:
31             photo = Photo.objects.get(id=self.kwargs['id'])
32             return photo
33         except:
34             raise Http404
35
36     def get_context_data(self, **kwargs):
37         context = super(PhotoView, self).get_context_data(**kwargs)
38         album = Album.objects.get(slug__exact=self.kwargs['slug'])
39         photos = Photo.objects.filter(album=album).order_by('order', 'image')
40
41         found_prev = False
42         prev_photo = None
43         found_next = False
44         next_photo = None
45
46         for photo in photos:
47             if not found_prev:
48                 if not photo.id == int(self.kwargs['id']):
49                     prev_photo = photo
50                 else:
51                     found_prev = True
52             if found_next:
53                 next_photo = photo
54                 break
55             if photo.id == int(self.kwargs['id']):
56                 found_next = True
57
58         context['next_photo'] = next_photo
59         context['prev_photo'] = prev_photo
60
61         return context