app/soc/views/sitemap/sidebar.py
changeset 614 53a3e46fc512
parent 589 ee9122db04af
child 615 1d09147de51f
equal deleted inserted replaced
613:4880ffa9f3ba 614:53a3e46fc512
    38     if menu:
    38     if menu:
    39       # only if there is a menu we should append it
    39       # only if there is a menu we should append it
    40       sidebar.append(menu)
    40       sidebar.append(menu)
    41 
    41 
    42   return sidebar
    42   return sidebar
       
    43 
       
    44 
       
    45 def getSidebarItems(params):
       
    46   """Retrieves a list of sidebar entries for this view
       
    47 
       
    48   Params usage:
       
    49     The params dictionary is provided to the menu_text's format.
       
    50 
       
    51     sidebar: The sidebar value is returned directly if non-False
       
    52     sidebar_defaults: The sidebar_defaults are used to construct the
       
    53       sidebar items for this View. It is expected to be a tuple of
       
    54       three items, the item's url, it's menu_text, and it's
       
    55       access_type, see getSidebarLinks on how access_type is used.
       
    56     sidebar_additional: The sidebar_additional values are appended
       
    57       to the list of items verbatim, and should be in the format
       
    58       expected by getSidebarLinks.
       
    59 
       
    60   Args:
       
    61     params: a dict with params for this View.
       
    62   """
       
    63 
       
    64   # Return the found result
       
    65   if params['sidebar']:
       
    66     return params['sidebar']
       
    67 
       
    68   # Construct defaults manualy
       
    69   defaults = params['sidebar_defaults']
       
    70 
       
    71   result = []
       
    72 
       
    73   for url, menu_text, access_type in defaults:
       
    74     url = url % params['url_name'].lower()
       
    75     item = (url, menu_text % params, access_type)
       
    76     result.append(item)
       
    77 
       
    78   for item in params['sidebar_additional']:
       
    79     result.append(item)
       
    80 
       
    81   return result
       
    82 
       
    83 
       
    84 def getSidebarLinks(request, params=None):
       
    85   """Returns an dictionary with one sidebar entry.
       
    86 
       
    87   Calls getSidebarItems to retrieve the items that should be in the
       
    88   menu. Expected is a tuple with an url, a menu_text, and an
       
    89   access_type. The access_type is then passed to checkAccess, if it
       
    90   raises out_of_band.Error, the item will not be added.
       
    91 
       
    92   Args:
       
    93     request: the django request object
       
    94     params: a dict with params for this View
       
    95 
       
    96   Params usage:
       
    97     The params dictionary is passed as argument to getSidebarItems,
       
    98       see the docstring of getSidebarItems on how it uses it.
       
    99 
       
   100     rights: The rights dictionary is used to check if the user has
       
   101       the required rights to see a sidebar item.
       
   102       See checkAccess for more details on how the rights dictionary
       
   103       is used to check access rights.
       
   104     sidebar_heading: The sidebar_heading value is used to set the
       
   105       heading variable in the result.
       
   106     name: The name value is used if sidebar_heading is not present.
       
   107 
       
   108   Returns: A dictionary is returned with it's 'heading' value set
       
   109     as explained above. It's 'items' value is constructed by
       
   110     calling _getSidebarItems. It constists of dictionaries with a
       
   111     url and a title field.
       
   112   """
       
   113 
       
   114   rights = params['rights']
       
   115 
       
   116   items = []
       
   117 
       
   118   for url, menu_text, access_type in getSidebarItems(params):
       
   119     try:
       
   120       access.checkAccess(access_type, request, rights)
       
   121       items.append({'url': url, 'title': menu_text})
       
   122     except out_of_band.Error:
       
   123       pass
       
   124 
       
   125   if not items:
       
   126     return
       
   127 
       
   128   res = {}
       
   129 
       
   130   if 'sidebar_heading' not in params:
       
   131     params['sidebar_heading'] = params['name']
       
   132 
       
   133   res['heading'] = params['sidebar_heading']
       
   134   res['items'] = items
       
   135 
       
   136   return res