+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.conf import settings
+
+def blog_index(request, section=None):
+ entries = BlogEntry.objects.all()
+ if section:
+ entries = entries.filter(sections__slug__exact=section)
+ 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)]
+ t = loader.get_template("blog/blog_index.html")
+ c = RequestContext(request,
+ {
+ "entries": paginated_entries,
+ "title": settings.BLOG_TITLE,
+ "page": page + 1,
+ "pages": pages,
+ "paginated": 1,
+ "has_next": paginator.has_next_page(page),
+ "has_prev": paginator.has_next_page(page),
+ "next": page + 2,
+ "prev": page - 1
+ })
+ return HttpResponse(t.render(c))
+ else:
+ raise Http404
+
+def blog_view(request, year=None, month=None, day=None, hour=None, minutes=None, section=None, slug=None):
+ # work out what we've got roughly
+ if year and month and day and slug:
+ # exact blog entry (woo!)
+ year = int(year)
+ month = int(month)
+ day = int(day)
+ blog_entry = BlogEntry.objects.get(publish_date__year=year, publish_date__month=month, publish_date__day=day, slug__exact=slug)
+ template_name = "blog/single_entry_%s.html" %(blog_entry.format,)
+ t = loader.get_template(template_name)
+ c = RequestContext(request,
+ {
+ "content": blog_entry.content,
+ "title": blog_entry.title,
+ "publish_date": blog_entry.publish_date
+ }
+ )
+ return HttpResponse(t.render(c))