/
views.py
64 lines (52 loc) · 1.95 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404
import datetime
from blog.models import Post, Category
from blog.settings import N_HOME_POSTS
def getc(request):
c = RequestContext(request)
c['categories'] = Category.objects.all()
#sort out months menu (archive)
stats = {} # {<date> : n_posts, ... }
for p in Post.objects.all():
first_of_month = datetime.date(p.date.year, p.date.month, 1)
stats[first_of_month] = stats.get(first_of_month, 0) + 1
months = []
for d, n in stats.items() :
months.append({'date' : d, 'n_posts' : n})
c['months'] = months
return c
def home(request) :
c = getc(request)
c['posts'] = Post.objects.all().order_by("-date")[:N_HOME_POSTS]
return render_to_response("blog/home.html", c)
def show_month(request, year, month) :
c = getc(request)
year = int(year)
month = int(month)
try:
start = datetime.date(year, month, 01)
# gracefully catch months greater than 13
except ValueError:
raise Http404("there are no valid months after 12")
if month < 12:
end = datetime.date(year, month+1, 01)
elif month == 12:
end = datetime.date(year, month, 01)
c['posts'] = Post.objects.filter(date__gte=start, date__lt=end).order_by("-date")
c['month'] = start
return render_to_response("blog/show_month.html", c)
def show_post(request, year, month, title) :
c = getc(request)
c['posts'] = Post.objects.filter(title=title)
if c['posts']:
c['post'] = c['posts'][0]
return render_to_response("blog/show_post.html", c)
else:
raise Http404("Not posts matching that title exist")
def show_category(request, category) :
c = getc(request)
c['posts'] = Post.objects.filter(categories__name=category).order_by("-date")
c['category'] = category
return render_to_response("blog/show_category.html", c)