From 1cf1cb2e90207067ed43afa8f9580b60912bfd8a Mon Sep 17 00:00:00 2001 From: fonfon Date: Fri, 11 Jul 2014 02:36:25 +0300 Subject: [PATCH] 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 --- context_processors.py | 30 ++++++++++++++++++++++++++++++ errors.py | 17 +++++++++++++++++ menu.py | 12 ------------ modules/lib/urls.py | 1 - plinth.py | 20 +++----------------- templates/base.html | 2 +- 6 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 context_processors.py diff --git a/context_processors.py b/context_processors.py new file mode 100644 index 000000000..6c5c77d6b --- /dev/null +++ b/context_processors.py @@ -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 . +# + +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 + } diff --git a/errors.py b/errors.py index e6b320084..b46661354 100644 --- a/errors.py +++ b/errors.py @@ -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 . +# + """ Plinth project specific errors """ diff --git a/menu.py b/menu.py index be526ae03..99a908ead 100644 --- a/menu.py +++ b/menu.py @@ -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: diff --git a/modules/lib/urls.py b/modules/lib/urls.py index 02321e7b8..2ff9a80b9 100644 --- a/modules/lib/urls.py +++ b/modules/lib/urls.py @@ -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 '', diff --git a/plinth.py b/plinth.py index 816bc977b..6027ea893 100755 --- a/plinth.py +++ b/plinth.py @@ -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', diff --git a/templates/base.html b/templates/base.html index 5848f7bd0..b0e686e8b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -67,7 +67,7 @@ {% block add_nav_and_login %}