Skip to content

Instantly share code, notes, and snippets.

@timmyomahony
Created January 26, 2012 00:40
Show Gist options
  • Save timmyomahony/1680051 to your computer and use it in GitHub Desktop.
Save timmyomahony/1680051 to your computer and use it in GitHub Desktop.
cmsplugin-blog menu for use with breadcrumb navigation
import itertools
from time import strftime
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from menus.menu_pool import menu_pool
from menus.base import NavigationNode
from cms.menu_bases import CMSAttachMenu
from cmsplugin_blog.models import Entry, EntryTitle
from tagging.models import Tag, TaggedItem
def ordinal(n):
if 10 <= n % 100 < 20:
return str(n) + 'th'
else:
return str(n) + {1 : 'st', 2 : 'nd', 3 : 'rd'}.get(n % 10, "th")
class BlogMenu(CMSAttachMenu):
name = _("Blog Menu")
def get_nodes(self, request):
c=itertools.count()
menu = []
dates = authors = tags = dict()
root = next(c)
# Put each entry into buckets according to date
for entry in Entry.published.all().order_by('-pub_date'):
y = dates.setdefault(entry.pub_date.year, dict())
m_slug, m_eng = entry.pub_date.strftime("%m"), entry.pub_date.strftime("%b")
m = y.setdefault("%s,%s" % (m_slug, m_eng), dict())
d_slug = entry.pub_date.strftime("%d")
d_eng = "%s" % (ordinal(int(d_slug)).lstrip("0"))
d = m.setdefault("%s,%s" % (d_slug, d_eng), [])
d.append(entry)
# Now go depth first and add menu nodes
for k_y,v_y in dates.items():
c_y = next(c)
menu.append(NavigationNode(_(str(k_y)), reverse("blog_archive_year", kwargs={ 'year' : k_y }) , c_y, parent_id=root))
for k_m, v_m in v_y.items():
c_m = next(c)
menu.append(NavigationNode(_(str(k_m.split(",")[1])), reverse("blog_archive_month", kwargs={ 'year' : k_y, 'month' : k_m.split(',')[0] }), c_m, parent_id=c_y))
for k_d, v_d in v_m.items():
#c_d = next(c)
#menu.append(NavigationNode(_(k_d.split(",")[1]), reverse("blog_archive_day", kwargs={ 'year' : k_y, 'month' : k_m.split(',')[0] , 'day' : k_d.split(',')[0]}), c_d, parent_id=c_m))
for l in v_d:
for title in l.entrytitle_set.all():
menu.append(NavigationNode(_("'%s'"%title.title), reverse("blog_detail", kwargs={ 'year' : k_y, 'month' : k_m.split(',')[0] , 'day' : k_d.split(',')[0], 'slug' : title.slug}), next(c), parent_id=c_m))
# Authors
c_a = next(c)
menu.append(NavigationNode(_("Author"), reverse("blog_archive_index") , c_a, parent_id=root))
for user in User.objects.all():
menu.append(NavigationNode(_(user.username), reverse("blog_archive_author", kwargs={ 'author' : user.username }) , next(c), parent_id=c_a))
return menu
menu_pool.register_menu(BlogMenu)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment