]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blobdiff - sommitrealweird/photo/views.py
Clean up photos so that it does 404s
[sommitrealweird.git] / sommitrealweird / photo / views.py
index 02d80e67be0435a7dd04554ac6f9cc5b5b7be9f6..8501a6fef06677a1f74242a031f1912622a20832 100644 (file)
@@ -1,36 +1,59 @@
-import django.views.generic.list_detail
+from django.views.generic import ListView, DetailView
 from django.http import Http404
 from models import Album, Photo
 
-def album_view(request, slug):
-    try:
-        album = Album.objects.get(slug__exact=slug)
-        photos = Photo.objects.filter(album=album).order_by('order', 'image')
-    except:
-        raise Http404
-    return django.views.generic.list_detail.object_list(request, photos, paginate_by=20, template_name='photo/photo_index.html', allow_empty=False)
-
-def photo_view(request, slug, id):
-    try:
-        id = int(id)
-        album = Album.objects.get(slug__exact=slug)
+class AlbumListView(ListView):
+    model = Album
+    queryset = Album.objects.order_by('-name')
+    paginate_by = 20
+
+class PhotoListView(ListView):
+    model = Photo
+    paginate_by = 20
+    order_by = 'order, image'
+
+    def get_queryset(self):
+        try:
+            album = Album.objects.get(slug__exact=self.kwargs['slug'])
+            return Photo.objects.filter(album=album)
+        except:
+            raise Http404
+
+class PhotoView(DetailView):
+
+    def get_queryset(self, **kwargs):
+        return Photo.objects.get(id=self.kwargs['id']).get_queryset()
+
+    def get_object(self, **kwargs):
+        try:
+            photo = Photo.objects.get(id=self.kwargs['id'])
+            return photo
+        except:
+            raise Http404
+
+    def get_context_data(self, **kwargs):
+        context = super(PhotoView, self).get_context_data(**kwargs)
+        album = Album.objects.get(slug__exact=self.kwargs['slug'])
         photos = Photo.objects.filter(album=album).order_by('order', 'image')
-        photo_qs = Photo.objects.filter(id=id)
+
         found_prev = False
         prev_photo = None
         found_next = False
         next_photo = None
+
         for photo in photos:
             if not found_prev:
-                if not photo.id == id:
+                if not photo.id == int(self.kwargs['id']):
                     prev_photo = photo
                 else:
                     found_prev = True
             if found_next:
                 next_photo = photo
                 break
-            if photo.id == id:
+            if photo.id == int(self.kwargs['id']):
                 found_next = True
-    except:
-        raise Http404
-    return django.views.generic.list_detail.object_detail(request, photo_qs, object_id=id, template_name='photo/photo.html', extra_context={"next_photo": next_photo, "prev_photo": prev_photo })
+
+        context['next_photo'] = next_photo
+        context['prev_photo'] = prev_photo
+
+        return context