moved context_processor from plinth.py to a separate file; removed two unnecessary context variables: 1) use cfg.main_menu instead of main_menu; 2) request_path is available automatically

This commit is contained in:
fonfon 2014-07-11 02:36:25 +03:00
parent 0d0c9f7bb0
commit 1cf1cb2e90
6 changed files with 51 additions and 31 deletions

30
context_processors.py Normal file
View File

@ -0,0 +1,30 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import re
import cfg
def plinth_processor(request):
"""Add additional context values to RequestContext for use in templates"""
slash_indizes = [m.start() for m in re.finditer('/', request.path)]
active_menu_urls = [request.path[:index+1] for index in slash_indizes]
return {
'cfg': cfg,
'submenu': cfg.main_menu.active_item(request),
'active_menu_urls': active_menu_urls
}

View File

@ -1,3 +1,20 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
""" Plinth project specific errors """

12
menu.py
View File

@ -1,8 +1,5 @@
from django.core.urlresolvers import reverse
import util
import cfg
class Menu(object):
"""One menu item."""
@ -61,15 +58,6 @@ class Menu(object):
self.sort_items()
return item
def is_active(self, request_path):
"""
Returns True if this menu item is active, otherwise False.
We can tell if a menu is active if the menu item points
anywhere above url we are visiting in the url tree.
"""
return request_path.startswith(self.url)
def active_item(self, request):
"""Return the first active item (e.g. submenu) that is found"""
for item in self.items:

View File

@ -22,7 +22,6 @@ URLs for the Lib module
from django.conf.urls import patterns, url
from django.core.urlresolvers import reverse_lazy
import cfg
urlpatterns = patterns( # pylint: disable-msg=C0103
'',

View File

@ -110,25 +110,11 @@ def setup_server():
cherrypy.engine.signal_handler.subscribe()
def context_processor(request):
"""Add additional context values to RequestContext for use in templates"""
path_parts = request.path.split('/')
active_menu_urls = ['/'.join(path_parts[:length])
for length in xrange(1, len(path_parts))]
return {
'cfg': cfg,
'main_menu': cfg.main_menu,
'submenu': cfg.main_menu.active_item(request),
'request_path': request.path,
'active_menu_urls': active_menu_urls
}
def configure_django():
"""Setup Django configuration in the absense of .settings file"""
# In module_loader.py we reverse URLs for the menu without having a proper
# request. In this case, get_script_prefix (used by reverse) returns the
# request. In this case, get_script_prefix (used by reverse) returns the
# wrong prefix. Set it here manually to have the correct prefix right away.
django.core.urlresolvers.set_script_prefix(cfg.server_dir)
@ -140,7 +126,7 @@ def configure_django():
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'plinth.context_processor']
'context_processors.plinth_processor']
logging_configuration = {
'version': 1,
@ -188,7 +174,7 @@ def configure_django():
LOGIN_URL='lib:login',
LOGIN_REDIRECT_URL='apps:index',
LOGOUT_URL='lib:logout',
MIDDLEWARE_CLASSES = (
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',

View File

@ -67,7 +67,7 @@
{% block add_nav_and_login %}
<div class="nav-collapse">
<ul class="nav">
{% for item in main_menu.items %}
{% for item in cfg.main_menu.items %}
{% if item.url in active_menu_urls %}
<li class="active">
<a href="{{ item.url }}" class="active">