2013-09-24 13:54:41 +00:00
|
|
|
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
|
2014-06-15 08:06:55 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
import datetime
|
|
|
|
import pytz
|
|
|
|
from django.templatetags.static import static
|
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
|
|
|
|
2014-04-21 08:53:53 +00:00
|
|
|
def listing(request, tag, year, month, day, page):
|
|
|
|
kwargs = {}
|
|
|
|
kwargs['draft'] = False
|
|
|
|
|
2014-06-20 19:49:06 +00:00
|
|
|
view = 'index'
|
|
|
|
|
2013-10-14 10:12:50 +00:00
|
|
|
if (tag == None):
|
2014-04-21 08:39:43 +00:00
|
|
|
view = 'index'
|
2013-10-14 10:12:50 +00:00
|
|
|
else:
|
2014-04-21 08:53:53 +00:00
|
|
|
kwargs['tags__slug'] = tag
|
2014-04-21 08:39:43 +00:00
|
|
|
view = 'tag'
|
2014-04-21 08:53:53 +00:00
|
|
|
|
|
|
|
if (year != None):
|
|
|
|
kwargs['created_at__year'] = year
|
|
|
|
|
|
|
|
if (month != None):
|
|
|
|
kwargs['created_at__month'] = month
|
2013-10-14 10:12:50 +00:00
|
|
|
|
2014-04-21 08:53:53 +00:00
|
|
|
if (day != None):
|
|
|
|
kwargs['created_at__day'] = day
|
|
|
|
|
|
|
|
post_list = Post.objects.filter(**kwargs)
|
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)
|
|
|
|
|
2014-06-20 19:49:06 +00:00
|
|
|
if paginator.num_pages > 1:
|
|
|
|
view = view + 'page'
|
|
|
|
|
2014-04-21 08:39:43 +00:00
|
|
|
return render(request, 'blog/listing.html', {
|
|
|
|
'posts': posts,
|
|
|
|
'tag': tag,
|
|
|
|
'view': "blog:" + view
|
|
|
|
})
|
2013-10-14 10:12:50 +00:00
|
|
|
|
2013-10-01 20:55:35 +00:00
|
|
|
def index(request):
|
2014-04-21 08:53:53 +00:00
|
|
|
return listing(request, None, None, None, None, 1)
|
2013-10-14 10:12:50 +00:00
|
|
|
|
|
|
|
def indexpage(request, page):
|
2014-04-21 08:53:53 +00:00
|
|
|
return listing(request, None, None, None, None, page)
|
2013-10-14 10:12:50 +00:00
|
|
|
|
|
|
|
def taglist(request, tag):
|
2014-04-21 08:53:53 +00:00
|
|
|
return listing(request, tag, None, None, None, 1)
|
2013-10-14 10:12:50 +00:00
|
|
|
|
|
|
|
def tagpage(request, tag, page):
|
2014-04-21 08:53:53 +00:00
|
|
|
return listing(request, tag, None, None, None, page)
|
|
|
|
|
|
|
|
def datelist(request, year, month, day):
|
|
|
|
return listing(request, None, year, month, day, 1)
|
|
|
|
|
|
|
|
def datepage(request, year, month, day, page):
|
|
|
|
return listing(request, None, year, month, day, 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-04-21 08:39:43 +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
|
|
|
|
)
|
2014-06-22 15:14:06 +00:00
|
|
|
next_post = Post.objects.filter(created_at__gt = post.created_at, draft = False).order_by('created_at')[0:1]
|
|
|
|
prev_post = Post.objects.filter(created_at__lt = post.created_at, draft = False).order_by('-created_at')[0:1]
|
2014-06-19 14:25:23 +00:00
|
|
|
|
|
|
|
if not next_post:
|
|
|
|
next_post = None
|
|
|
|
else:
|
|
|
|
next_post = next_post[0]
|
|
|
|
|
|
|
|
if not prev_post:
|
|
|
|
prev_post = None
|
|
|
|
else:
|
|
|
|
prev_post = prev_post[0]
|
|
|
|
|
2014-04-21 08:39:43 +00:00
|
|
|
return render(request, 'blog/view.html', {
|
|
|
|
'post': post,
|
|
|
|
'prev_post': prev_post,
|
|
|
|
'next_post': next_post,
|
|
|
|
})
|
2013-09-24 13:54:41 +00:00
|
|
|
|
2013-10-02 19:49:55 +00:00
|
|
|
def codechunk(request, language, slug):
|
2014-04-21 08:39:43 +00:00
|
|
|
chunk = get_object_or_404(CodeChunk, language = language, slug = slug)
|
2013-10-02 19:49:55 +00:00
|
|
|
return render(request, 'blog/code-chunk.html', {'codechunk': chunk})
|
|
|
|
|
2013-09-24 13:54:41 +00:00
|
|
|
def feed(request):
|
2014-06-15 08:06:55 +00:00
|
|
|
latest_post = Post.objects.filter(draft = False).order_by('-created_at')[0:1]
|
|
|
|
|
|
|
|
if not latest_post:
|
|
|
|
latest_date = datetime.datetime(1983, 3, 7, 11, 54, 45, 0, pytz.timezone('Europe/Budapest'))
|
|
|
|
else:
|
|
|
|
latest_date = latest_post[0].created_at
|
|
|
|
|
|
|
|
posts = Post.objects.order_by('-created_at')[:10]
|
|
|
|
|
|
|
|
return render(request, 'blog/feed.xml', {
|
|
|
|
'site_url': request.build_absolute_uri(reverse('home')),
|
|
|
|
'profile_pic': request.build_absolute_uri(static('images/profile.png')),
|
|
|
|
'last_build_date': latest_date.strftime('%a, %d %b %Y %T %z'),
|
|
|
|
'posts': posts,
|
|
|
|
},
|
|
|
|
content_type = 'application/xml'
|
|
|
|
)
|
2013-09-24 13:54:41 +00:00
|
|
|
|