]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/blog/views.py
add caching to the toplevel menu
[sommitrealweird.git] / sommitrealweird / blog / views.py
1 from blog.models import BlogEntry, BlogSection
2 from django.http import Http404, HttpResponse
3 from django.template import RequestContext, loader
4 from django.core.paginator import ObjectPaginator
5 from django.conf import settings
6
7 def blog_index(request, section=None):
8     entries = BlogEntry.objects.all()
9     if section:
10         entries = entries.filter(sections__slug__exact=section)
11     entries = entries.order_by('-publish_date')
12     if entries:
13         paginate_by=20
14         paginator = ObjectPaginator(entries, paginate_by)
15         page = int(request.GET.get('page', 0))
16         if page > 0:
17             page = page - 1
18         paginated_entries = paginator.get_page(page)
19         pages = [i for i in range(1, paginator.pages + 1)]
20         t = loader.get_template("blog/blog_index.html")
21         c = RequestContext(request,
22             {
23                 "entries": paginated_entries,
24                 "title": settings.BLOG_TITLE,
25                 "page": page + 1,
26                 "pages": pages,
27                 "paginated": 1,
28                 "has_next": paginator.has_next_page(page),
29                 "has_prev": paginator.has_next_page(page),
30                 "next": page + 2,
31                 "prev": page - 1
32             })
33         return HttpResponse(t.render(c))
34     else:
35         raise Http404
36
37 def blog_view(request, year=None, month=None, day=None, hour=None, minutes=None, section=None, slug=None):
38     # work out what we've got roughly
39     if year and month and day and slug:
40         # exact blog entry (woo!)
41             year = int(year)
42             month = int(month)
43             day = int(day)
44             blog_entry = BlogEntry.objects.get(publish_date__year=year, publish_date__month=month, publish_date__day=day, slug__exact=slug)
45             template_name = "blog/single_entry_%s.html" %(blog_entry.format,)
46             t = loader.get_template(template_name)
47             c = RequestContext(request,
48                 {
49                     "entry": blog_entry,
50                     "publish_date": blog_entry.publish_date
51                 }
52             )
53             return HttpResponse(t.render(c))