Make the sidebar dynamic depending on the user's rights
This is done by doing the usual access checks that are also called
when the user actually visits the page.
--- a/app/soc/views/helper/responses.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/helper/responses.py Tue Nov 25 17:19:37 2008 +0000
@@ -106,7 +106,7 @@
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_items'] = sidebar.getSidebar()
+ context['sidebar_menu_items'] = sidebar.getSidebar(request)
context['soc_release'] = release.RELEASE_TAG
context['gae_version'] = system.getAppVersion()
--- a/app/soc/views/models/base.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/models/base.py Tue Nov 25 17:19:37 2008 +0000
@@ -106,11 +106,10 @@
new_params['sidebar'] = None
new_params['sidebar_defaults'] = [
- ('/%s/create', 'New %(name)s'),
- ('/%s/list', 'List %(name_plural)s'),
+ ('/%s/create', 'New %(name)s', 'create'),
+ ('/%s/list', 'List %(name_plural)s', 'list'),
]
new_params['sidebar_additional'] = []
- new_params['sidebar_heading'] = None
new_params['key_fields_prefix'] = []
@@ -586,9 +585,9 @@
result = []
- for url, menu_text in defaults:
+ for url, menu_text, access_type in defaults:
url = url % params['url_name'].lower()
- item = (url, menu_text % params)
+ item = (url, menu_text % params, access_type)
result.append(item)
for item in params['sidebar_additional']:
@@ -596,7 +595,7 @@
return result
- def getSidebarLinks(self, params=None):
+ def getSidebarLinks(self, request, params=None):
"""Returns an dictionary with one sidebar entry.
Args:
@@ -604,15 +603,23 @@
"""
params = dicts.merge(params, self._params)
+ rights = params['rights']
items = []
- for url, menu_text in self._getSidebarItems(params):
- items.append({'url': url, 'title': menu_text})
+ for url, menu_text, access_type in self._getSidebarItems(params):
+ try:
+ self.checkAccess(access_type, request, rights)
+ items.append({'url': url, 'title': menu_text})
+ except out_of_band.Error:
+ pass
+
+ if not items:
+ return
res = {}
- if not params['sidebar_heading']:
+ if 'sidebar_heading' not in params:
params['sidebar_heading'] = params['name']
res['heading'] = params['sidebar_heading']
--- a/app/soc/views/models/presence.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/models/presence.py Tue Nov 25 17:19:37 2008 +0000
@@ -118,6 +118,9 @@
params['edit_form'] = EditForm
params['create_form'] = CreateForm
+ # Disable the presence sidebar until we have some use for it
+ params['sidebar_defaults'] = []
+
params = dicts.merge(original_params, params)
base.View.__init__(self, params=params)
--- a/app/soc/views/models/request.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/models/request.py Tue Nov 25 17:19:37 2008 +0000
@@ -35,6 +35,7 @@
from soc.logic.models import user as user_logic
from soc.views import helper
from soc.views import out_of_band
+from soc.views.helper import access
from soc.views.helper import widgets
from soc.views.models import base
from soc.views.models import role as role_view
@@ -111,7 +112,7 @@
params['edit_form'] = EditForm
params['create_form'] = CreateForm
- params['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s')]
+ params['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s', 'list')]
params['delete_redirect'] = '/' + params['url_name'] + '/list'
params['create_redirect'] = '/' + params['url_name']
--- a/app/soc/views/models/site.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/models/site.py Tue Nov 25 17:19:37 2008 +0000
@@ -92,7 +92,7 @@
params['edit_form'] = EditForm
params['create_form'] = CreateForm
- params['sidebar_defaults'] = [('/%s/edit', 'Edit %(name)s')]
+ params['sidebar_defaults'] = [('/%s/edit', 'Edit %(name)s', 'edit')]
params['sidebar_heading'] = params['name_short']
params['public_template'] = 'soc/home/public.html'
--- a/app/soc/views/models/user.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/models/user.py Tue Nov 25 17:19:37 2008 +0000
@@ -291,17 +291,17 @@
# fill in the account field with the user created from email
fields['account'] = users.User(fields['email'])
- def getUserSidebar(self):
+ def getUserSidebar(self, request):
"""Returns an dictionary with the user sidebar entry.
"""
params = {}
params['sidebar_heading'] = "User (self)"
params['sidebar'] = [
- ('/' + self._params['url_name'] + '/edit', 'Profile'),
- ('/' + self._params['url_name'] + '/roles', 'Roles'),
+ ('/' + self._params['url_name'] + '/edit', 'Profile', 'editSelf'),
+ ('/' + self._params['url_name'] + '/roles', 'Roles', 'roles'),
]
- return self.getSidebarLinks(params)
+ return self.getSidebarLinks(request, params)
def getDjangoURLPatterns(self):
"""See base.View.getDjangoURLPatterns().
--- a/app/soc/views/sitemap/sidebar.py Tue Nov 25 17:18:57 2008 +0000
+++ b/app/soc/views/sitemap/sidebar.py Tue Nov 25 17:19:37 2008 +0000
@@ -29,11 +29,11 @@
global SIDEBAR
SIDEBAR.append(callback)
-def getSidebar():
+def getSidebar(request):
sidebar = []
for callback in SIDEBAR:
- menu = callback()
+ menu = callback(request)
sidebar.append(menu)
return sidebar