Make photo albums work
authorBrett Parker <brettp@mythic-beasts.com>
Fri, 22 May 2015 16:13:13 +0000 (17:13 +0100)
committerBrett Parker <brettp@mythic-beasts.com>
Fri, 22 May 2015 16:13:13 +0000 (17:13 +0100)
sommitrealweird/photo/templates/photo/photo_detail.html [moved from sommitrealweird/photo/templates/photo/photo.html with 100% similarity]
sommitrealweird/photo/templates/photo/photo_list.html [moved from sommitrealweird/photo/templates/photo/photo_index.html with 93% similarity]
sommitrealweird/photo/urls.py
sommitrealweird/photo/views.py

similarity index 93%
rename from sommitrealweird/photo/templates/photo/photo_index.html
rename to sommitrealweird/photo/templates/photo/photo_list.html
index 27e441793c72123650a0718ef04893480dcebd97..558124307560aeedf937ad8a4acfd9abf3a936dd 100644 (file)
@@ -28,8 +28,8 @@
                 {% else %}
                     |
                 {% endif %}
-                {% ifequal paginator_page page %}
-                    {{ page }}
+                {% ifequal paginator_page page_obj.number %}
+                    {{ paginator_page }}
                 {% else %}
                     <a href="?page={{ paginator_page }}">{{ paginator_page }}</a>
                 {% endifequal %}
index dbea64f99eca4e3d7560208fba04d33b857e1fbc..d5666e586103114c61938792e935b09efaa72a36 100644 (file)
@@ -1,9 +1,9 @@
 from django.conf.urls import url
 from models import Album
-from views import AlbumListView
+from views import AlbumListView, PhotoListView, PhotoView
 
 urlpatterns = [
     url(r'^$', AlbumListView.as_view()),
-    url(r'^(?P<slug>[^/]*)/$', 'photo.views.album_view'),
-    url(r'^(?P<slug>[^/]*)/(?P<id>[0-9]+)/$', 'photo.views.photo_view'),
+    url(r'^(?P<slug>[^/]*)/$', PhotoListView.as_view()),
+    url(r'^(?P<slug>[^/]*)/(?P<id>[0-9]+)/$', PhotoView.as_view()),
 ]
index 77b4dd88c9e14134de991c1cc28c74de2557a623..28955abc072f3b32fa2a4880ef4976e92f308300 100644 (file)
@@ -1,4 +1,4 @@
-from django.views.generic import ListView
+from django.views.generic import ListView, DetailView
 from django.http import Http404
 from models import Album, Photo
 
@@ -11,35 +11,45 @@ class AlbumListView(ListView):
         context = super(AlbumListView, self).get_context_data(**kwargs)
         return context
 
-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 PhotoListView(ListView):
+    model = Photo
+    paginate_by = 20
+    order_by = 'order, image'
+
+    def get_queryset(self):
+        album = Album.objects.get(slug__exact=self.kwargs['slug'])
+        return Photo.objects.filter(album=album)
+
+class PhotoView(DetailView):
+    model = Photo
+
+    def get_object(self, **kwargs):
+        return Photo.objects.get(id=self.kwargs['id'])
+
+    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
+