]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/blog/views.py
Fix up for django 1.10
[sommitrealweird.git] / sommitrealweird / blog / views.py
1 from blog.models import BlogEntry, BlogSection
2 from django.http import Http404, HttpResponse
3 from django.shortcuts import render
4 from django.template import RequestContext, loader
5 from django.core.paginator import Paginator
6 from django.conf import settings
7
8 def blog_index(request, section=None):
9     entries = BlogEntry.objects.all()
10     if section:
11         entries = entries.filter(sections__slug__exact=section)
12     entries = entries.order_by('-publish_date')
13     if entries:
14         paginate_by=20
15         paginator = Paginator(entries, paginate_by)
16         try:
17             page = int(request.GET.get('page', 1))
18         except ValueError:
19             page = 1
20         try:
21             paginated_entries = paginator.page(page)
22             pages = paginator.page_range
23             c = {
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 render(request, 'blog/blog_index.html', c)
36         except:
37             raise Http404
38     else:
39         raise Http404
40
41 def blog_view(request, year=None, month=None, day=None, hour=None, minutes=None, section=None, slug=None):
42     # work out what we've got roughly
43     if year and month and day and slug:
44         # exact blog entry (woo!)
45             year = int(year)
46             month = int(month)
47             day = int(day)
48             try:
49                 blog_entry = BlogEntry.objects.get(publish_date__year=year, publish_date__month=month, publish_date__day=day, slug__exact=slug)
50                 template_name = "blog/single_entry_%s.html" %(blog_entry.format,)
51                 c = {
52                         "entry": blog_entry,
53                         "publish_date": blog_entry.publish_date
54                     }
55                 return render(request, template_name, c)
56             except:
57                 raise Http404
58     else:
59         entries = BlogEntry.objects.all()
60         if year and month and day:
61             year = int(year)
62             month = int(month)
63             day = int(day)
64             entries = entries.filter(publish_date__year=year, publish_date__month=month, publish_date__day=day)
65         elif year and month:
66             year = int(year)
67             month = int(month)
68             entries = entries.filter(publish_date__year=year, publish_date__month=month)
69         elif year:
70             year = int(year)
71             entries = entries.filter(publish_date__year=year)
72         if entries:
73             entries = entries.order_by('-publish_date')
74             paginate_by=20
75             paginator = Paginator(entries, paginate_by)
76             try:
77                 page = int(request.GET.get('page', 1))
78             except ValueError:
79                 page = 1
80             try:
81                 paginated_entries = paginator.page(page)
82                 pages = paginator.page_range
83                 c = {
84                         "entries": paginated_entries.object_list,
85                         "title": settings.BLOG_TITLE,
86                         "page": page,
87                         "pages": pages,
88                         "paginated": 1,
89                         "has_more_than_one_page": (len(pages) > 1),
90                         "has_next": paginated_entries.has_next(),
91                         "has_prev": paginated_entries.has_previous(),
92                         "next": page + 1,
93                         "prev": page - 1
94                     }
95                 return render(request, "blog/blog_index.html", c)
96             except:
97                 raise Http404
98         else:
99             raise Http404