app/soc/logic/site/sidebar.py
changeset 517 661ab830e921
parent 516 ec1dcd70b97e
child 518 d9d31d316a74
equal deleted inserted replaced
516:ec1dcd70b97e 517:661ab830e921
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     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
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    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.
       
    16 
       
    17 """Site-wide sidebar menu creation.
       
    18 
       
    19 """
       
    20 
       
    21 __authors__ = [
       
    22   '"Todd Larsen" <tlarsen@google.com>',
       
    23   ]
       
    24 
       
    25 
       
    26 from google.appengine.api import users
       
    27 
       
    28 from soc.logic import accounts
       
    29 from soc.logic import menu
       
    30 from soc.logic.site import map
       
    31 
       
    32 
       
    33 def buildUserSidebar(account=None, **ignored):
       
    34   """Returns a list of menu items for the User portion of the sidebar.
       
    35   
       
    36   Args:
       
    37     account: a Google Account (users.User) object; default is None, in which
       
    38       case users.get_current_user() is called 
       
    39     **ignored: other keyword arguments supplied to other sidebar builder
       
    40       functions, but ignored by this one
       
    41   """
       
    42   if account is None:
       
    43     account = users.get_current_user()
       
    44 
       
    45   if not account:
       
    46     return [map.user_signin_sub_menu.makeMenuItem()]
       
    47 
       
    48   return [map.user_signout_sub_menu.makeMenuItem()]
       
    49 
       
    50 
       
    51 def buildSiteSidebar(is_admin=None, **ignored):
       
    52   """Returns a list of menu items for the Developer portion of the sidebar.
       
    53   
       
    54   Args:
       
    55     is_admin: Boolean indicating that current user is a "Developer"
       
    56       (site super-user); default is None, in which case
       
    57       accounts.isDeveloper() is called 
       
    58     **ignored: other keyword arguments supplied to other sidebar builder
       
    59       functions, but ignored by this one
       
    60   """
       
    61   if is_admin is None:
       
    62     is_admin = accounts.isDeveloper()
       
    63 
       
    64   if not is_admin:
       
    65     # user is either not logged in or not a "Developer", so return no menu
       
    66     return None
       
    67 
       
    68   return [map.site_sub_menu.makeMenuItem()]
       
    69 
       
    70 
       
    71 def buildProgramsSidebar(**unused):
       
    72   """Mock-up for Programs section of sidebar menu.
       
    73   
       
    74   Args:
       
    75     **unused: all keyword arguments are currently unused in this mock-up
       
    76   
       
    77   TODO: actually implement this once Program entities are present in the
       
    78     Datastore.
       
    79   """
       
    80   return [
       
    81     menu.MenuItem(
       
    82       'Google Summer of Code',
       
    83       value='/program/gsoc2009/home',
       
    84       sub_menu=menu.Menu(items=[
       
    85         menu.MenuItem(
       
    86           'Community',
       
    87           value='/program/gsoc2009/community'),
       
    88         menu.MenuItem(
       
    89           'FAQs',
       
    90           value='/program/gsoc2009/document/faqs'),
       
    91         menu.MenuItem(
       
    92           'Terms of Service',
       
    93           value='/program/gsoc2009/document/tos'),
       
    94         ]
       
    95       )
       
    96     ),
       
    97     menu.MenuItem(
       
    98       'Google Highly Open Participation',
       
    99       value='/program/ghop2008/home',
       
   100       sub_menu=menu.Menu(items=[
       
   101         menu.MenuItem(
       
   102           'Community',
       
   103           value='/program/ghop2008/community'),
       
   104         menu.MenuItem(
       
   105           'FAQs',
       
   106           value='/program/ghop2008/document/faqs'),
       
   107         menu.MenuItem(
       
   108           'Contest Rules',
       
   109           value='/program/ghop2008/document/rules'),
       
   110         ]
       
   111       )
       
   112     ),
       
   113   ]
       
   114   
       
   115 
       
   116 DEF_SIDEBAR_BUILDERS = [
       
   117   buildUserSidebar,
       
   118   buildSiteSidebar,
       
   119   buildProgramsSidebar,
       
   120 ]
       
   121 
       
   122 def buildSidebar(path=None, builders=DEF_SIDEBAR_BUILDERS, **builder_args):
       
   123   """Calls all sidebar builders to construct the sidebar menu.
       
   124 
       
   125   Args:
       
   126     builders: list of functions that take context as a single
       
   127       argument; default is the list of sidebar builder functions present
       
   128       in soc.logic.site.sidebar
       
   129     **builder_args: keyword arguments passed to each sidebar builder function
       
   130       
       
   131   Returns:
       
   132     an soc.logic.menu.Menu object containing the sidebar menu items 
       
   133   """
       
   134   menu_items = []
       
   135 
       
   136   # call each of the sidebar builders and append any menu items they create
       
   137   for builder in builders:
       
   138     built_items = builder(**builder_args)
       
   139     
       
   140     if built_items:
       
   141       menu_items.extend(built_items)
       
   142 
       
   143   # try to determine which of the menu items is the current path, to indicate
       
   144   # that it is "selected"
       
   145   if not path:
       
   146     # path argument not supplied, so see if an HTTP request object was
       
   147     # supplied in the builder_args
       
   148     request = builder_args.get('request')
       
   149 
       
   150     if request:
       
   151       # there is an HTTP request object, so use the path stored in it
       
   152       path = request.path
       
   153 
       
   154   if path:
       
   155     # TODO(tlarsen): scan through list and mark current request.path 
       
   156     # as "selected"
       
   157     pass
       
   158 
       
   159   return menu.Menu(items=menu_items)