]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/blog/views.py
08b52670cb00ce3c15507d9e500e8b7631d7e7e8
[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 Paginator
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 = Paginator(entries, paginate_by)
15         try:
16             page = int(request.GET.get('page', 1))
17         except ValueError:
18             page = 1
19         paginated_entries = paginator.page(page)
20         pages = paginator.page_range
21         t = loader.get_template("blog/blog_index.html")
22         c = RequestContext(request,
23             {
24                 "entries": paginated_entries.object_list,
25                 "title": settings.BLOG_TITLE,
26                 "page": page,
27                 "pages": pages,
28                 "paginated": 1,
29                 "has_more_than_one_page": (len(pages) > 1),
30                 "has_next": paginated_entries.has_next(),
31                 "has_prev": paginated_entries.has_previous(),
32                 "next": page + 1,
33                 "prev": page - 1
34             })
35         return HttpResponse(t.render(c))
36     else:
37         raise Http404
38
39 def blog_view(request, year=None, month=None, day=None, hour=None, minutes=None, section=None, slug=None):
40     # work out what we've got roughly
41     if year and month and day and slug:
42         # exact blog entry (woo!)
43             year = int(year)
44             month = int(month)
45             day = int(day)
46             blog_entry = BlogEntry.objects.get(publish_date__year=year, publish_date__month=month, publish_date__day=day, slug__exact=slug)
47             template_name = "blog/single_entry_%s.html" %(blog_entry.format,)
48             t = loader.get_template(template_name)
49             c = RequestContext(request,
50                 {
51                     "entry": blog_entry,
52                     "publish_date": blog_entry.publish_date
53                 }
54             )
55             return HttpResponse(t.render(c))
56     else:
57         entries = BlogEntry.objects.all()
58         if year and month and day:
59             year = int(year)
60             month = int(month)
61             day = int(day)
62             entries = entries.filter(publish_date__year=year, publish_date__month=month, publish_date__day=day)
63         elif year and month:
64             year = int(year)
65             month = int(month)
66             entries = entries.filter(publish_date__year=year, publish_date__month=month)
67         elif year:
68             year = int(year)
69             entries = entries.filter(publish_date__year=year)
70         if entries:
71             entries = entries.order_by('-publish_date')
72             paginate_by=20
73             paginator = Paginator(entries, paginate_by)
74             try:
75                 page = int(request.GET.get('page', 1))
76             except ValueError:
77                 page = 1
78             paginated_entries = paginator.page(page)
79             pages = paginator.page_range
80             t = loader.get_template("blog/blog_index.html")
81             c = RequestContext(request,
82                 {
83                     "entries": paginated_entries.object_list,
84                     "title": settings.BLOG_TITLE,
85                     "page": page,
86                     "pages": pages,
87                     "paginated": 1,
88                     "has_more_than_one_page": (len(pages) > 1),
89                     "has_next": paginated_entries.has_next(),
90                     "has_prev": paginated_entries.has_previous(),
91                     "next": page + 1,
92                     "prev": page - 1
93                 })
94             return HttpResponse(t.render(c))
95         else:
96             raise Http404