2013-09-24 13:54:41 +00:00
|
|
|
import datetime
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
2013-10-14 10:12:50 +00:00
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
2013-10-02 19:49:55 +00:00
|
|
|
from blog.models import Post, CodeChunk
|
2013-09-24 13:54:41 +00:00
|
|
|
|
2013-10-01 20:55:35 +00:00
|
|
|
def mainpage(request):
|
2013-10-14 10:12:50 +00:00
|
|
|
last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5]
|
2013-10-01 19:17:41 +00:00
|
|
|
return render(request, 'blog/listing.html', {'posts': last_posts})
|
2013-09-24 13:54:41 +00:00
|
|
|
|
2013-10-14 10:12:50 +00:00
|
|
|
def listing(request, tag, page):
|
|
|
|
if (tag == None):
|
|
|
|
post_list = Post.objects.filter(draft = False)
|
|
|
|
view = "index"
|
|
|
|
else:
|
|
|
|
post_list = Post.objects.filter(tags__slug = tag, draft = False)
|
2014-06-14 20:50:07 +00:00
|
|
|
view = "tag"
|
2013-10-14 10:12:50 +00:00
|
|
|
|
|
|
|
paginator = Paginator(post_list.order_by('-created_at'), 10)
|
|
|
|
|
|
|
|
try:
|
|
|
|
posts = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
posts = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
posts = paginator.page(paginator.num_pages)
|
|
|
|
|
|
|
|
return render(request, 'blog/listing.html', { 'posts': posts, 'view': "blog:" + view })
|
|
|
|
|
2013-10-01 20:55:35 +00:00
|
|
|
def index(request):
|
2013-10-14 10:12:50 +00:00
|
|
|
return listing(request, None, 1)
|
|
|
|
|
|
|
|
def indexpage(request, page):
|
|
|
|
return listing(request, None, page)
|
|
|
|
|
|
|
|
def taglist(request, tag):
|
|
|
|
return listing(request, tag, 1)
|
|
|
|
|
|
|
|
def tagpage(request, tag, page):
|
|
|
|
return listing(request, tag, page)
|
2013-10-01 20:55:35 +00:00
|
|
|
|
2013-09-24 13:54:41 +00:00
|
|
|
def read(request, year, month, day, slug):
|
2014-06-14 20:50:41 +00:00
|
|
|
post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug, draft=False)
|
2013-09-24 13:54:41 +00:00
|
|
|
return render(request, 'blog/view.html', {'post': post})
|
|
|
|
|
2013-10-02 19:49:55 +00:00
|
|
|
def codechunk(request, language, slug):
|
|
|
|
chunk = get_object_or_404(CodeChunk, language=language, slug=slug)
|
|
|
|
return render(request, 'blog/code-chunk.html', {'codechunk': chunk})
|
|
|
|
|
2013-09-24 13:54:41 +00:00
|
|
|
def feed(request):
|
|
|
|
return render(request, 'blog/feed.xml', {})
|
|
|
|
|