pyams-table 1.0.0__py3.5.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +55 -0
- EGG-INFO/SOURCES.txt +32 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/namespace_packages.txt +1 -0
- EGG-INFO/not-zip-safe +1 -0
- EGG-INFO/requires.txt +14 -0
- EGG-INFO/top_level.txt +1 -0
- pyams_table/__init__.py +29 -0
- pyams_table/__pycache__/__init__.cpython-35.pyc +0 -0
- pyams_table/__pycache__/batch.cpython-35.pyc +0 -0
- pyams_table/__pycache__/column.cpython-35.pyc +0 -0
- pyams_table/__pycache__/header.cpython-35.pyc +0 -0
- pyams_table/__pycache__/include.cpython-35.pyc +0 -0
- pyams_table/__pycache__/interfaces.cpython-35.pyc +0 -0
- pyams_table/__pycache__/table.cpython-35.pyc +0 -0
- pyams_table/__pycache__/testing.cpython-35.pyc +0 -0
- pyams_table/__pycache__/value.cpython-35.pyc +0 -0
- pyams_table/batch.py +159 -0
- pyams_table/column.py +495 -0
- pyams_table/doctests/README.rst +668 -0
- pyams_table/doctests/batch.rst +660 -0
- pyams_table/doctests/column.rst +1274 -0
- pyams_table/doctests/miscellaneous.rst +123 -0
- pyams_table/doctests/sequence.rst +267 -0
- pyams_table/doctests/sort.rst +388 -0
- pyams_table/header.py +102 -0
- pyams_table/include.py +27 -0
- pyams_table/interfaces.py +249 -0
- pyams_table/locales/fr/LC_MESSAGES/pyams_table.mo +0 -0
- pyams_table/locales/fr/LC_MESSAGES/pyams_table.po +40 -0
- pyams_table/locales/pyams_table.pot +41 -0
- pyams_table/table.py +383 -0
- pyams_table/testing.py +129 -0
- pyams_table/tests/__init__.py +29 -0
- pyams_table/tests/__pycache__/__init__.cpython-35.pyc +0 -0
- pyams_table/tests/__pycache__/test_utilsdocs.cpython-35.pyc +0 -0
- pyams_table/tests/__pycache__/test_utilsdocstrings.cpython-35.pyc +0 -0
- pyams_table/tests/test_utilsdocs.py +60 -0
- pyams_table/tests/test_utilsdocstrings.py +65 -0
- pyams_table/value.py +51 -0
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
Batching
|
|
2
|
+
--------
|
|
3
|
+
|
|
4
|
+
Our table implements batching out of the box. If the amount of
|
|
5
|
+
row items is smaller than the given ``startBatchingAt`` size, the table starts
|
|
6
|
+
to batch at this size. Let's define a new Table.
|
|
7
|
+
|
|
8
|
+
We need to configure our batch provider for the next step first. See the
|
|
9
|
+
section ``BatchProvider`` below for more infos about batch rendering:
|
|
10
|
+
|
|
11
|
+
>>> from pyramid.testing import setUp, tearDown, DummyRequest
|
|
12
|
+
>>> from pyams_table.testing import setup_adapters
|
|
13
|
+
|
|
14
|
+
>>> config = setUp()
|
|
15
|
+
>>> setup_adapters(config.registry)
|
|
16
|
+
>>> root = {}
|
|
17
|
+
|
|
18
|
+
>>> from zope.interface import Interface
|
|
19
|
+
>>> from pyramid.interfaces import IRequest
|
|
20
|
+
>>> from pyams_batching.interfaces import IBatch
|
|
21
|
+
>>> from pyams_batching.batch import Batch
|
|
22
|
+
>>> from pyams_table.interfaces import IBatchProvider, ITable
|
|
23
|
+
>>> from pyams_table.batch import BatchProvider
|
|
24
|
+
>>> from pyams_utils.factory import register_factory
|
|
25
|
+
>>> register_factory(IBatch, Batch)
|
|
26
|
+
>>> config.registry.registerAdapter(BatchProvider,
|
|
27
|
+
... (Interface, IRequest, ITable),
|
|
28
|
+
... provided=IBatchProvider,
|
|
29
|
+
... name='batch')
|
|
30
|
+
|
|
31
|
+
Now we can create our table:
|
|
32
|
+
|
|
33
|
+
>>> from pyams_table.testing import Container, Content, SimpleTable
|
|
34
|
+
>>> container = Container()
|
|
35
|
+
>>> root['container-1'] = container
|
|
36
|
+
>>> request = DummyRequest()
|
|
37
|
+
>>> batchingTable = SimpleTable(container, request)
|
|
38
|
+
>>> batchingTable.css_class_sorted_on = None
|
|
39
|
+
|
|
40
|
+
We also need to give the table a location and a name like we normally setup
|
|
41
|
+
in traversing:
|
|
42
|
+
|
|
43
|
+
>>> batchingTable.__parent__ = container
|
|
44
|
+
>>> batchingTable.__name__ = 'batchingTable.html'
|
|
45
|
+
|
|
46
|
+
Now setup some items:
|
|
47
|
+
|
|
48
|
+
>>> container['zero'] = Content('Zero', 0)
|
|
49
|
+
>>> container['first'] = Content('First', 1)
|
|
50
|
+
>>> container['second'] = Content('Second', 2)
|
|
51
|
+
>>> container['third'] = Content('Third', 3)
|
|
52
|
+
>>> container['fourth'] = Content('Fourth', 4)
|
|
53
|
+
>>> container['sixth'] = Content('Sixth', 6)
|
|
54
|
+
>>> container['seventh'] = Content('Seventh', 7)
|
|
55
|
+
>>> container['eighth'] = Content('Eighth', 8)
|
|
56
|
+
>>> container['ninth'] = Content('Ninth', 9)
|
|
57
|
+
>>> container['tenth'] = Content('Tenth', 10)
|
|
58
|
+
>>> container['eleventh'] = Content('Eleventh', 11)
|
|
59
|
+
>>> container['twelfth '] = Content('Twelfth', 12)
|
|
60
|
+
>>> container['thirteenth'] = Content('Thirteenth', 13)
|
|
61
|
+
>>> container['fourteenth'] = Content('Fourteenth', 14)
|
|
62
|
+
>>> container['fifteenth '] = Content('Fifteenth', 15)
|
|
63
|
+
>>> container['sixteenth'] = Content('Sixteenth', 16)
|
|
64
|
+
>>> container['seventeenth'] = Content('Seventeenth', 17)
|
|
65
|
+
>>> container['eighteenth'] = Content('Eighteenth', 18)
|
|
66
|
+
>>> container['nineteenth'] = Content('Nineteenth', 19)
|
|
67
|
+
>>> container['twentieth'] = Content('Twentieth', 20)
|
|
68
|
+
|
|
69
|
+
Now let's show the full table without batching:
|
|
70
|
+
|
|
71
|
+
>>> batchingTable.update()
|
|
72
|
+
>>> print(batchingTable.render())
|
|
73
|
+
<table>
|
|
74
|
+
<thead>
|
|
75
|
+
<tr>
|
|
76
|
+
<th>My items</th>
|
|
77
|
+
<th>Number</th>
|
|
78
|
+
</tr>
|
|
79
|
+
</thead>
|
|
80
|
+
<tbody>
|
|
81
|
+
<tr>
|
|
82
|
+
<td>Eighteenth item</td>
|
|
83
|
+
<td>number: 18</td>
|
|
84
|
+
</tr>
|
|
85
|
+
<tr>
|
|
86
|
+
<td>Eighth item</td>
|
|
87
|
+
<td>number: 8</td>
|
|
88
|
+
</tr>
|
|
89
|
+
<tr>
|
|
90
|
+
<td>Eleventh item</td>
|
|
91
|
+
<td>number: 11</td>
|
|
92
|
+
</tr>
|
|
93
|
+
<tr>
|
|
94
|
+
<td>Fifteenth item</td>
|
|
95
|
+
<td>number: 15</td>
|
|
96
|
+
</tr>
|
|
97
|
+
<tr>
|
|
98
|
+
<td>First item</td>
|
|
99
|
+
<td>number: 1</td>
|
|
100
|
+
</tr>
|
|
101
|
+
<tr>
|
|
102
|
+
<td>Fourteenth item</td>
|
|
103
|
+
<td>number: 14</td>
|
|
104
|
+
</tr>
|
|
105
|
+
<tr>
|
|
106
|
+
<td>Fourth item</td>
|
|
107
|
+
<td>number: 4</td>
|
|
108
|
+
</tr>
|
|
109
|
+
<tr>
|
|
110
|
+
<td>Nineteenth item</td>
|
|
111
|
+
<td>number: 19</td>
|
|
112
|
+
</tr>
|
|
113
|
+
<tr>
|
|
114
|
+
<td>Ninth item</td>
|
|
115
|
+
<td>number: 9</td>
|
|
116
|
+
</tr>
|
|
117
|
+
<tr>
|
|
118
|
+
<td>Second item</td>
|
|
119
|
+
<td>number: 2</td>
|
|
120
|
+
</tr>
|
|
121
|
+
<tr>
|
|
122
|
+
<td>Seventeenth item</td>
|
|
123
|
+
<td>number: 17</td>
|
|
124
|
+
</tr>
|
|
125
|
+
<tr>
|
|
126
|
+
<td>Seventh item</td>
|
|
127
|
+
<td>number: 7</td>
|
|
128
|
+
</tr>
|
|
129
|
+
<tr>
|
|
130
|
+
<td>Sixteenth item</td>
|
|
131
|
+
<td>number: 16</td>
|
|
132
|
+
</tr>
|
|
133
|
+
<tr>
|
|
134
|
+
<td>Sixth item</td>
|
|
135
|
+
<td>number: 6</td>
|
|
136
|
+
</tr>
|
|
137
|
+
<tr>
|
|
138
|
+
<td>Tenth item</td>
|
|
139
|
+
<td>number: 10</td>
|
|
140
|
+
</tr>
|
|
141
|
+
<tr>
|
|
142
|
+
<td>Third item</td>
|
|
143
|
+
<td>number: 3</td>
|
|
144
|
+
</tr>
|
|
145
|
+
<tr>
|
|
146
|
+
<td>Thirteenth item</td>
|
|
147
|
+
<td>number: 13</td>
|
|
148
|
+
</tr>
|
|
149
|
+
<tr>
|
|
150
|
+
<td>Twelfth item</td>
|
|
151
|
+
<td>number: 12</td>
|
|
152
|
+
</tr>
|
|
153
|
+
<tr>
|
|
154
|
+
<td>Twentieth item</td>
|
|
155
|
+
<td>number: 20</td>
|
|
156
|
+
</tr>
|
|
157
|
+
<tr>
|
|
158
|
+
<td>Zero item</td>
|
|
159
|
+
<td>number: 0</td>
|
|
160
|
+
</tr>
|
|
161
|
+
</tbody>
|
|
162
|
+
</table>
|
|
163
|
+
|
|
164
|
+
As you can see, the table is not ordered and it uses all items. If we like
|
|
165
|
+
to use the batch, we need to set the startBatchingAt size to a lower value than
|
|
166
|
+
it is set by default.
|
|
167
|
+
The default value which a batch is used is set to ``50``:
|
|
168
|
+
|
|
169
|
+
>>> batchingTable.start_batching_at
|
|
170
|
+
50
|
|
171
|
+
|
|
172
|
+
We will set the batch start to ``5`` for now. This means the first 5 items
|
|
173
|
+
do not get used:
|
|
174
|
+
|
|
175
|
+
>>> batchingTable.start_batching_at = 5
|
|
176
|
+
>>> batchingTable.start_batching_at
|
|
177
|
+
5
|
|
178
|
+
|
|
179
|
+
There is also a ``batchSize`` value which we need to set to ``5``. By default
|
|
180
|
+
the value gets initialized by the ``batchSize`` value:
|
|
181
|
+
|
|
182
|
+
>>> batchingTable.batch_size
|
|
183
|
+
50
|
|
184
|
+
|
|
185
|
+
>>> batchingTable.batch_size = 5
|
|
186
|
+
>>> batchingTable.batch_size
|
|
187
|
+
5
|
|
188
|
+
|
|
189
|
+
Now we can update and render the table again. But you will see that we only get
|
|
190
|
+
a table size of 5 rows, which is correct. But the order doesn't depend on the
|
|
191
|
+
numbers we see in cells:
|
|
192
|
+
|
|
193
|
+
>>> batchingTable.update()
|
|
194
|
+
>>> print(batchingTable.render())
|
|
195
|
+
<table>
|
|
196
|
+
<thead>
|
|
197
|
+
<tr>
|
|
198
|
+
<th>My items</th>
|
|
199
|
+
<th>Number</th>
|
|
200
|
+
</tr>
|
|
201
|
+
</thead>
|
|
202
|
+
<tbody>
|
|
203
|
+
<tr>
|
|
204
|
+
<td>Eighteenth item</td>
|
|
205
|
+
<td>number: 18</td>
|
|
206
|
+
</tr>
|
|
207
|
+
<tr>
|
|
208
|
+
<td>Eighth item</td>
|
|
209
|
+
<td>number: 8</td>
|
|
210
|
+
</tr>
|
|
211
|
+
<tr>
|
|
212
|
+
<td>Eleventh item</td>
|
|
213
|
+
<td>number: 11</td>
|
|
214
|
+
</tr>
|
|
215
|
+
<tr>
|
|
216
|
+
<td>Fifteenth item</td>
|
|
217
|
+
<td>number: 15</td>
|
|
218
|
+
</tr>
|
|
219
|
+
<tr>
|
|
220
|
+
<td>First item</td>
|
|
221
|
+
<td>number: 1</td>
|
|
222
|
+
</tr>
|
|
223
|
+
</tbody>
|
|
224
|
+
</table>
|
|
225
|
+
|
|
226
|
+
I think we should order the table by the second column before we show the next
|
|
227
|
+
batch values. We do this by simply set the ``defaultSortOn``:
|
|
228
|
+
|
|
229
|
+
>>> batchingTable.sort_on = u'table-number-1'
|
|
230
|
+
|
|
231
|
+
Now we should see a nice ordered and batched table:
|
|
232
|
+
|
|
233
|
+
>>> batchingTable.update()
|
|
234
|
+
>>> print(batchingTable.render())
|
|
235
|
+
<table>
|
|
236
|
+
<thead>
|
|
237
|
+
<tr>
|
|
238
|
+
<th>My items</th>
|
|
239
|
+
<th>Number</th>
|
|
240
|
+
</tr>
|
|
241
|
+
</thead>
|
|
242
|
+
<tbody>
|
|
243
|
+
<tr>
|
|
244
|
+
<td>Zero item</td>
|
|
245
|
+
<td>number: 0</td>
|
|
246
|
+
</tr>
|
|
247
|
+
<tr>
|
|
248
|
+
<td>First item</td>
|
|
249
|
+
<td>number: 1</td>
|
|
250
|
+
</tr>
|
|
251
|
+
<tr>
|
|
252
|
+
<td>Second item</td>
|
|
253
|
+
<td>number: 2</td>
|
|
254
|
+
</tr>
|
|
255
|
+
<tr>
|
|
256
|
+
<td>Third item</td>
|
|
257
|
+
<td>number: 3</td>
|
|
258
|
+
</tr>
|
|
259
|
+
<tr>
|
|
260
|
+
<td>Fourth item</td>
|
|
261
|
+
<td>number: 4</td>
|
|
262
|
+
</tr>
|
|
263
|
+
</tbody>
|
|
264
|
+
</table>
|
|
265
|
+
|
|
266
|
+
The batch concept allows us to choose from all batches and render the rows
|
|
267
|
+
for this batched items. We can do this by set any batch as rows. as you can see
|
|
268
|
+
we have ``4`` batched row data available:
|
|
269
|
+
|
|
270
|
+
>>> len(batchingTable.rows.batches)
|
|
271
|
+
4
|
|
272
|
+
|
|
273
|
+
We can set such a batch as row values, then this batch data are used for
|
|
274
|
+
rendering. But take care, if we update the table, our rows get overridden
|
|
275
|
+
and reset to the previous values. this means you can set any batch as rows
|
|
276
|
+
data and only render them. This is possible since the update method sorted all
|
|
277
|
+
items and all batch contain ready-to-use data. This concept could be important
|
|
278
|
+
if you need to cache batches etc. :
|
|
279
|
+
|
|
280
|
+
>>> batchingTable.rows = batchingTable.rows.batches[1]
|
|
281
|
+
>>> print(batchingTable.render())
|
|
282
|
+
<table>
|
|
283
|
+
<thead>
|
|
284
|
+
<tr>
|
|
285
|
+
<th>My items</th>
|
|
286
|
+
<th>Number</th>
|
|
287
|
+
</tr>
|
|
288
|
+
</thead>
|
|
289
|
+
<tbody>
|
|
290
|
+
<tr>
|
|
291
|
+
<td>Sixth item</td>
|
|
292
|
+
<td>number: 6</td>
|
|
293
|
+
</tr>
|
|
294
|
+
<tr>
|
|
295
|
+
<td>Seventh item</td>
|
|
296
|
+
<td>number: 7</td>
|
|
297
|
+
</tr>
|
|
298
|
+
<tr>
|
|
299
|
+
<td>Eighth item</td>
|
|
300
|
+
<td>number: 8</td>
|
|
301
|
+
</tr>
|
|
302
|
+
<tr>
|
|
303
|
+
<td>Ninth item</td>
|
|
304
|
+
<td>number: 9</td>
|
|
305
|
+
</tr>
|
|
306
|
+
<tr>
|
|
307
|
+
<td>Tenth item</td>
|
|
308
|
+
<td>number: 10</td>
|
|
309
|
+
</tr>
|
|
310
|
+
</tbody>
|
|
311
|
+
</table>
|
|
312
|
+
|
|
313
|
+
And like described above, if you call ``update`` our batch to rows setup get
|
|
314
|
+
reset:
|
|
315
|
+
|
|
316
|
+
>>> batchingTable.update()
|
|
317
|
+
>>> print(batchingTable.render())
|
|
318
|
+
<table>
|
|
319
|
+
<thead>
|
|
320
|
+
<tr>
|
|
321
|
+
<th>My items</th>
|
|
322
|
+
<th>Number</th>
|
|
323
|
+
</tr>
|
|
324
|
+
</thead>
|
|
325
|
+
<tbody>
|
|
326
|
+
<tr>
|
|
327
|
+
<td>Zero item</td>
|
|
328
|
+
<td>number: 0</td>
|
|
329
|
+
</tr>
|
|
330
|
+
<tr>
|
|
331
|
+
<td>First item</td>
|
|
332
|
+
<td>number: 1</td>
|
|
333
|
+
</tr>
|
|
334
|
+
<tr>
|
|
335
|
+
<td>Second item</td>
|
|
336
|
+
<td>number: 2</td>
|
|
337
|
+
</tr>
|
|
338
|
+
<tr>
|
|
339
|
+
<td>Third item</td>
|
|
340
|
+
<td>number: 3</td>
|
|
341
|
+
</tr>
|
|
342
|
+
<tr>
|
|
343
|
+
<td>Fourth item</td>
|
|
344
|
+
<td>number: 4</td>
|
|
345
|
+
</tr>
|
|
346
|
+
</tbody>
|
|
347
|
+
</table>
|
|
348
|
+
|
|
349
|
+
This means you can probably update all batches, cache them and use them after.
|
|
350
|
+
But this is not useful for normal usage in a page without an enhanced concept
|
|
351
|
+
which is not a part of this implementation. This also means, there must be
|
|
352
|
+
another way to set the batch index. Yes there is, there are two other ways how
|
|
353
|
+
we can set the batch position. We can set a batch position by setting the
|
|
354
|
+
``batchStart`` value in our table or we can use a request variable. Let's show
|
|
355
|
+
the first one first:
|
|
356
|
+
|
|
357
|
+
>>> batchingTable.batch_start = 6
|
|
358
|
+
>>> batchingTable.update()
|
|
359
|
+
>>> print(batchingTable.render())
|
|
360
|
+
<table>
|
|
361
|
+
<thead>
|
|
362
|
+
<tr>
|
|
363
|
+
<th>My items</th>
|
|
364
|
+
<th>Number</th>
|
|
365
|
+
</tr>
|
|
366
|
+
</thead>
|
|
367
|
+
<tbody>
|
|
368
|
+
<tr>
|
|
369
|
+
<td>Seventh item</td>
|
|
370
|
+
<td>number: 7</td>
|
|
371
|
+
</tr>
|
|
372
|
+
<tr>
|
|
373
|
+
<td>Eighth item</td>
|
|
374
|
+
<td>number: 8</td>
|
|
375
|
+
</tr>
|
|
376
|
+
<tr>
|
|
377
|
+
<td>Ninth item</td>
|
|
378
|
+
<td>number: 9</td>
|
|
379
|
+
</tr>
|
|
380
|
+
<tr>
|
|
381
|
+
<td>Tenth item</td>
|
|
382
|
+
<td>number: 10</td>
|
|
383
|
+
</tr>
|
|
384
|
+
<tr>
|
|
385
|
+
<td>Eleventh item</td>
|
|
386
|
+
<td>number: 11</td>
|
|
387
|
+
</tr>
|
|
388
|
+
</tbody>
|
|
389
|
+
</table>
|
|
390
|
+
|
|
391
|
+
We can also set the batch position by using the batchStart value in a request.
|
|
392
|
+
Note that we need the table ``prefix`` and column ``__name__`` like we use in
|
|
393
|
+
the sorting concept:
|
|
394
|
+
|
|
395
|
+
>>> batchingRequest = DummyRequest(params={'table-batch-start': '11',
|
|
396
|
+
... 'table-batch-size': '5',
|
|
397
|
+
... 'table-sort-on': 'table-number-1'})
|
|
398
|
+
>>> requestBatchingTable = SimpleTable(container, batchingRequest)
|
|
399
|
+
>>> requestBatchingTable.css_class_sorted_on = None
|
|
400
|
+
|
|
401
|
+
We also need to give the table a location and a name like we normally set up
|
|
402
|
+
in traversing:
|
|
403
|
+
|
|
404
|
+
>>> requestBatchingTable.__parent__ = container
|
|
405
|
+
>>> requestBatchingTable.__name__ = 'requestBatchingTable.html'
|
|
406
|
+
|
|
407
|
+
Note: our table needs to start batching at smaller amount of items than we
|
|
408
|
+
have by default otherwise we don't get a batch:
|
|
409
|
+
|
|
410
|
+
>>> requestBatchingTable.start_batching_at = 5
|
|
411
|
+
>>> requestBatchingTable.update()
|
|
412
|
+
>>> print(requestBatchingTable.render())
|
|
413
|
+
<table>
|
|
414
|
+
<thead>
|
|
415
|
+
<tr>
|
|
416
|
+
<th>My items</th>
|
|
417
|
+
<th>Number</th>
|
|
418
|
+
</tr>
|
|
419
|
+
</thead>
|
|
420
|
+
<tbody>
|
|
421
|
+
<tr>
|
|
422
|
+
<td>Twelfth item</td>
|
|
423
|
+
<td>number: 12</td>
|
|
424
|
+
</tr>
|
|
425
|
+
<tr>
|
|
426
|
+
<td>Thirteenth item</td>
|
|
427
|
+
<td>number: 13</td>
|
|
428
|
+
</tr>
|
|
429
|
+
<tr>
|
|
430
|
+
<td>Fourteenth item</td>
|
|
431
|
+
<td>number: 14</td>
|
|
432
|
+
</tr>
|
|
433
|
+
<tr>
|
|
434
|
+
<td>Fifteenth item</td>
|
|
435
|
+
<td>number: 15</td>
|
|
436
|
+
</tr>
|
|
437
|
+
<tr>
|
|
438
|
+
<td>Sixteenth item</td>
|
|
439
|
+
<td>number: 16</td>
|
|
440
|
+
</tr>
|
|
441
|
+
</tbody>
|
|
442
|
+
</table>
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
BatchProvider
|
|
446
|
+
-------------
|
|
447
|
+
|
|
448
|
+
The batch provider allows us to render the batch HTML independently of our
|
|
449
|
+
table. This means by default the batch gets not rendered in the render method.
|
|
450
|
+
You can change this in your custom table implementation and return the batch
|
|
451
|
+
and the table in the render method.
|
|
452
|
+
|
|
453
|
+
As we can see, our table rows provides IBatch if it comes to batching:
|
|
454
|
+
|
|
455
|
+
>>> from pyams_batching.interfaces import IBatch
|
|
456
|
+
>>> IBatch.providedBy(requestBatchingTable.rows)
|
|
457
|
+
True
|
|
458
|
+
|
|
459
|
+
Let's check some batch variables before we render our test. This let us compare
|
|
460
|
+
the rendered result. For more information about batching see the README.txt in
|
|
461
|
+
pyams_batching:
|
|
462
|
+
|
|
463
|
+
>>> requestBatchingTable.rows.start
|
|
464
|
+
11
|
|
465
|
+
|
|
466
|
+
>>> requestBatchingTable.rows.index
|
|
467
|
+
2
|
|
468
|
+
|
|
469
|
+
>>> requestBatchingTable.rows.batches
|
|
470
|
+
<pyams_batching.batch.Batches object at ...>
|
|
471
|
+
|
|
472
|
+
>>> len(requestBatchingTable.rows.batches)
|
|
473
|
+
4
|
|
474
|
+
|
|
475
|
+
We use our previous batching table and render the batch with the built-in
|
|
476
|
+
``renderBatch`` method:
|
|
477
|
+
|
|
478
|
+
>>> requestBatchingTable.update()
|
|
479
|
+
>>> print(requestBatchingTable.render_batch())
|
|
480
|
+
<a href="...html?table-batch-size=5&table-batch-start=0&..." class="first">1</a>
|
|
481
|
+
<a href="...html?table-batch-size=5&table-batch-start=5&...">2</a>
|
|
482
|
+
<a href="...html?table-batch-size=5&table-batch-start=11&..." class="current">3</a>
|
|
483
|
+
<a href="...html?table-batch-size=5&table-batch-start=15&..." class="last">4</a>
|
|
484
|
+
|
|
485
|
+
Now let's add more items so that we can test the skipped links in large
|
|
486
|
+
batches:
|
|
487
|
+
|
|
488
|
+
>>> for i in range(1000):
|
|
489
|
+
... idx = i+20
|
|
490
|
+
... container[str(idx)] = Content(str(idx), idx)
|
|
491
|
+
|
|
492
|
+
Now let's test the batching table again with the new amount of items and
|
|
493
|
+
the same ``startBatchingAt`` of 5 but starting the batch at item ``100``
|
|
494
|
+
and sorted on the second numbered column:
|
|
495
|
+
|
|
496
|
+
>>> batchingRequest = DummyRequest(params={'table-batch-start': '100',
|
|
497
|
+
... 'table-batch-size': '5',
|
|
498
|
+
... 'table-sort-on': 'table-number-1'})
|
|
499
|
+
>>> requestBatchingTable = SimpleTable(container, batchingRequest)
|
|
500
|
+
>>> requestBatchingTable.start_batching_at = 5
|
|
501
|
+
>>> requestBatchingTable.css_class_sorted_on = None
|
|
502
|
+
|
|
503
|
+
We also need to give the table a location and a name like we normally setup
|
|
504
|
+
in traversing:
|
|
505
|
+
|
|
506
|
+
>>> requestBatchingTable.__parent__ = container
|
|
507
|
+
>>> requestBatchingTable.__name__ = 'requestBatchingTable.html'
|
|
508
|
+
|
|
509
|
+
>>> requestBatchingTable.update()
|
|
510
|
+
>>> print(requestBatchingTable.render())
|
|
511
|
+
<table>
|
|
512
|
+
<thead>
|
|
513
|
+
<tr>
|
|
514
|
+
<th>My items</th>
|
|
515
|
+
<th>Number</th>
|
|
516
|
+
</tr>
|
|
517
|
+
</thead>
|
|
518
|
+
<tbody>
|
|
519
|
+
<tr>
|
|
520
|
+
<td>100 item</td>
|
|
521
|
+
<td>number: 100</td>
|
|
522
|
+
</tr>
|
|
523
|
+
<tr>
|
|
524
|
+
<td>101 item</td>
|
|
525
|
+
<td>number: 101</td>
|
|
526
|
+
</tr>
|
|
527
|
+
<tr>
|
|
528
|
+
<td>102 item</td>
|
|
529
|
+
<td>number: 102</td>
|
|
530
|
+
</tr>
|
|
531
|
+
<tr>
|
|
532
|
+
<td>103 item</td>
|
|
533
|
+
<td>number: 103</td>
|
|
534
|
+
</tr>
|
|
535
|
+
<tr>
|
|
536
|
+
<td>104 item</td>
|
|
537
|
+
<td>number: 104</td>
|
|
538
|
+
</tr>
|
|
539
|
+
</tbody>
|
|
540
|
+
</table>
|
|
541
|
+
|
|
542
|
+
And test the batch. Note the three dots between the links are rendered by the
|
|
543
|
+
batch provider and are not a part of the doctest:
|
|
544
|
+
|
|
545
|
+
>>> print(requestBatchingTable.render_batch())
|
|
546
|
+
<a href="...html?table-batch-size=5&table-batch-start=0&table-sort-on=table-number-1" class="first">1</a>
|
|
547
|
+
...
|
|
548
|
+
<a href="...html?table-batch-size=5&table-batch-start=85&table-sort-on=table-number-1">18</a>
|
|
549
|
+
<a href="...html?table-batch-size=5&table-batch-start=90&table-sort-on=table-number-1">19</a>
|
|
550
|
+
<a href="...html?table-batch-size=5&table-batch-start=95&table-sort-on=table-number-1">20</a>
|
|
551
|
+
<a href="...html?table-batch-size=5&table-batch-start=100&table-sort-on=table-number-1" class="current">21</a>
|
|
552
|
+
<a href="...html?table-batch-size=5&table-batch-start=105&table-sort-on=table-number-1">22</a>
|
|
553
|
+
<a href="...html?table-batch-size=5&table-batch-start=110&table-sort-on=table-number-1">23</a>
|
|
554
|
+
<a href="...html?table-batch-size=5&table-batch-start=115&table-sort-on=table-number-1">24</a>
|
|
555
|
+
...
|
|
556
|
+
<a href="...html?table-batch-size=5&table-batch-start=1015&table-sort-on=table-number-1" class="last">204</a>
|
|
557
|
+
|
|
558
|
+
You can change the spacer in the batch provider if you set the ``batch_spacer``
|
|
559
|
+
value:
|
|
560
|
+
|
|
561
|
+
>>> from pyams_table.batch import BatchProvider
|
|
562
|
+
>>> from pyams_table.interfaces import IBatchProvider
|
|
563
|
+
>>> from zope.interface import implementer
|
|
564
|
+
>>> @implementer(IBatchProvider)
|
|
565
|
+
... class XBatchProvider(BatchProvider):
|
|
566
|
+
... """Just another batch provider."""
|
|
567
|
+
... batch_spacer = 'xxx'
|
|
568
|
+
|
|
569
|
+
Now register the new batch provider for our batching table:
|
|
570
|
+
|
|
571
|
+
>>> config.registry.registerAdapter(XBatchProvider,
|
|
572
|
+
... (Interface, IRequest, SimpleTable), name='batch')
|
|
573
|
+
|
|
574
|
+
If we update and render our table, the new batch provider should get used.
|
|
575
|
+
As you can see the spacer get changed now:
|
|
576
|
+
|
|
577
|
+
>>> requestBatchingTable.update()
|
|
578
|
+
>>> requestBatchingTable.batch_provider
|
|
579
|
+
<...XBatchProvider object at ...>
|
|
580
|
+
>>> print(requestBatchingTable.render_batch())
|
|
581
|
+
<a href="...html?table-batch-size=5&table-batch-start=0&table-sort-on=table-number-1" class="first">1</a>
|
|
582
|
+
xxx
|
|
583
|
+
<a href="...html?table-batch-size=5&table-batch-start=85&table-sort-on=table-number-1">18</a>
|
|
584
|
+
<a href="...html?table-batch-size=5&table-batch-start=90&table-sort-on=table-number-1">19</a>
|
|
585
|
+
<a href="...html?table-batch-size=5&table-batch-start=95&table-sort-on=table-number-1">20</a>
|
|
586
|
+
<a href="...html?table-batch-size=5&table-batch-start=100&table-sort-on=table-number-1" class="current">21</a>
|
|
587
|
+
<a href="...html?table-batch-size=5&table-batch-start=105&table-sort-on=table-number-1">22</a>
|
|
588
|
+
<a href="...html?table-batch-size=5&table-batch-start=110&table-sort-on=table-number-1">23</a>
|
|
589
|
+
<a href="...html?table-batch-size=5&table-batch-start=115&table-sort-on=table-number-1">24</a>
|
|
590
|
+
xxx
|
|
591
|
+
<a href="...html?table-batch-size=5&table-batch-start=1015&table-sort-on=table-number-1" class="last">204</a>
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
Now test the extremities, need to define a new batchingRequest:
|
|
595
|
+
Beginning by the left end point:
|
|
596
|
+
|
|
597
|
+
>>> leftBatchingRequest = DummyRequest(params={'table-batch-start': '10',
|
|
598
|
+
... 'table-batch-size': '5',
|
|
599
|
+
... 'table-sort-on': 'table-number-1'})
|
|
600
|
+
>>> leftRequestBatchingTable = SimpleTable(container, leftBatchingRequest)
|
|
601
|
+
>>> leftRequestBatchingTable.__parent__ = container
|
|
602
|
+
>>> leftRequestBatchingTable.__name__ = 'leftRequestBatchingTable.html'
|
|
603
|
+
>>> leftRequestBatchingTable.update()
|
|
604
|
+
>>> print(leftRequestBatchingTable.render_batch())
|
|
605
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=0&table-sort-on=table-number-1" class="first">1</a>
|
|
606
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=5&table-sort-on=table-number-1">2</a>
|
|
607
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=10&table-sort-on=table-number-1" class="current">3</a>
|
|
608
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=15&table-sort-on=table-number-1">4</a>
|
|
609
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=20&table-sort-on=table-number-1">5</a>
|
|
610
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=25&table-sort-on=table-number-1">6</a>
|
|
611
|
+
xxx
|
|
612
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=1015&table-sort-on=table-number-1" class="last">204</a>
|
|
613
|
+
|
|
614
|
+
Go on with the right extremity:
|
|
615
|
+
|
|
616
|
+
>>> rightBatchingRequest = DummyRequest(params={'table-batch-start': '1005',
|
|
617
|
+
... 'table-batch-size': '5',
|
|
618
|
+
... 'table-sort-on': 'table-number-1'})
|
|
619
|
+
>>> rightRequestBatchingTable = SimpleTable(container, rightBatchingRequest)
|
|
620
|
+
>>> rightRequestBatchingTable.__parent__ = container
|
|
621
|
+
>>> rightRequestBatchingTable.__name__ = 'rightRequestBatchingTable.html'
|
|
622
|
+
>>> rightRequestBatchingTable.update()
|
|
623
|
+
>>> print(rightRequestBatchingTable.render_batch())
|
|
624
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=0&table-sort-on=table-number-1" class="first">1</a>
|
|
625
|
+
xxx
|
|
626
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=990&table-sort-on=table-number-1">199</a>
|
|
627
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=995&table-sort-on=table-number-1">200</a>
|
|
628
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=1000&table-sort-on=table-number-1">201</a>
|
|
629
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=1005&table-sort-on=table-number-1" class="current">202</a>
|
|
630
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=1010&table-sort-on=table-number-1">203</a>
|
|
631
|
+
<a href="http://...html?table-batch-size=5&table-batch-start=1015&table-sort-on=table-number-1" class="last">204</a>
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
None previous and next batch size. Probably it doesn't make sense but let's
|
|
635
|
+
show what happens if we set the previous and next batch size to 0 (zero):
|
|
636
|
+
|
|
637
|
+
>>> from pyams_table.batch import BatchProvider
|
|
638
|
+
>>> class ZeroBatchProvider(BatchProvider):
|
|
639
|
+
... """Just another batch provider."""
|
|
640
|
+
... batch_spacer = u'xxx'
|
|
641
|
+
... previous_batch_size = 0
|
|
642
|
+
... next_batch_size = 0
|
|
643
|
+
|
|
644
|
+
Now register the new batch provider for our batching table:
|
|
645
|
+
|
|
646
|
+
>>> config.registry.registerAdapter(ZeroBatchProvider,
|
|
647
|
+
... (Interface, IRequest, SimpleTable), provided=IBatchProvider, name='batch')
|
|
648
|
+
|
|
649
|
+
Update the table and render the batch:
|
|
650
|
+
|
|
651
|
+
>>> requestBatchingTable.update()
|
|
652
|
+
>>> print(requestBatchingTable.render_batch())
|
|
653
|
+
<a href="...html?table-batch-size=5&table-batch-start=0&table-sort-on=table-number-1" class="first">1</a>
|
|
654
|
+
xxx
|
|
655
|
+
<a href="...html?table-batch-size=5&table-batch-start=100&table-sort-on=table-number-1" class="current">21</a>
|
|
656
|
+
xxx
|
|
657
|
+
<a href="...html?table-batch-size=5&table-batch-start=1015&table-sort-on=table-number-1" class="last">204</a>
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
>>> tearDown()
|