author | Sverre Rabbelier <srabbelier@gmail.com> |
Sat, 22 Nov 2008 20:10:43 +0000 | |
changeset 562 | 1bf2beedda03 |
parent 560 | a32be584ee04 |
child 572 | 1b3e7280743a |
permissions | -rw-r--r-- |
539
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""List generation logic |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
class Lists(object): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
"""List array suitable for enumerating over with just 'for in' |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
DEF_PASSTHROUGH_FIELDS = [ |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
'pagination', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
'description', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
'heading', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
'row', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
'limit', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
'newest', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
'prev', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
'next', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
'first', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
'last', |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
def __init__(self, contents): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
"""Constructs a new Lists object with the specified contents |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
# All the contents of all the lists |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
self.contents = contents |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
self.content = {} |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
# For iterating over all the lists |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
self.lists = range(len(contents)) |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
self.list_data = [] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
# For iterating over the rows |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
self.rows = [] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
self.row_data = [] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
def __getattr__(self, attr): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
"""Delegate field lookup to the current list if appropriate |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
If, and only if, a lookup is done on one of the fields defined in |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
DEF_PASSTHROUGH_FIELDS, and the current list defines this field, |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
the value from the current list is returned. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
if attr not in self.DEF_PASSTHROUGH_FIELDS: |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
raise AttributeError() |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
if attr not in self.content: |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
raise AttributeError() |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
return self.get(attr) |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
def get(self, item): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
"""Returns the item for the current list data |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
return self.content[item] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
def next_list(self): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
"""Shifts out the current list content |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
The main content of the next list is returned for further processing. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
# Advance the list data once |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
self.content = self.contents[0] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
self.contents = self.contents[1:] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
# Update internal 'iterators' |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
self.list_data = self.get('data') |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
self.rows = range(len(self.list_data)) |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
return self.get('main') |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
def next_row(self): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
"""Returns the next list row for the current list |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
Before calling this method, next_list should be called at least once. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
# Update internal 'iterators' |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
self.row_data = self.list_data[0] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
# Advance the row data once |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
self.list_data = self.list_data[1:] |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
return self.get('row') |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
def lists(self): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
"""Returns a list of numbers the size of the amount of lists. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
This method can be used to iterate over all lists with shift, |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
without using a while loop. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
116 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
return self.lists |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
def rows(self): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
"""Returns a list of numbers the size of the amount of items. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
This method can be used to iterate over all items with next for |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
the current list, without using a while loop. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
126 |
return self.rows |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
127 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
def item(self): |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
"""Returns the current row item for the current list |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
131 |
Before calling this method, next_row should be called at least once. |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
132 |
""" |
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
|
e30462354e26
Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
return self.row_data |
553
c0cc20b4afc9
Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents:
539
diff
changeset
|
135 |
|
c0cc20b4afc9
Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents:
539
diff
changeset
|
136 |
def redirect(self): |
c0cc20b4afc9
Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents:
539
diff
changeset
|
137 |
"""Returns the redirect for the current row item in the current list. |
c0cc20b4afc9
Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents:
539
diff
changeset
|
138 |
""" |
c0cc20b4afc9
Make redirect generic using the new Lists object
Sverre Rabbelier <srabbelier@gmail.com>
parents:
539
diff
changeset
|
139 |
|
560
a32be584ee04
Requests are almost working now
Sverre Rabbelier <srabbelier@gmail.com>
parents:
553
diff
changeset
|
140 |
action, args = self.get('action') |
a32be584ee04
Requests are almost working now
Sverre Rabbelier <srabbelier@gmail.com>
parents:
553
diff
changeset
|
141 |
result = action(self.row_data, args) |
a32be584ee04
Requests are almost working now
Sverre Rabbelier <srabbelier@gmail.com>
parents:
553
diff
changeset
|
142 |
return result |