marking episodes as favorites, added favorite-list
[mygpo.git] / mygpo / web / templatetags / menu.py
blob9ed85a587b784e404f6e7b9a891f74167cefdb40
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 ('/favorites/', _('Favorite Episodes')),
26 ('/history/', _('History')),
27 ('/suggestions/', _('Suggestions')),
28 )),
29 (_('My Devices'), (
30 ('/devices/', _('Overview')),
31 ('/device/', _('Device')),
32 )),
33 (_('Podcast Directory'), (
34 ('/toplist/', _('Toplist')),
35 ('/search/', _('Search')),
36 ('/toplist/episodes', _('Episodes')),
37 ('/podcast/', _('Podcast')),
38 ('/user/subscriptions/', _('User subscriptions')),
39 )),
40 (_('Settings'), (
41 ('/account/', _('Account')),
42 ('/account/privacy', _('Privacy')),
43 ('/share/', _('Sharing')),
44 )),
45 (_('Publisher'), (
46 ('/publisher/', _('Home')),
47 ('/publisher/link/', _('Link to gpodder.net')),
48 ('/publisher/podcast/', _('Podcast')),
49 )),
52 @register.filter
53 def main_menu(selected):
54 found_section = False
55 links = []
56 for label, items in MENU_STRUCTURE:
57 uris = [uri for uri, caption in items]
58 if selected in uris:
59 found_section = True
60 links.append((items[0][0], label, uris))
62 items = []
63 for uri, caption, subpages in links:
64 if selected in subpages or ('/' in subpages and not found_section):
65 items.append('<li class="selected"><a href="%s">%s</a></li>' % \
66 (uri, caption))
67 else:
68 items.append('<li><a href="%s">%s</a></li>' % (uri, caption))
70 s = '<ul class="menu primary">%s</ul>' % ('\n'.join(items),)
71 return mark_safe(s)
73 def get_section_items(selected):
74 for label, items in MENU_STRUCTURE:
75 if selected in (uri for uri, caption in items):
76 return items
78 # If we arrive here, we cannot find the page items, so return a faked one
79 return list(MENU_STRUCTURE[0][1]) + [
80 (selected, selected),
83 @register.filter
84 def section_menu(selected, title=None):
85 items = []
86 for uri, caption in get_section_items(selected):
87 if uri == selected:
88 if title is not None:
89 if len(title) > 35:
90 title = title[:33] + '...'
91 caption = title
92 if uri in HIDDEN_URIS:
93 items.append('<li class="selected">%s</li>' % caption)
94 else:
95 items.append('<li class="selected"><a href="%s">%s</a></li>' % \
96 (uri, caption))
97 elif uri not in HIDDEN_URIS:
98 items.append('<li><a href="%s">%s</a></li>' % (uri, caption))
100 s = '<ul class="menu secondary">%s</ul>' % ('\n'.join(items),)
101 return mark_safe(s)