]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blobdiff - sommitrealweird/blog/views.py
Small photo fixes
[sommitrealweird.git] / sommitrealweird / blog / views.py
index a801cd463081518c599cdf2c929e731f211cdd49..08b52670cb00ce3c15507d9e500e8b7631d7e7e8 100644 (file)
@@ -1,7 +1,7 @@
 from blog.models import BlogEntry, BlogSection
 from django.http import Http404, HttpResponse
 from django.template import RequestContext, loader
 from blog.models import BlogEntry, BlogSection
 from django.http import Http404, HttpResponse
 from django.template import RequestContext, loader
-from django.core.paginator import ObjectPaginator
+from django.core.paginator import Paginator
 from django.conf import settings
 
 def blog_index(request, section=None):
 from django.conf import settings
 
 def blog_index(request, section=None):
@@ -11,23 +11,25 @@ def blog_index(request, section=None):
     entries = entries.order_by('-publish_date')
     if entries:
         paginate_by=20
     entries = entries.order_by('-publish_date')
     if entries:
         paginate_by=20
-        paginator = ObjectPaginator(entries, paginate_by)
-        page = int(request.GET.get('page', 0))
-        if page > 0:
-            page = page - 1
-        paginated_entries = paginator.get_page(page)
-        pages = [i for i in range(1, paginator.pages + 1)]
+        paginator = Paginator(entries, paginate_by)
+        try:
+            page = int(request.GET.get('page', 1))
+        except ValueError:
+            page = 1
+        paginated_entries = paginator.page(page)
+        pages = paginator.page_range
         t = loader.get_template("blog/blog_index.html")
         c = RequestContext(request,
             {
         t = loader.get_template("blog/blog_index.html")
         c = RequestContext(request,
             {
-                "entries": paginated_entries,
+                "entries": paginated_entries.object_list,
                 "title": settings.BLOG_TITLE,
                 "title": settings.BLOG_TITLE,
-                "page": page + 1,
+                "page": page,
                 "pages": pages,
                 "paginated": 1,
                 "pages": pages,
                 "paginated": 1,
-                "has_next": paginator.has_next_page(page),
-                "has_prev": paginator.has_next_page(page),
-                "next": page + 2,
+                "has_more_than_one_page": (len(pages) > 1),
+                "has_next": paginated_entries.has_next(),
+                "has_prev": paginated_entries.has_previous(),
+                "next": page + 1,
                 "prev": page - 1
             })
         return HttpResponse(t.render(c))
                 "prev": page - 1
             })
         return HttpResponse(t.render(c))
@@ -46,9 +48,49 @@ def blog_view(request, year=None, month=None, day=None, hour=None, minutes=None,
             t = loader.get_template(template_name)
             c = RequestContext(request,
                 {
             t = loader.get_template(template_name)
             c = RequestContext(request,
                 {
-                    "content": blog_entry.content,
-                    "title": blog_entry.title,
+                    "entry": blog_entry,
                     "publish_date": blog_entry.publish_date
                 }
             )
             return HttpResponse(t.render(c))
                     "publish_date": blog_entry.publish_date
                 }
             )
             return HttpResponse(t.render(c))
+    else:
+        entries = BlogEntry.objects.all()
+        if year and month and day:
+            year = int(year)
+            month = int(month)
+            day = int(day)
+            entries = entries.filter(publish_date__year=year, publish_date__month=month, publish_date__day=day)
+        elif year and month:
+            year = int(year)
+            month = int(month)
+            entries = entries.filter(publish_date__year=year, publish_date__month=month)
+        elif year:
+            year = int(year)
+            entries = entries.filter(publish_date__year=year)
+        if entries:
+            entries = entries.order_by('-publish_date')
+            paginate_by=20
+            paginator = Paginator(entries, paginate_by)
+            try:
+                page = int(request.GET.get('page', 1))
+            except ValueError:
+                page = 1
+            paginated_entries = paginator.page(page)
+            pages = paginator.page_range
+            t = loader.get_template("blog/blog_index.html")
+            c = RequestContext(request,
+                {
+                    "entries": paginated_entries.object_list,
+                    "title": settings.BLOG_TITLE,
+                    "page": page,
+                    "pages": pages,
+                    "paginated": 1,
+                    "has_more_than_one_page": (len(pages) > 1),
+                    "has_next": paginated_entries.has_next(),
+                    "has_prev": paginated_entries.has_previous(),
+                    "next": page + 1,
+                    "prev": page - 1
+                })
+            return HttpResponse(t.render(c))
+        else:
+            raise Http404