app/soc/views/sitemap/sidebar.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 20 Mar 2009 16:54:01 +0000
changeset 1954 7aef36e912bb
parent 1308 35b75ffcbb37
child 2069 08b13ca90666
permissions -rw-r--r--
Minor style fix. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
     1
#!/usr/bin/python2.5
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
637
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
    17
"""Module contains sidebar related functions.
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
    18
"""
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
    19
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  ]
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
1024
2a0ae6483825 Group dynamically generated menu items
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
    25
import operator
2a0ae6483825 Group dynamically generated menu items
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
    26
615
1d09147de51f Fixed a bug in latest commit
Sverre Rabbelier <srabbelier@gmail.com>
parents: 614
diff changeset
    27
from soc.views import out_of_band
1d09147de51f Fixed a bug in latest commit
Sverre Rabbelier <srabbelier@gmail.com>
parents: 614
diff changeset
    28
from soc.views.helper import access
1d09147de51f Fixed a bug in latest commit
Sverre Rabbelier <srabbelier@gmail.com>
parents: 614
diff changeset
    29
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    30
import soc.cache.sidebar
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    31
637
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
    32
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
    33
SIDEBAR = []
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 840
diff changeset
    34
SIDEBAR_ACCESS_ARGS = ['SIDEBAR_CALLING']
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 840
diff changeset
    35
SIDEBAR_ACCESS_KWARGS = {'SIDEBAR_CALLING': True}
226
a92b11181a49 Update some of the URL patterns to get their link_name parsing from the new
Todd Larsen <tlarsen@google.com>
parents: 205
diff changeset
    36
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
585
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    38
def addMenu(callback):
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    39
  """Adds a callback to the menu builder.
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    40
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    41
  The callback should return a list of menu's when called.
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    42
  """
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    43
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 284
diff changeset
    44
  global SIDEBAR
585
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    45
  SIDEBAR.append(callback)
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    46
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    47
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
    48
@soc.cache.sidebar.cache
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
    49
def getSidebar(id, user):
973
f9c2b32b9e2b Do not pass around request anymore in buildSidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 972
diff changeset
    50
  """Constructs a sidebar for the current user.
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    51
  """
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    52
585
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    53
  sidebar = []
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    54
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    55
  for callback in SIDEBAR:
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
    56
    menus = callback(id, user)
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    57
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
    58
    for menu in (menus if menus else []):
589
ee9122db04af Fixes empty spaces appearing in the menu if a person is not allowed to see this menu or if no menu is defined.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 586
diff changeset
    59
      sidebar.append(menu)
585
903890857ed8 Have the sidebar be rebuilt on every request with callbacks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    60
1024
2a0ae6483825 Group dynamically generated menu items
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
    61
  return sorted(sidebar, key=lambda x: x.get('group'))
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    62
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    63
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    64
def getSidebarItems(params):
637
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
    65
  """Retrieves a list of sidebar entries for this view.
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    66
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    67
  Params usage:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    68
    The params dictionary is provided to the menu_text's format.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    69
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    70
    sidebar: The sidebar value is returned directly if non-False
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    71
    sidebar_defaults: The sidebar_defaults are used to construct the
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    72
      sidebar items for this View. It is expected to be a tuple of
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    73
      three items, the item's url, it's menu_text, and it's
703
c4f3997becd9 Rename getSidebarLinks to getSidebarMenus
Sverre Rabbelier <srabbelier@gmail.com>
parents: 697
diff changeset
    74
      access_type, see getSidebarMenus on how access_type is used.
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    75
    sidebar_additional: The sidebar_additional values are appended
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    76
      to the list of items verbatim, and should be in the format
703
c4f3997becd9 Rename getSidebarLinks to getSidebarMenus
Sverre Rabbelier <srabbelier@gmail.com>
parents: 697
diff changeset
    77
      expected by getSidebarMenus.
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    78
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    79
  Args:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    80
    params: a dict with params for this View.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    81
  """
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    82
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    83
  # Return the found result
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    84
  if params['sidebar']:
840
d3f9fff0860b When constructing the sidebar don't overide 'sidebar'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    85
    default = params['sidebar']
d3f9fff0860b When constructing the sidebar don't overide 'sidebar'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    86
    result = default[:]
d3f9fff0860b When constructing the sidebar don't overide 'sidebar'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    87
    for item in params['sidebar_additional']:
d3f9fff0860b When constructing the sidebar don't overide 'sidebar'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    88
      result.append(item)
d3f9fff0860b When constructing the sidebar don't overide 'sidebar'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    89
    return result
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    90
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    91
  # Construct defaults manualy
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    92
  defaults = params['sidebar_defaults']
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    93
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    94
  result = []
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    95
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
    96
  for item in params['sidebar_additional']:
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
    97
    result.append(item)
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
    98
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
    99
  for url, menu_text, access_type in defaults:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   100
    url = url % params['url_name'].lower()
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   101
    item = (url, menu_text % params, access_type)
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   102
    result.append(item)
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   103
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   104
  return result
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   105
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   106
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   107
def getSidebarMenu(id, user, items, params):
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   108
  """Returns an dictionary with one sidebar entry.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   109
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   110
  Items is expected to be a tuple with an url, a menu_text, and an
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   111
  access_type. The access_type is then passed to checkAccess, if it
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   112
  raises out_of_band.Error, the item will not be added.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   113
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   114
  Args:
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   115
    items: see above
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   116
    params: a dict with params for this View
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   117
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   118
  Params usage:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   119
    The params dictionary is passed as argument to getSidebarItems,
637
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
   120
    see the docstring of getSidebarItems on how it uses it.
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   121
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   122
    rights: The rights dictionary is used to check if the user has
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   123
      the required rights to see a sidebar item.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   124
      See checkAccess for more details on how the rights dictionary
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   125
      is used to check access rights.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   126
    sidebar_heading: The sidebar_heading value is used to set the
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   127
      heading variable in the result.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   128
    name: The name value is used if sidebar_heading is not present.
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   129
637
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
   130
  Returns: 
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
   131
    A dictionary is returned with it's 'heading' value set as explained above.
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
   132
    It's 'items' value is constructed by calling _getSidebarItems. It constists
86ec079d8302 Some docstring and missing blank lines fixes in soc.views.sitemap modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 615
diff changeset
   133
    of dictionaries with a url and a title field.
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   134
  """
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   135
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   136
  rights = params['rights']
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   137
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   138
  submenus = []
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   139
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 840
diff changeset
   140
  args = SIDEBAR_ACCESS_ARGS
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 840
diff changeset
   141
  kwargs = SIDEBAR_ACCESS_KWARGS
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 840
diff changeset
   142
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   143
  # reset and pre-fill the Checker's cache
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   144
  rights.setCurrentUser(id, user)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   145
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   146
  for url, menu_text, access_type in items:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   147
    try:
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   148
      rights.checkAccess(access_type, kwargs)
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   149
      submenus.append({'url': url, 'title': menu_text})
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   150
    except out_of_band.Error:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   151
      pass
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   152
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   153
  return submenus
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   154
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   155
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   156
def getSidebarMenus(id, user, params=None):
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   157
  """Constructs the default sidebar menu for a View.
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   158
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   159
  Calls getSidebarItems to retrieve the items that should be in the
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   160
  menu. Then passes the result to getSidebarMenu. See the respective
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   161
  docstrings for an explanation on what they do.
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   162
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   163
  Args:
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   164
    params: a dict with params for this View
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   165
  """
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   166
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   167
  items = getSidebarItems(params)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   168
  submenus = getSidebarMenu(id, user, items, params)
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   169
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   170
  if not submenus:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   171
    return
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   172
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
   173
  menu = {}
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   174
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   175
  if 'sidebar_heading' not in params:
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   176
    params['sidebar_heading'] = params['name']
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   177
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
   178
  menu['heading'] = params['sidebar_heading']
704
8647e6b441fc Factor out sidebar entry construction
Sverre Rabbelier <srabbelier@gmail.com>
parents: 703
diff changeset
   179
  menu['items'] = submenus
1024
2a0ae6483825 Group dynamically generated menu items
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
   180
  menu['group'] = params['sidebar_grouping']
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 589
diff changeset
   181
697
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
   182
  menus = [menu]
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
   183
190b65431579 Prepare for callbacks that return more than one menu
Sverre Rabbelier <srabbelier@gmail.com>
parents: 637
diff changeset
   184
  return menus