Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
django-medusa renderer.py example files (these are the ones that power my blog)
# mt3/renderers.py
from django_medusa.renderers import StaticSiteRenderer
class HomeRenderer(StaticSiteRenderer):
def get_paths(self):
return frozenset([
"/",
"/about/",
"/colophon/",
"/contact/",
"/portfolio/",
"/feeds/blog/",
"/sitemap.xml",
"/500.html",
"/sopa/",
"/onionbrowser/",
"/onionbrowser/help/",
])
renderers = [HomeRenderer, ]
# mt3/blog/renderers.py
from django_medusa.renderers import StaticSiteRenderer
from mt3.blog.models import BlogPost
from datetime import datetime
from django.core.urlresolvers import reverse
from math import ceil
class BlogHomeRenderer(StaticSiteRenderer):
def get_paths(self):
paths = set(["/blog/", ])
items = BlogPost.objects.filter(
is_live=True, pubdate__lte=datetime.now()).order_by('-pubdate')
num_items = 0
for item in items.values('slug', 'pubdate').iterator():
item_slug, item_pubdate = item['slug'], item['pubdate']
num_items += 1
paths.add(reverse(
'blog:post',
args=(
item_pubdate.year,
item_pubdate.strftime('%m'),
item_pubdate.strftime('%d'),
item_slug
)
))
# Getto way of forcing in every "dated based archive" view in.
# Since paths is an unordered set, we can throw these in blindly.
paths.add(reverse('blog:archive_day', args=(
item_pubdate.year, item_pubdate.strftime('%m'),
item_pubdate.strftime('%d')
)))
paths.add(reverse('blog:archive_month', args=(
item_pubdate.year, item_pubdate.strftime('%m')
)))
paths.add(reverse('blog:archive_year', args=(item_pubdate.year,)))
# Currently hardcoded: 5 blog posts per pagination
pages = int(ceil(num_items / 5.0))
for pgno in xrange(2, pages + 1):
url = reverse('blog:archive', args=(pgno,))
paths.add(url)
return list(paths)
renderers = [BlogHomeRenderer, ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment