mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-18 08:33:41 +00:00
menu and submenu now also uses djangos named urls and url-reverse
This commit is contained in:
parent
809dc4130a
commit
5150efac5f
30
menu.py
30
menu.py
@ -1,3 +1,5 @@
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
import util
|
||||
import cfg
|
||||
|
||||
@ -28,32 +30,32 @@ class Menu(object):
|
||||
self.order = order
|
||||
self.items = []
|
||||
|
||||
def find(self, url, basehref=True):
|
||||
"""Return a menu item with given URL"""
|
||||
if basehref and url.startswith('/'):
|
||||
url = util.rel_urljoin([cfg.server_dir, url])
|
||||
|
||||
def get(self, urlname, url_args=None, url_kwargs=None):
|
||||
"""Return a menu item with given URL name"""
|
||||
url = reverse(urlname, args=url_args, kwargs=url_kwargs)
|
||||
url = util.rel_urljoin([cfg.server_dir, url])
|
||||
for item in self.items:
|
||||
if item.url == url:
|
||||
return item
|
||||
|
||||
raise KeyError('Menu item not found')
|
||||
|
||||
def sort_items(self):
|
||||
"""Sort the items in self.items by order."""
|
||||
self.items = sorted(self.items, key=lambda x: x.order, reverse=False)
|
||||
|
||||
def add_item(self, label, icon, url, order=50, basehref=True):
|
||||
def add_urlname(self, label, icon, urlname, order=50, url_args=None,
|
||||
url_kwargs=None):
|
||||
""" Add a named URL to the menu (via add_item)
|
||||
url_args and url_kwargs will be passed on to url reverse """
|
||||
url = reverse(urlname, args=url_args, kwargs=url_kwargs)
|
||||
return self.add_item(label, icon, url, order, add_url_prefix=True)
|
||||
|
||||
def add_item(self, label, icon, url, order=50, add_url_prefix=False):
|
||||
"""This method creates a menu item with the parameters, adds
|
||||
that menu item to this menu, and returns the item.
|
||||
|
||||
If BASEHREF is true and url start with a slash, prepend the
|
||||
cfg.server_dir to it"""
|
||||
|
||||
if basehref and url.startswith("/"):
|
||||
"""
|
||||
if add_url_prefix:
|
||||
url = util.rel_urljoin([cfg.server_dir, url])
|
||||
#url = cfg.server_dir + url
|
||||
|
||||
item = Menu(label=label, icon=icon, url=url, order=order)
|
||||
self.items.append(item)
|
||||
self.sort_items()
|
||||
|
||||
@ -6,7 +6,7 @@ import cfg
|
||||
|
||||
def init():
|
||||
"""Initailize the apps module"""
|
||||
cfg.main_menu.add_item("Apps", "icon-download-alt", "/apps", 80)
|
||||
cfg.main_menu.add_urlname("Apps", "icon-download-alt", "apps:index", 80)
|
||||
|
||||
|
||||
def index(request):
|
||||
|
||||
@ -96,8 +96,8 @@ and must not be greater than 63 characters in length.'),
|
||||
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Configure'), 'icon-cog', '/sys/config', 10)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Configure'), 'icon-cog', 'config:index', 10)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -30,8 +30,8 @@ from errors import ActionError
|
||||
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item("Diagnostics", "icon-screenshot", "/sys/diagnostics", 30)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname("Diagnostics", "icon-screenshot", "diagnostics:index", 30)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -16,8 +16,8 @@ class ExpertsForm(forms.Form): # pylint: disable-msg=W0232
|
||||
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Expert Mode'), 'icon-cog', '/sys/expert', 10)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Expert Mode'), 'icon-cog', 'expert_mode:index', 10)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -34,8 +34,8 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def init():
|
||||
"""Initailze firewall module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Firewall'), 'icon-flag', '/sys/firewall', 50)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Firewall'), 'icon-flag', 'firewall:index', 50)
|
||||
|
||||
service_module.ENABLED.connect(on_service_enabled)
|
||||
|
||||
|
||||
@ -8,15 +8,17 @@ import cfg
|
||||
|
||||
def init():
|
||||
"""Initialize the Help module"""
|
||||
menu = cfg.main_menu.add_item(_('Documentation'), 'icon-book',
|
||||
'/help/index', 101)
|
||||
menu.add_item(_("Where to Get Help"), "icon-search", "/help/index", 5)
|
||||
menu.add_item(_('Developer\'s Manual'), 'icon-info-sign',
|
||||
'/help/page/plinth', 10)
|
||||
menu.add_item(_('FAQ'), 'icon-question-sign', '/help/page/faq', 20)
|
||||
menu = cfg.main_menu.add_urlname(_('Documentation'), 'icon-book',
|
||||
'help:index', 101)
|
||||
menu.add_urlname(_("Where to Get Help"), "icon-search",
|
||||
"help:index_explicit", 5)
|
||||
menu.add_urlname(_('Developer\'s Manual'), 'icon-info-sign',
|
||||
'help:helppage', 10, url_args=('plinth',))
|
||||
menu.add_urlname(_('FAQ'), 'icon-question-sign', 'help:helppage', 20,
|
||||
url_args=('faq',))
|
||||
menu.add_item(_('%s Wiki' % cfg.box_name), 'icon-pencil',
|
||||
'http://wiki.debian.org/FreedomBox', 30)
|
||||
menu.add_item(_('About'), 'icon-star', '/help/about', 100)
|
||||
menu.add_urlname(_('About'), 'icon-star', 'help:about', 100)
|
||||
|
||||
|
||||
def index(request):
|
||||
|
||||
@ -20,14 +20,15 @@ URLs for the Help module
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.views.generic import RedirectView
|
||||
|
||||
|
||||
urlpatterns = patterns( # pylint: disable-msg=C0103
|
||||
'modules.help.help',
|
||||
url(r'^help/$', RedirectView.as_view(url=reverse_lazy('help:index'))),
|
||||
url(r'^help/index/$', 'index', name='index'),
|
||||
# having two urls for one page is a hack to help the current url/menu
|
||||
# system highlight the correct menu item. Every submenu-item with the same
|
||||
# url prefix as the main-menu is highlighted automatically.
|
||||
url(r'^help/$', 'index', name='index'),
|
||||
url(r'^help/index/$', 'index', name='index_explicit'),
|
||||
url(r'^help/about/$', 'about', name='about'),
|
||||
url(r'^help/page/([\w]+)/$', 'helppage', name='helppage'),
|
||||
)
|
||||
|
||||
@ -19,8 +19,8 @@ class OwnCloudForm(forms.Form): # pylint: disable-msg=W0232
|
||||
|
||||
def init():
|
||||
"""Initialize the ownCloud module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item('Owncloud', 'icon-picture', '/apps/owncloud', 35)
|
||||
menu = cfg.main_menu.get('apps:index')
|
||||
menu.add_urlname('Owncloud', 'icon-picture', 'owncloud:index', 35)
|
||||
|
||||
status = get_status()
|
||||
|
||||
|
||||
@ -37,8 +37,8 @@ class PackagesForm(forms.Form):
|
||||
|
||||
def init():
|
||||
"""Initialize the Packages module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item('Package Manager', 'icon-gift', '/sys/packages', 20)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname('Package Manager', 'icon-gift', 'packages:index', 20)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -38,9 +38,9 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def init():
|
||||
"""Intialize the PageKite module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item(_('Public Visibility (PageKite)'), 'icon-flag',
|
||||
'/apps/pagekite', 50)
|
||||
menu = cfg.main_menu.get('apps:index')
|
||||
menu.add_urlname(_('Public Visibility (PageKite)'), 'icon-flag',
|
||||
'pagekite:index', 50)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -6,7 +6,7 @@ import cfg
|
||||
|
||||
def init():
|
||||
"""Initialize the system module"""
|
||||
cfg.main_menu.add_item(_('System'), 'icon-cog', '/sys', 100)
|
||||
cfg.main_menu.add_urlname(_('System'), 'icon-cog', 'system:index', 100)
|
||||
|
||||
|
||||
def index(request):
|
||||
|
||||
@ -29,8 +29,8 @@ import cfg
|
||||
|
||||
def init():
|
||||
"""Initialize the Tor module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item("Tor", "icon-eye-close", "/apps/tor", 30)
|
||||
menu = cfg.main_menu.get('apps:index')
|
||||
menu.add_urlname("Tor", "icon-eye-close", "tor:index", 30)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -18,8 +18,8 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def init():
|
||||
"""Intialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Users and Groups'), 'icon-user', '/sys/users', 15)
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Users and Groups'), 'icon-user', 'users:index', 15)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
from django import forms
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.template import RequestContext
|
||||
from django.template.loader import render_to_string
|
||||
from django.template.response import TemplateResponse
|
||||
@ -14,18 +15,26 @@ import service
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SIDE_MENU = {'title': _('XMPP'),
|
||||
'items': [{'url': '/apps/xmpp/configure',
|
||||
'text': 'Configure XMPP Server'},
|
||||
{'url': '/apps/xmpp/register',
|
||||
'text': 'Register XMPP Account'}]}
|
||||
SIDE_MENU = {
|
||||
'title': _('XMPP'),
|
||||
'items': [
|
||||
{
|
||||
'url': reverse_lazy('xmpp:configure'),
|
||||
'text': 'Configure XMPP Server'
|
||||
},
|
||||
{
|
||||
'url': reverse_lazy('xmpp:register'),
|
||||
'text': 'Register XMPP Account'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the XMPP module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu = cfg.main_menu.get('apps:index')
|
||||
menu.add_item('Chat', 'icon-comment', '/../jwchat', 20)
|
||||
menu.add_item('XMPP', 'icon-comment', '/apps/xmpp', 40)
|
||||
menu.add_urlname('XMPP', 'icon-comment', 'xmpp:index', 40)
|
||||
|
||||
service.Service(
|
||||
'xmpp-client', _('Chat Server - client connections'),
|
||||
|
||||
@ -120,7 +120,6 @@ def context_processor(request):
|
||||
'main_menu': cfg.main_menu,
|
||||
'submenu': cfg.main_menu.active_item(request),
|
||||
'request_path': request.path,
|
||||
'basehref': cfg.server_dir,
|
||||
'active_menu_urls': active_menu_urls
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<li class='nav-header'>{{ menu.title }}</li>
|
||||
{% for item in menu.items %}
|
||||
<li>
|
||||
<a href="{{ basehref }}{{ item.url }}">{{ item.text }}</a>
|
||||
<a href="{{ item.url }}">{{ item.text }}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user