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