Merge branch 'master' into django2
[mygpo.git] / mygpo / web / templatetags / menu.py
blob72da8aea2c14594ba4ad31fe811dedadb4371e95
1 from django import template
2 from django.utils.safestring import mark_safe
3 from django.utils.translation import ugettext
4 from django.utils.translation import ugettext_lazy as _
8 register = template.Library()
10 HIDDEN_URIS = (
11 '/login/',
12 '/register/',
13 '/podcast/',
14 '/device/',
15 '/user/subscriptions/',
16 '/publisher/podcast/',
17 '/share/me',
20 MENU_STRUCTURE = (
21 ('', (
22 ('/', _('Home')),
23 ('/login/', _('Login')),
24 ('/register/', _('Register')),
25 ('', _('Docs')),
26 ('/contribute/', _('Contribute')),
27 ('/developer/', _('Development')),
28 ('/privacy/', _('Privacy Policy')),
29 ('/online-help', _('Help')),
30 )),
31 (_('Discover'), (
32 ('/directory/', _('Directory')),
33 ('/podcast/', _('Podcast')),
34 ('/search/', _('Search')),
35 ('/missing/', _('Missing Podcast')),
36 ('/lists/', _('Podcast Lists')),
37 ('/user/subscriptions/', _('User subscriptions')),
38 ('/suggestions/', _('Suggestions')),
39 ('', _('Features')),
40 ('/directory/+license', _('License')),
41 ('', _('Toplists')),
42 ('/toplist/', _('Podcasts')),
43 ('/toplist/episodes', _('Episodes')),
44 )),
45 (_('Subscriptions'), (
46 ('/subscriptions/', _('Subscriptions')),
47 ('/favorites/', _('Favorite Episodes')),
48 ('/tags/', _('My Tags')),
49 ('/devices/', _('Devices')),
50 ('/device/', _('Device')),
51 ('/history/', _('History')),
52 )),
53 (_('Community'), (
54 ('/share/', _('Overview')),
55 ('/share/favorites', _('Favorite Episodes')),
56 ('/share/me', _('My Userpage')),
57 ('/user/subscriptions/', _('Subscriptions')),
58 ('/share/lists/', _('Podcast Lists')),
59 )),
60 (_('Settings'), (
61 ('/account/', _('Account')),
62 ('/account/privacy', _('Privacy')),
63 )),
64 (_('Publish'), (
65 ('/publisher/', _('Home')),
66 ('/publisher/advertise', _('Advertise')),
67 ('/publisher/link/', _('Link to gpodder.net')),
68 ('/publisher/podcast/', _('Podcast')),
69 )),
72 @register.filter
73 def main_menu(selected):
74 found_section = False
75 links = []
76 for label, items in MENU_STRUCTURE[1:]:
77 uris = [uri for uri, caption in items]
78 if selected in uris:
79 found_section = True
80 links.append((items[0][0], label, uris))
82 items = []
83 for uri, caption, subpages in links:
84 if selected in subpages or ('/' in subpages and not found_section):
85 items.append('<li class="active"><a href="%s">%s</a></li>' % \
86 (uri, ugettext(caption)))
87 else:
88 items.append('<li><a href="%s">%s</a></li>' % (uri, ugettext(caption)))
90 return mark_safe('\n'.join(items))
92 def get_section_items(selected):
93 for label, items in MENU_STRUCTURE:
94 if selected in (uri for uri, caption in items):
95 return items
97 # If we arrive here, we cannot find the page items, so return a faked one
98 return list(MENU_STRUCTURE[0][1]) + [
99 (selected, selected),
102 @register.filter
103 def section_menu(selected, title=None):
105 items = []
106 for uri, caption in get_section_items(selected):
107 if uri == selected:
108 if title is not None:
109 if len(title) > 35:
110 title = title[:33] + '...'
111 caption = title
112 if uri in HIDDEN_URIS:
113 items.append('<li class="active"><a href="">%s</a></li>' % ugettext(caption))
114 elif uri == '':
115 items.append('<li class="disabled nav-header"><a href="">%s</a></li>' % ugettext(caption))
116 else:
117 items.append('<li class="active"><a href="%s">%s</a></li>' % \
118 (uri, ugettext(caption)))
119 else:
120 if uri in HIDDEN_URIS:
121 continue
123 if not uri:
124 items.append('<li class="disabled nav-header"><a>%s</a></li>' % ugettext(caption))
125 else:
126 items.append('<li><a href="%s">%s</a></li>' % (uri, ugettext(caption)))
128 return mark_safe('\n'.join(items))