app/soc/logic/menu.py
changeset 192 f6bf679dab26
child 196 089a86d84067
equal deleted inserted replaced
191:80f08751f1e5 192:f6bf679dab26
       
     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 """Representations and manipulation of arbitrarily nested menus.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import users
       
    26 
       
    27 from django.utils import datastructures
       
    28 
       
    29 
       
    30 class Menu:
       
    31   """Ordered collection of MenuItem objects.
       
    32   
       
    33   MenuItems are retrievable as an ordered list or individually by their
       
    34   MenuItem.text key.
       
    35   """
       
    36   
       
    37   def __init__(self, items=None):
       
    38     """Initializes ordered list of MenuItems.
       
    39     
       
    40     Args:
       
    41       items: list of MenuItem objects in display order
       
    42     """
       
    43     if not items:
       
    44       items = []
       
    45     
       
    46     items = [(i.name, i) for i in items]
       
    47     self._items = datastructures.SortedDict(data=items)
       
    48 
       
    49   def getItem(self, name):
       
    50     """Returns a MenuItem retrieved by its MenuItem.text."""
       
    51     return self._items.get(name)
       
    52 
       
    53   def setItem(self, item):
       
    54     """Overwrites an existing MenuItem, or appends a new one."""
       
    55     self._items[item.name] = item
       
    56 
       
    57   def delItem(self, name):
       
    58     """Removes an existing MenuItem."""
       
    59     del self._items[name]
       
    60 
       
    61   def _getItems(self):
       
    62     """Returns an ordered list of the MenuItems."""
       
    63     return self._items.values()
       
    64 
       
    65   items = property(_getItems, doc=
       
    66     """Read-only list of MenuItems, for use in templates.""")
       
    67 
       
    68 
       
    69 class MenuItem:
       
    70   """Provides menu item properties as easily-accessible attributes.
       
    71   """
       
    72   
       
    73   def __init__(self, name, selected=False, sub_menu=None):
       
    74     """Initializes the menu item attributes from supplied arguments.
       
    75     
       
    76     Args:
       
    77       name: name of the menu item
       
    78       selected: Boolean indicating if this menu item is selected;
       
    79         default is False
       
    80       sub_menu: a Menu of sub-items to display below this menu item;
       
    81         default is None, indicating no sub-menu
       
    82     """
       
    83     self.name = name
       
    84     self.selected = selected
       
    85     self.sub_menu = sub_menu