app/soc/views/sitemap/sidebar.py
changeset 614 53a3e46fc512
parent 589 ee9122db04af
child 615 1d09147de51f
--- a/app/soc/views/sitemap/sidebar.py	Sat Nov 29 19:44:48 2008 +0000
+++ b/app/soc/views/sitemap/sidebar.py	Sat Nov 29 19:45:19 2008 +0000
@@ -40,3 +40,97 @@
       sidebar.append(menu)
 
   return sidebar
+
+
+def getSidebarItems(params):
+  """Retrieves a list of sidebar entries for this view
+
+  Params usage:
+    The params dictionary is provided to the menu_text's format.
+
+    sidebar: The sidebar value is returned directly if non-False
+    sidebar_defaults: The sidebar_defaults are used to construct the
+      sidebar items for this View. It is expected to be a tuple of
+      three items, the item's url, it's menu_text, and it's
+      access_type, see getSidebarLinks on how access_type is used.
+    sidebar_additional: The sidebar_additional values are appended
+      to the list of items verbatim, and should be in the format
+      expected by getSidebarLinks.
+
+  Args:
+    params: a dict with params for this View.
+  """
+
+  # Return the found result
+  if params['sidebar']:
+    return params['sidebar']
+
+  # Construct defaults manualy
+  defaults = params['sidebar_defaults']
+
+  result = []
+
+  for url, menu_text, access_type in defaults:
+    url = url % params['url_name'].lower()
+    item = (url, menu_text % params, access_type)
+    result.append(item)
+
+  for item in params['sidebar_additional']:
+    result.append(item)
+
+  return result
+
+
+def getSidebarLinks(request, params=None):
+  """Returns an dictionary with one sidebar entry.
+
+  Calls getSidebarItems to retrieve the items that should be in the
+  menu. Expected is a tuple with an url, a menu_text, and an
+  access_type. The access_type is then passed to checkAccess, if it
+  raises out_of_band.Error, the item will not be added.
+
+  Args:
+    request: the django request object
+    params: a dict with params for this View
+
+  Params usage:
+    The params dictionary is passed as argument to getSidebarItems,
+      see the docstring of getSidebarItems on how it uses it.
+
+    rights: The rights dictionary is used to check if the user has
+      the required rights to see a sidebar item.
+      See checkAccess for more details on how the rights dictionary
+      is used to check access rights.
+    sidebar_heading: The sidebar_heading value is used to set the
+      heading variable in the result.
+    name: The name value is used if sidebar_heading is not present.
+
+  Returns: A dictionary is returned with it's 'heading' value set
+    as explained above. It's 'items' value is constructed by
+    calling _getSidebarItems. It constists of dictionaries with a
+    url and a title field.
+  """
+
+  rights = params['rights']
+
+  items = []
+
+  for url, menu_text, access_type in getSidebarItems(params):
+    try:
+      access.checkAccess(access_type, request, rights)
+      items.append({'url': url, 'title': menu_text})
+    except out_of_band.Error:
+      pass
+
+  if not items:
+    return
+
+  res = {}
+
+  if 'sidebar_heading' not in params:
+    params['sidebar_heading'] = params['name']
+
+  res['heading'] = params['sidebar_heading']
+  res['items'] = items
+
+  return res