Refactor the sidebar to use Django templates
With this change the html for the sidebar is no longer generated by
Melange, instead it's delegated to Django (which is what it does
best anyway). The downside is that it is no longer possible to have
arbitrary deeply nested menu's.
--- a/app/soc/templates/soc/base.html Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/templates/soc/base.html Sun Nov 16 23:02:31 2008 +0000
@@ -108,8 +108,8 @@
Google Open Source Programs
{% endblock %}
</a>
-{% if sidebar_menu_html %}
-{{ sidebar_menu_html|safe }}
+{% if sidebar_menu_items %}
+{% include 'soc/sidebar/sidebar.html' %}
{% endif %}
</li>
</ul>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/sidebar/menuitem.html Sun Nov 16 23:02:31 2008 +0000
@@ -0,0 +1,3 @@
+<li class="leaf">
+<span><a href="{{ item.url }}">{{ item.title }}</a></span>
+</li>
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/sidebar/sidebar.html Sun Nov 16 23:02:31 2008 +0000
@@ -0,0 +1,5 @@
+<ul>
+{% for subitem in sidebar_menu_items %}
+{% include 'soc/sidebar/submenu.html' %}
+{% endfor %}
+</ul>
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/sidebar/submenu.html Sun Nov 16 23:02:31 2008 +0000
@@ -0,0 +1,8 @@
+<li class="expandable">
+<span>{{ subitem.heading }}</span>
+<ul>
+{% for item in subitem.items %}
+{% include 'soc/sidebar/menuitem.html' %}
+{% endfor %}
+</ul>
+</li>
\ No newline at end of file
--- a/app/soc/views/helper/responses.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/helper/responses.py Sun Nov 16 23:02:31 2008 +0000
@@ -31,9 +31,8 @@
from soc.logic import accounts
from soc.logic import system
from soc.logic.models import site_settings
-from soc.logic.site import sidebar
from soc.views import helper
-from soc.views.helper import html_menu
+from soc.views.sitemap import sidebar
import soc.logic
import soc.logic.models.user
@@ -106,9 +105,8 @@
context['is_debug'] = system.isDebug()
context['sign_in'] = users.create_login_url(request.path)
context['sign_out'] = users.create_logout_url(request.path)
- context['sidebar_menu_html'] = str(html_menu.UlMenu(
- sidebar.buildSidebar(**context)))
-
+ context['sidebar_menu_items'] = sidebar.SIDEBAR
+
settings = site_settings.logic.getFromFields(
partial_path=site_settings.logic.DEF_SITE_SETTINGS_PARTIAL_PATH,
link_name=site_settings.logic.DEF_SITE_SETTINGS_LINK_NAME)
--- a/app/soc/views/models/base.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/base.py Sun Nov 16 23:02:31 2008 +0000
@@ -39,7 +39,7 @@
from soc.views.helper import access
-class View:
+class View(object):
"""Views for entity classes.
The View class functions specific to Entity classes by relying
@@ -77,6 +77,8 @@
create_redirect: the Django template to redirect to after creation
save_message: the message to display when the entity is saved
edit_params: the params to use when editing
+ sidebar: the sidebar menu items for this view
+ sidebar_defaults: a dictionary with defaults for the sidebar
"""
new_rights = {}
@@ -86,6 +88,13 @@
new_params['create_redirect'] = '/%s' % params['name_short'].lower()
new_params['missing_redirect'] = '/%s/create' % params['name_short'].lower()
+ new_params['sidebar'] = None
+ new_params['sidebar_defaults'] = [
+ ('/%s/create', 'New %(name)s'),
+ ('/%s/list', 'List %(plural)s'),
+ ]
+ new_params['sidebar_additional'] = []
+
self._rights = dicts.merge(rights, new_rights)
self._params = dicts.merge(params, new_params)
@@ -472,3 +481,54 @@
fields[field] = value
return key_name, fields
+
+ def _getSidebarItems(self, params):
+ """Retrieves a list of sidebar entries for this view from self._params
+
+ If params['sidebar'] is None default entries will be constructed
+ """
+
+ # Return the found result
+ if params['sidebar']:
+ return params['sidebar']
+
+ # Construct defaults manualy
+ defaults = params['sidebar_defaults']
+
+ result = []
+
+ for url, title in defaults:
+ url = url % params['name_short'].lower()
+
+ title = title % {
+ 'name': params['name'],
+ 'plural': params['name_plural']
+ }
+
+ item = (url, title)
+ result.append(item)
+
+ for item in params['sidebar_additional']:
+ result.append(item)
+
+ return result
+
+ def getSidebarLinks(self, params=None):
+ """Returns an dictionary with one sidebar entry
+
+ Args:
+ params: see __init__
+ """
+
+ params = dicts.merge(params, self._params)
+
+ items = []
+
+ for url, title in self._getSidebarItems(params):
+ items.append({'url': url, 'title': title})
+
+ res = {}
+ res['heading'] = params['name']
+ res['items'] = items
+
+ return res
--- a/app/soc/views/models/document.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/document.py Sun Nov 16 23:02:31 2008 +0000
@@ -34,6 +34,7 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
+from soc.views.sitemap import sidebar
import soc.models.document
import soc.logic.models.document
@@ -155,3 +156,5 @@
delete = view.delete
list = view.list
public = view.public
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/home_settings.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/home_settings.py Sun Nov 16 23:02:31 2008 +0000
@@ -34,6 +34,7 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
+from soc.views.sitemap import sidebar
import soc.models.home_settings
import soc.logic.models.home_settings
@@ -112,7 +113,7 @@
rights = {}
params['name'] = "Home Settings"
- params['name_short'] = "Home"
+ params['name_short'] = "home_settings"
params['name_plural'] = "Home Settings"
params['edit_form'] = EditForm
@@ -196,3 +197,5 @@
delete = view.delete
list = view.list
public = view.public
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/host.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/host.py Sun Nov 16 23:02:31 2008 +0000
@@ -27,6 +27,7 @@
from soc.logic import dicts
from soc.views import helper
from soc.views.models import base
+from soc.views.sitemap import sidebar
import soc.models.host
import soc.logic.models.host
@@ -139,3 +140,5 @@
edit = view.edit
list = view.list
public = view.public
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/site_settings.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/site_settings.py Sun Nov 16 23:02:31 2008 +0000
@@ -33,6 +33,7 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import home_settings
+from soc.views.sitemap import sidebar
import soc.models.site_settings
import soc.logic.models.site_settings
@@ -83,7 +84,7 @@
rights = {}
params['name'] = "Site Settings"
- params['name_short'] = "Site"
+ params['name_short'] = "site_settings"
params['name_plural'] = "Site Settings"
params['edit_form'] = EditForm
@@ -98,6 +99,8 @@
params['delete_redirect'] = '/site/list'
+ params['sidebar_additional'] = [('/site_settings/edit','Edit Main Site Settings')]
+
params = dicts.merge(original_params, params)
rights = dicts.merge(original_rights, rights)
@@ -145,3 +148,5 @@
public = view.public
main_public = view.main_public
main_edit = view.main_edit
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/sponsor.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/sponsor.py Sun Nov 16 23:02:31 2008 +0000
@@ -34,6 +34,7 @@
from soc.views import helper
from soc.views.helper import widgets
from soc.views.models import base
+from soc.views.sitemap import sidebar
import soc.models.sponsor
import soc.logic.models.sponsor
@@ -122,7 +123,7 @@
params['edit_params'] = {
self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
}
-
+
rights['list'] = [helper.access.checkIsDeveloper]
rights['delete'] = [helper.access.checkIsDeveloper]
@@ -148,3 +149,5 @@
edit = view.edit
list = view.list
public = view.public
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- a/app/soc/views/models/user.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/soc/views/models/user.py Sun Nov 16 23:02:31 2008 +0000
@@ -32,6 +32,7 @@
from soc.logic.models import user as user_logic
from soc.views import helper
from soc.views.models import base
+from soc.views.sitemap import sidebar
import soc.models.user
import soc.logic.models.user
@@ -192,6 +193,18 @@
# fill in the account field with the user created from email
fields['account'] = users.User(fields['email'])
+ def getUserSidebar(self):
+ """Returns an dictionary with the user sidebar entry
+ """
+
+ params = {}
+ params['name'] = "User (self)"
+ params['sidebar'] = [
+ ('/user/edit', 'Profile'),
+ ('/roles/list', 'Roles'),
+ ]
+ return self.getSidebarLinks(params)
+
view = View()
@@ -201,3 +214,5 @@
list = view.list
public = view.public
edit_self = view.editSelf
+
+sidebar.SIDEBAR.append(view.getSidebarLinks())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/views/sitemap/sidebar.py Sun Nov 16 23:02:31 2008 +0000
@@ -0,0 +1,30 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Sidebar
+"""
+
+__authors__ = [
+ '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+ ]
+
+
+SIDEBAR = []
+
+
+def addMenu(pages):
+ global SIDEBAR
+ SIDEBAR.append(pages)
--- a/app/urls.py Sun Nov 16 22:11:58 2008 +0000
+++ b/app/urls.py Sun Nov 16 23:02:31 2008 +0000
@@ -21,5 +21,13 @@
from soc.logic.site import map
+# TODO(SRabbelier) Do this dynamically
+import soc.views.models.document
+import soc.views.models.home_settings
+import soc.views.models.host
+import soc.views.models.site_settings
+import soc.views.models.sponsor
+import soc.views.models.user
+
urlpatterns = map.getDjangoUrlPatterns()