apply new navigation to publisher pages
[mygpo.git] / mygpo / web / templatetags / menu.py
blobc6abb6dad88ae08262bfbb9ced3d33a238674318
1 from django import template
2 from django.utils.safestring import mark_safe
3 from django.utils.translation import ugettext as _
5 register = template.Library()
7 HIDDEN_URIS = (
8 '/login/',
9 '/register/',
10 '/podcast/',
11 '/device/',
12 '/user/subscriptions/',
13 '/publisher/podcast/',
16 MENU_STRUCTURE = (
17 ('gpodder.net', (
18 ('/', _('Home')),
19 ('/login/', _('Login')),
20 ('/register/', _('Register')),
21 ('/online-help', _('Help')),
22 )),
23 (_('My Podcasts'), (
24 ('/subscriptions/', _('Subscriptions')),
25 ('/history/', _('History')),
26 ('/suggestions/', _('Suggestions')),
27 )),
28 (_('My Devices'), (
29 ('/devices/', _('Overview')),
30 ('/device/', _('Device')),
31 )),
32 (_('Podcast Directory'), (
33 ('/toplist/', _('Toplist')),
34 ('/search/', _('Search')),
35 ('/toplist/episodes', _('Episodes')),
36 ('/podcast/', _('Podcast')),
37 ('/user/subscriptions/', _('User subscriptions')),
38 )),
39 (_('Settings'), (
40 ('/account/', _('Account')),
41 ('/account/privacy', _('Privacy')),
42 ('/share/', _('Sharing')),
43 )),
44 (_('Publisher'), (
45 ('/publisher/', _('Home')),
46 ('/publisher/link/', _('Link to gpodder.net')),
47 ('/publisher/podcast/', _('Podcast')),
48 )),
51 @register.filter
52 def main_menu(selected):
53 found_section = False
54 links = []
55 for label, items in MENU_STRUCTURE:
56 uris = [uri for uri, caption in items]
57 if selected in uris:
58 found_section = True
59 links.append((items[0][0], label, uris))
61 items = []
62 for uri, caption, subpages in links:
63 if selected in subpages or ('/' in subpages and not found_section):
64 items.append('<li class="selected"><a href="%s">%s</a></li>' % \
65 (uri, caption))
66 else:
67 items.append('<li><a href="%s">%s</a></li>' % (uri, caption))
69 s = '<ul class="menu primary">%s</ul>' % ('\n'.join(items),)
70 return mark_safe(s)
72 def get_section_items(selected):
73 for label, items in MENU_STRUCTURE:
74 if selected in (uri for uri, caption in items):
75 return items
77 # If we arrive here, we cannot find the page items, so return a faked one
78 return list(MENU_STRUCTURE[0][1]) + [
79 (selected, selected),
82 @register.filter
83 def section_menu(selected, title=None):
84 items = []
85 for uri, caption in get_section_items(selected):
86 if uri == selected:
87 if title is not None:
88 if len(title) > 35:
89 title = title[:33] + '...'
90 caption = title
91 if uri in HIDDEN_URIS:
92 items.append('<li class="selected">%s</li>' % caption)
93 else:
94 items.append('<li class="selected"><a href="%s">%s</a></li>' % \
95 (uri, caption))
96 elif uri not in HIDDEN_URIS:
97 items.append('<li><a href="%s">%s</a></li>' % (uri, caption))
99 s = '<ul class="menu secondary">%s</ul>' % ('\n'.join(items),)
100 return mark_safe(s)