# HG changeset patch # User Todd Larsen # Date 1222233464 0 # Node ID 4cf7c0040f2e64f999e4da6867d71cce3aaec049 # Parent 089a86d84067ebb131331495ba023ad3dd006d6e Sidebar-building controller, with a TODO remaining to implement marking the current page as "selected". diff -r 089a86d84067 -r 4cf7c0040f2e app/soc/logic/site/sidebar.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/soc/logic/site/sidebar.py Wed Sep 24 05:17:44 2008 +0000 @@ -0,0 +1,196 @@ +#!/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" ', + ] + + +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 for a User', + value='/site/user/lookup'), + menu.MenuItem( + 'List Users', + value='/site/user/list'), + menu.MenuItem( + 'Create a new User', + value='/site/user/profile'), + ] + ) + ), + ] + + +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)