author | Todd Larsen <tlarsen@google.com> |
Wed, 01 Oct 2008 00:45:30 +0000 | |
changeset 224 | 35bf48c27eb6 |
parent 196 | 089a86d84067 |
child 390 | d12c95ade374 |
permissions | -rw-r--r-- |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
2 |
# |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
4 |
# |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
10 |
# |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
16 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
17 |
"""Representations and manipulation of arbitrarily nested menus. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
18 |
""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
19 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
22 |
] |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
23 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
25 |
from google.appengine.api import users |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
26 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
from django.utils import datastructures |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
28 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
29 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
30 |
class Menu: |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
31 |
"""Ordered collection of MenuItem objects. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
32 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
33 |
MenuItems are retrievable as an ordered list or individually by their |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
34 |
MenuItem.text key. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
def __init__(self, items=None): |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
"""Initializes ordered list of MenuItems. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
39 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
40 |
Args: |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
items: list of MenuItem objects in display order |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
42 |
""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
if not items: |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
44 |
items = [] |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
45 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
46 |
items = [(i.name, i) for i in items] |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
self._items = datastructures.SortedDict(data=items) |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
49 |
def getItem(self, name): |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
"""Returns a MenuItem retrieved by its MenuItem.text.""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
51 |
return self._items.get(name) |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
52 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
def setItem(self, item): |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
"""Overwrites an existing MenuItem, or appends a new one.""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
self._items[item.name] = item |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
57 |
def delItem(self, name): |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
58 |
"""Removes an existing MenuItem.""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
59 |
del self._items[name] |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
61 |
def _getItems(self): |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
62 |
"""Returns an ordered list of the MenuItems.""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
63 |
return self._items.values() |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
64 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
65 |
items = property(_getItems, doc= |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
66 |
"""Read-only list of MenuItems, for use in templates.""") |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
67 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
68 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
69 |
class MenuItem: |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
70 |
"""Provides menu item properties as easily-accessible attributes. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
71 |
""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
72 |
|
196
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
73 |
def __init__(self, name, value=None, selected=False, annotation=None, |
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
74 |
sub_menu=None): |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
75 |
"""Initializes the menu item attributes from supplied arguments. |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
76 |
|
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
77 |
Args: |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
78 |
name: name of the menu item |
196
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
79 |
value: optional value associated with the menu item; |
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
80 |
default is None |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
81 |
selected: Boolean indicating if this menu item is selected; |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
82 |
default is False |
196
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
83 |
annotation: optional annotation associated with the menu item; |
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
84 |
default is None |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
85 |
sub_menu: a Menu of sub-items to display below this menu item; |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
86 |
default is None, indicating no sub-menu |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
87 |
""" |
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
88 |
self.name = name |
196
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
89 |
self.value = value |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
90 |
self.selected = selected |
196
089a86d84067
Make views.helpers.html_menu MenuItem classes and logic.menu MenuItem classes
Todd Larsen <tlarsen@google.com>
parents:
192
diff
changeset
|
91 |
self.annotation = annotation |
192
f6bf679dab26
Simple MenuItem and Menu classes for keeping track of ordered menus.
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
92 |
self.sub_menu = sub_menu |