Skip to content

Commit 2d580ec

Browse files
author
Richard Jones
committed
more batching cleanup
1 parent f7f6604 commit 2d580ec

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

roundup/cgi/templating.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,20 +1295,48 @@ def batch(self):
12951295
matches = None
12961296
l = klass.filter(matches, filterspec, sort, group)
12971297

1298-
# return the batch object
1299-
return Batch(self.client, self.classname, l, self.pagesize,
1300-
self.startwith)
1298+
# map the item ids to instances
1299+
if self.classname == 'user':
1300+
klass = HTMLUser
1301+
else:
1302+
klass = HTMLItem
1303+
l = [klass(self.client, self.classname, item) for item in l]
13011304

1305+
# return the batch object
1306+
return Batch(self.client, l, self.pagesize, self.startwith)
13021307

13031308
# extend the standard ZTUtils Batch object to remove dependency on
13041309
# Acquisition and add a couple of useful methods
13051310
class Batch(ZTUtils.Batch):
1306-
def __init__(self, client, classname, l, size, start, end=0, orphan=0, overlap=0):
1311+
''' Use me to turn a list of items, or item ids of a given class, into a
1312+
series of batches.
1313+
1314+
========= ========================================================
1315+
Parameter Usage
1316+
========= ========================================================
1317+
sequence a list of HTMLItems
1318+
size how big to make the sequence.
1319+
start where to start (0-indexed) in the sequence.
1320+
end where to end (0-indexed) in the sequence.
1321+
orphan if the next batch would contain less items than this
1322+
value, then it is combined with this batch
1323+
overlap the number of items shared between adjacent batches
1324+
========= ========================================================
1325+
1326+
Attributes: Note that the "start" attribute, unlike the
1327+
argument, is a 1-based index (I know, lame). "first" is the
1328+
0-based index. "length" is the actual number of elements in
1329+
the batch.
1330+
1331+
"sequence_length" is the length of the original, unbatched, sequence.
1332+
'''
1333+
def __init__(self, client, sequence, size, start, end=0, orphan=0,
1334+
overlap=0):
13071335
self.client = client
1308-
self.classname = classname
13091336
self.last_index = self.last_item = None
13101337
self.current_item = None
1311-
ZTUtils.Batch.__init__(self, l, size, start, end, orphan, overlap)
1338+
ZTUtils.Batch.__init__(self, sequence, size, start, end, orphan,
1339+
overlap)
13121340

13131341
# overwrite so we can late-instantiate the HTMLItem instance
13141342
def __getitem__(self, index):
@@ -1325,16 +1353,7 @@ def __getitem__(self, index):
13251353
self.last_item = self.current_item
13261354
self.last_index = index
13271355

1328-
item = self._sequence[index + self.first]
1329-
if not isinstance(item, HTMLItem):
1330-
# "item" is actually just an id - wrap the return in an HTMLItem
1331-
if self.classname == 'user':
1332-
klass = HTMLUser
1333-
else:
1334-
klass = HTMLItem
1335-
item = klass(self.client, self.classname, item)
1336-
1337-
self.current_item = item
1356+
self.current_item = self._sequence[index + self.first]
13381357
return self.current_item
13391358

13401359
def propchanged(self, property):
@@ -1350,7 +1369,7 @@ def propchanged(self, property):
13501369
def previous(self):
13511370
if self.start == 1:
13521371
return None
1353-
return Batch(self.client, self.classname, self._sequence, self._size,
1372+
return Batch(self.client, self._sequence, self._size,
13541373
self.first - self._size + self.overlap, 0, self.orphan,
13551374
self.overlap)
13561375

@@ -1359,7 +1378,7 @@ def next(self):
13591378
self._sequence[self.end]
13601379
except IndexError:
13611380
return None
1362-
return Batch(self.client, self.classname, self._sequence, self._size,
1381+
return Batch(self.client, self._sequence, self._size,
13631382
self.end - self.overlap, 0, self.orphan, self.overlap)
13641383

13651384
def length(self):
@@ -1371,7 +1390,7 @@ class TemplatingUtils:
13711390
'''
13721391
def __init__(self, client):
13731392
self.client = client
1374-
def Batch(self, classname, l, size, start, end=0, orphan=0, overlap=0):
1375-
return Batch(self.client, classname, l, size, start, end, orphan,
1393+
def Batch(self, sequence, size, start, end=0, orphan=0, overlap=0):
1394+
return Batch(self.client, sequence, size, start, end, orphan,
13761395
overlap)
13771396

roundup/templates/classic/html/issue.index

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
<tr><th style="text-align: left; border: 0">
4040
<a tal:define="prev batch/previous" tal:condition="prev"
4141
tal:attributes="href python:request.indexargs_href(request.classname,
42-
{':startwith':prev.start, ':pagesize':prev.size})">&lt;&lt; previous</a>
42+
{':startwith':prev.first, ':pagesize':prev.size})">&lt;&lt; previous</a>
4343
&nbsp;
4444
</th>
4545
<th style="text-align: right; border: 0">
4646
<a tal:define="next batch/next" tal:condition="next"
4747
tal:attributes="href python:request.indexargs_href(request.classname,
48-
{':startwith':next.start, ':pagesize':next.size})">next &gt;&gt;</a>
48+
{':startwith':next.first, ':pagesize':next.size})">next &gt;&gt;</a>
4949
&nbsp;
5050
</th></tr>
5151
</table>

roundup/templates/classic/html/keyword.item

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<tr><th colspan="4" class="header">Existing Keywords</th></tr>
55
<tr tal:define="keywords db/keyword/list"
66
tal:repeat="start python:range(0, len(keywords), 4)">
7-
<td tal:define="batch python:utils.Batch('keyword', keywords, 4, start)"
7+
<td tal:define="batch python:utils.Batch(keywords, 4, start)"
88
tal:repeat="keyword batch" tal:content="keyword/name">keyword here</td>
99
</tr>
1010
<tr><td colspan="4" style="border-top: 1px solid gray">&nbsp;</td></tr>

0 commit comments

Comments
 (0)