app/soc/logic/site/sidebar.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Wed, 01 Oct 2008 14:23:28 +0000
changeset 245 b14c2c4d3484
parent 221 9cca97feadaa
child 253 da304c6f8fff
permissions -rw-r--r--
Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc). Patch by: Pawel Solyga Review by: to-be-reviewed

#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Site-wide sidebar menu creation.

"""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
  ]


from google.appengine.api import users

from django.utils import datastructures

from soc.logic import menu
from soc.logic.site import id_user


def buildUserSidebar(id=None, **ignored):
  """Returns a list of menu items for the User portion of the sidebar.
  
  Args:
    is_admin: Boolean indicating that current user is a "Developer"
      (site super-user); default is None, in which case
      id_user.isIdDeveloper() is called 
    **ignored: other keyword arguments supplied to other sidebar builder
      functions, but ignored by this one
  """
  if id is None:
    id = users.get_current_user()

  if not id:
    return [
      # user is logged out, so User top-level menu doubles as a sign-in link
      menu.MenuItem(
        'User (sign in)',
        value=users.create_login_url('/')),
    ]
    
  return [
    # user is logged in, so User top-level menu doubles as a sign-out link
    menu.MenuItem(
      'User (sign out)',
      value=users.create_logout_url('/'),
      sub_menu=menu.Menu(items=[
        # edit existing (or create new) site-wide User profile
        menu.MenuItem(
          'Site-wide Profile',
          value='/user/profile'),
        ]
      )
    ),
  ]


def buildSiteSidebar(is_admin=None, **ignored):
  """Returns a list of menu items for the Developer portion of the sidebar.
  
  Args:
    is_admin: Boolean indicating that current user is a "Developer"
      (site super-user); default is None, in which case
      id_user.isIdDeveloper() is called 
    **ignored: other keyword arguments supplied to other sidebar builder
      functions, but ignored by this one
  """
  if is_admin is None:
    is_admin = id_user.isIdDeveloper()

  if not is_admin:
    # user is either not logged in or not a "Developer", so return no menu
    return None

  return [
    menu.MenuItem(
      # Site top-level menu doubles as link for editing site-wide settings
      'Site',
      value='/site/home/edit',
      sub_menu=menu.Menu(items=[
        menu.MenuItem(
          'Search Site for a User',
          value='/site/user/lookup'),
        menu.MenuItem(
          'List Users of Site',
          value='/site/user/list'),
        menu.MenuItem(
          'Create a new Site User',
          value='/site/user/profile'),
        menu.MenuItem(
          'List Documents on Site',
          value='/site/docs/list'),
        menu.MenuItem(
          'Create a new Site Document',
          value='/site/docs/edit'),
        ]
      )
    ),
  ]


def buildProgramsSidebar(**unused):
  """Mock-up for Programs section of sidebar menu.
  
  Args:
    **unused: all keyword arguments are currently unused in this mock-up
  
  TODO: actually implement this once Program entities are present in the
    Datastore.
  """
  return [
    menu.MenuItem(
      'Google Summer of Code',
      value='/program/gsoc2009/home',
      sub_menu=menu.Menu(items=[
        menu.MenuItem(
          'Community',
          value='/program/gsoc2009/community'),
        menu.MenuItem(
          'FAQs',
          value='/program/gsoc2009/docs/faqs'),
        menu.MenuItem(
          'Terms of Service',
          value='/program/gsoc2009/docs/tos'),
        ]
      )
    ),
    menu.MenuItem(
      'Google Highly Open Participation',
      value='/program/ghop2008/home',
      sub_menu=menu.Menu(items=[
        menu.MenuItem(
          'Community',
          value='/program/ghop2008/community'),
        menu.MenuItem(
          'FAQs',
          value='/program/ghop2008/docs/faqs'),
        menu.MenuItem(
          'Contest Rules',
          value='/program/ghop2008/docs/rules'),
        ]
      )
    ),
  ]
  

DEF_SIDEBAR_BUILDERS = [
  buildUserSidebar,
  buildSiteSidebar,
  buildProgramsSidebar,
]

def buildSidebar(path=None, builders=DEF_SIDEBAR_BUILDERS, **builder_args):
  """Calls all sidebar builders to construct the sidebar menu.

  Args:
    builders: list of functions that take context as a single
      argument; default is the list of sidebar builder functions present
      in soc.logic.site.sidebar
    **builder_args: keyword arguments passed to each sidebar builder function
      
  Returns:
    an soc.logic.menu.Menu object containing the sidebar menu items 
  """
  menu_items = []

  # call each of the sidebar builders and append any menu items they create
  for builder in builders:
    built_items = builder(**builder_args)
    
    if built_items:
      menu_items.extend(built_items)

  # try to determine which of the menu items is the current path, to indicate
  # that it is "selected"
  if not path:
    # path argument not supplied, so see if an HTTP request object was
    # supplied in the builder_args
    request = builder_args.get('request')

    if request:
      # there is an HTTP request object, so use the path stored in it
      path = request.path

  if path:
    # TODO(tlarsen): scan through list and mark current request.path as "selected"
    pass

  return menu.Menu(items=menu_items)