Source

cx / apps / blog / context.py

Full commit
from django.core.cache import cache
from cx.apps.blog.models import Post
from datetime import datetime, date, timedelta

def archive(request):
    c = cache.get('blog_archive')
    if c is not None:
        return c
        
    posts_timeline = { }
        
    posts = Post.objects.order_by('-created_on')

    for post in posts:
        year = post.created_on.year
        month = 13-post.created_on.month
        posts_timeline.setdefault(year, { })
        posts_timeline[year].setdefault(month, [ ])
        posts_timeline[year][month].append(post.created_on)

    archive = { 'archive': posts_timeline }
    
    cache.set('blog_archive', archive)
    
    return archive