app/soc/logic/site/sidebar.py
changeset 197 4cf7c0040f2e
child 199 14ede160ef16
equal deleted inserted replaced
196:089a86d84067 197:4cf7c0040f2e
       
     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 django.utils import datastructures
       
    29 
       
    30 from soc.logic import menu
       
    31 from soc.logic.site import id_user
       
    32 
       
    33 
       
    34 def buildUserSidebar(id=None, **ignored):
       
    35   """Returns a list of menu items for the User portion of the sidebar.
       
    36   
       
    37   Args:
       
    38     is_admin: Boolean indicating that current user is a "Developer"
       
    39       (site super-user); default is None, in which case
       
    40       id_user.isIdDeveloper() is called 
       
    41     **ignored: other keyword arguments supplied to other sidebar builder
       
    42       functions, but ignored by this one
       
    43   """
       
    44   if id is None:
       
    45     id = users.get_current_user()
       
    46 
       
    47   if not id:
       
    48     return [
       
    49       # user is logged out, so User top-level menu doubles as a sign-in link
       
    50       menu.MenuItem(
       
    51         'User (sign in)',
       
    52         value=users.create_login_url('/')),
       
    53     ]
       
    54     
       
    55   return [
       
    56     # user is logged in, so User top-level menu doubles as a sign-out link
       
    57     menu.MenuItem(
       
    58       'User (sign out)',
       
    59       value=users.create_logout_url('/'),
       
    60       sub_menu=menu.Menu(items=[
       
    61         # edit existing (or create new) site-wide User profile
       
    62         menu.MenuItem(
       
    63           'Site-wide Profile',
       
    64           value='/user/profile'),
       
    65         ]
       
    66       )
       
    67     ),
       
    68   ]
       
    69 
       
    70 
       
    71 def buildSiteSidebar(is_admin=None, **ignored):
       
    72   """Returns a list of menu items for the Developer portion of the sidebar.
       
    73   
       
    74   Args:
       
    75     is_admin: Boolean indicating that current user is a "Developer"
       
    76       (site super-user); default is None, in which case
       
    77       id_user.isIdDeveloper() is called 
       
    78     **ignored: other keyword arguments supplied to other sidebar builder
       
    79       functions, but ignored by this one
       
    80   """
       
    81   if is_admin is None:
       
    82     is_admin = id_user.isIdDeveloper()
       
    83 
       
    84   if not is_admin:
       
    85     # user is either not logged in or not a "Developer", so return no menu
       
    86     return None
       
    87 
       
    88   return [
       
    89     menu.MenuItem(
       
    90       # Site top-level menu doubles as link for editing site-wide settings
       
    91       'Site',
       
    92       value='/site/home/edit',
       
    93       sub_menu=menu.Menu(items=[
       
    94         menu.MenuItem(
       
    95           'Search for a User',
       
    96           value='/site/user/lookup'),
       
    97         menu.MenuItem(
       
    98           'List Users',
       
    99           value='/site/user/list'),
       
   100         menu.MenuItem(
       
   101           'Create a new User',
       
   102           value='/site/user/profile'),
       
   103         ]
       
   104       )
       
   105     ),
       
   106   ]
       
   107 
       
   108 
       
   109 def buildProgramsSidebar(**unused):
       
   110   """Mock-up for Programs section of sidebar menu.
       
   111   
       
   112   Args:
       
   113     **unused: all keyword arguments are currently unused in this mock-up
       
   114   
       
   115   TODO: actually implement this once Program entities are present in the
       
   116     Datastore.
       
   117   """
       
   118   return [
       
   119     menu.MenuItem(
       
   120       'Google Summer of Code',
       
   121       value='/program/gsoc2009/home',
       
   122       sub_menu=menu.Menu(items=[
       
   123         menu.MenuItem(
       
   124           'Community',
       
   125           value='/program/gsoc2009/community'),
       
   126         menu.MenuItem(
       
   127           'FAQs',
       
   128           value='/program/gsoc2009/docs/faqs'),
       
   129         menu.MenuItem(
       
   130           'Terms of Service',
       
   131           value='/program/gsoc2009/docs/tos'),
       
   132         ]
       
   133       )
       
   134     ),
       
   135     menu.MenuItem(
       
   136       'Google Highly Open Participation',
       
   137       value='/program/ghop2008/home',
       
   138       sub_menu=menu.Menu(items=[
       
   139         menu.MenuItem(
       
   140           'Community',
       
   141           value='/program/ghop2008/community'),
       
   142         menu.MenuItem(
       
   143           'FAQs',
       
   144           value='/program/ghop2008/docs/faqs'),
       
   145         menu.MenuItem(
       
   146           'Contest Rules',
       
   147           value='/program/ghop2008/docs/rules'),
       
   148         ]
       
   149       )
       
   150     ),
       
   151   ]
       
   152   
       
   153 
       
   154 DEF_SIDEBAR_BUILDERS = [
       
   155   buildUserSidebar,
       
   156   buildSiteSidebar,
       
   157   buildProgramsSidebar,
       
   158 ]
       
   159 
       
   160 def buildSidebar(path=None, builders=DEF_SIDEBAR_BUILDERS, **builder_args):
       
   161   """Calls all sidebar builders to construct the sidebar menu.
       
   162 
       
   163   Args:
       
   164     builders: list of functions that take context as a single
       
   165       argument; default is the list of sidebar builder functions present
       
   166       in soc.logic.site.sidebar
       
   167     **builder_args: keyword arguments passed to each sidebar builder function
       
   168       
       
   169   Returns:
       
   170     an soc.logic.menu.Menu object containing the sidebar menu items 
       
   171   """
       
   172   menu_items = []
       
   173 
       
   174   # call each of the sidebar builders and append any menu items they create
       
   175   for builder in builders:
       
   176     built_items = builder(**builder_args)
       
   177     
       
   178     if built_items:
       
   179       menu_items.extend(built_items)
       
   180 
       
   181   # try to determine which of the menu items is the current path, to indicate
       
   182   # that it is "selected"
       
   183   if not path:
       
   184     # path argument not supplied, so see if an HTTP request object was
       
   185     # supplied in the builder_args
       
   186     request = builder_args.get('request')
       
   187 
       
   188     if request:
       
   189       # there is an HTTP request object, so use the path stored in it
       
   190       path = request.path
       
   191 
       
   192   if path:
       
   193     # TODO(tlarsen): scan through list and mark current request.path as "selected"
       
   194     pass
       
   195 
       
   196   return menu.Menu(items=menu_items)