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