3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Module contains sidebar related functions.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
25 from soc
.views
import out_of_band
26 from soc
.views
.helper
import access
32 def addMenu(callback
):
33 """Adds a callback to the menu builder.
35 The callback should return a list of menu's when called.
39 SIDEBAR
.append(callback
)
41 def getSidebar(request
):
42 """Constructs a sidebar for the specified request.
47 for callback
in SIDEBAR
:
48 menus
= callback(request
)
50 for menu
in (menus
if menus
else []):
56 def getSidebarItems(params
):
57 """Retrieves a list of sidebar entries for this view.
60 The params dictionary is provided to the menu_text's format.
62 sidebar: The sidebar value is returned directly if non-False
63 sidebar_defaults: The sidebar_defaults are used to construct the
64 sidebar items for this View. It is expected to be a tuple of
65 three items, the item's url, it's menu_text, and it's
66 access_type, see getSidebarMenus on how access_type is used.
67 sidebar_additional: The sidebar_additional values are appended
68 to the list of items verbatim, and should be in the format
69 expected by getSidebarMenus.
72 params: a dict with params for this View.
75 # Return the found result
77 return params
['sidebar']
79 # Construct defaults manualy
80 defaults
= params
['sidebar_defaults']
84 for item
in params
['sidebar_additional']:
87 for url
, menu_text
, access_type
in defaults
:
88 url
= url
% params
['url_name'].lower()
89 item
= (url
, menu_text
% params
, access_type
)
95 def getSidebarMenu(request
, items
, params
):
96 """Returns an dictionary with one sidebar entry.
98 Items is expected to be a tuple with an url, a menu_text, and an
99 access_type. The access_type is then passed to checkAccess, if it
100 raises out_of_band.Error, the item will not be added.
103 request: the django request object
105 params: a dict with params for this View
108 The params dictionary is passed as argument to getSidebarItems,
109 see the docstring of getSidebarItems on how it uses it.
111 rights: The rights dictionary is used to check if the user has
112 the required rights to see a sidebar item.
113 See checkAccess for more details on how the rights dictionary
114 is used to check access rights.
115 sidebar_heading: The sidebar_heading value is used to set the
116 heading variable in the result.
117 name: The name value is used if sidebar_heading is not present.
120 A dictionary is returned with it's 'heading' value set as explained above.
121 It's 'items' value is constructed by calling _getSidebarItems. It constists
122 of dictionaries with a url and a title field.
125 rights
= params
['rights']
129 for url
, menu_text
, access_type
in items
:
131 access
.checkAccess(access_type
, request
, rights
)
132 submenus
.append({'url': url
, 'title': menu_text
})
133 except out_of_band
.Error
:
139 def getSidebarMenus(request
, params
=None):
140 """Constructs the default sidebar menu for a View.
142 Calls getSidebarItems to retrieve the items that should be in the
143 menu. Then passes the result to getSidebarMenu. See the respective
144 docstrings for an explanation on what they do.
147 request: the django request object
148 params: a dict with params for this View
151 items
= getSidebarItems(params
)
152 submenus
= getSidebarMenu(request
, items
, params
)
159 if 'sidebar_heading' not in params
:
160 params
['sidebar_heading'] = params
['name']
162 menu
['heading'] = params
['sidebar_heading']
163 menu
['items'] = submenus