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.
Files changed (40) hide show
  1. EGG-INFO/PKG-INFO +55 -0
  2. EGG-INFO/SOURCES.txt +32 -0
  3. EGG-INFO/dependency_links.txt +1 -0
  4. EGG-INFO/namespace_packages.txt +1 -0
  5. EGG-INFO/not-zip-safe +1 -0
  6. EGG-INFO/requires.txt +14 -0
  7. EGG-INFO/top_level.txt +1 -0
  8. pyams_table/__init__.py +29 -0
  9. pyams_table/__pycache__/__init__.cpython-35.pyc +0 -0
  10. pyams_table/__pycache__/batch.cpython-35.pyc +0 -0
  11. pyams_table/__pycache__/column.cpython-35.pyc +0 -0
  12. pyams_table/__pycache__/header.cpython-35.pyc +0 -0
  13. pyams_table/__pycache__/include.cpython-35.pyc +0 -0
  14. pyams_table/__pycache__/interfaces.cpython-35.pyc +0 -0
  15. pyams_table/__pycache__/table.cpython-35.pyc +0 -0
  16. pyams_table/__pycache__/testing.cpython-35.pyc +0 -0
  17. pyams_table/__pycache__/value.cpython-35.pyc +0 -0
  18. pyams_table/batch.py +159 -0
  19. pyams_table/column.py +495 -0
  20. pyams_table/doctests/README.rst +668 -0
  21. pyams_table/doctests/batch.rst +660 -0
  22. pyams_table/doctests/column.rst +1274 -0
  23. pyams_table/doctests/miscellaneous.rst +123 -0
  24. pyams_table/doctests/sequence.rst +267 -0
  25. pyams_table/doctests/sort.rst +388 -0
  26. pyams_table/header.py +102 -0
  27. pyams_table/include.py +27 -0
  28. pyams_table/interfaces.py +249 -0
  29. pyams_table/locales/fr/LC_MESSAGES/pyams_table.mo +0 -0
  30. pyams_table/locales/fr/LC_MESSAGES/pyams_table.po +40 -0
  31. pyams_table/locales/pyams_table.pot +41 -0
  32. pyams_table/table.py +383 -0
  33. pyams_table/testing.py +129 -0
  34. pyams_table/tests/__init__.py +29 -0
  35. pyams_table/tests/__pycache__/__init__.cpython-35.pyc +0 -0
  36. pyams_table/tests/__pycache__/test_utilsdocs.cpython-35.pyc +0 -0
  37. pyams_table/tests/__pycache__/test_utilsdocstrings.cpython-35.pyc +0 -0
  38. pyams_table/tests/test_utilsdocs.py +60 -0
  39. pyams_table/tests/test_utilsdocstrings.py +65 -0
  40. pyams_table/value.py +51 -0
@@ -0,0 +1,1274 @@
1
+ =============
2
+ Table Columns
3
+ =============
4
+
5
+ Let's show the different columns we offer by default. But first take a look at
6
+ the README.txt which explains the Table and Column concepts.
7
+
8
+
9
+ Sample data setup
10
+ -----------------
11
+
12
+ >>> from pyramid.testing import setUp, tearDown
13
+ >>> from pyams_table.testing import setup_adapters
14
+
15
+ >>> config = setUp()
16
+ >>> setup_adapters(config.registry)
17
+ >>> root = {}
18
+
19
+ Let's create a sample container that we can use as our iterable context:
20
+
21
+ >>> from zope.container import btree
22
+ >>> class Container(btree.BTreeContainer):
23
+ ... """Sample container."""
24
+ >>> container = Container()
25
+ >>> root['container'] = container
26
+
27
+ and create a sample content object that we use as container item:
28
+
29
+ >>> class Content(object):
30
+ ... """Sample content."""
31
+ ... def __init__(self, title, number, email):
32
+ ... self.title = title
33
+ ... self.number = number
34
+ ... self.email = email
35
+
36
+ Now setup some items:
37
+
38
+ >>> container['zero'] = Content('Zero', 0, 'zero@example.com')
39
+ >>> container['first'] = Content('First', 1, 'first@example.com')
40
+ >>> container['second'] = Content('Second', 2, 'second@example.com')
41
+ >>> container['third'] = Content('Third', 3, 'third@example.com')
42
+ >>> container['fourth'] = Content('Fourth', 4, None)
43
+
44
+ Let's also create a simple number sortable column:
45
+
46
+ >>> from pyams_table import column
47
+ >>> class NumberColumn(column.Column):
48
+ ...
49
+ ... header = 'Number'
50
+ ... weight = 20
51
+ ...
52
+ ... def get_sort_key(self, item):
53
+ ... return item.number
54
+ ...
55
+ ... def render_cell(self, item):
56
+ ... return 'number: %s' % item.number
57
+
58
+
59
+ NameColumn
60
+ ----------
61
+
62
+ Let's define a table using the NameColumn:
63
+
64
+ >>> from pyams_table import table
65
+ >>> class NameTable(table.Table):
66
+ ... css_class_sorted_on = None
67
+ ...
68
+ ... def setup_columns(self):
69
+ ... return [
70
+ ... column.add_column(self, column.NameColumn, 'name',
71
+ ... weight=1),
72
+ ... column.add_column(self, NumberColumn, name='number',
73
+ ... weight=2, header='Number')
74
+ ... ]
75
+
76
+ Now create, update and render our table and you can see that the NameColumn
77
+ renders the name of the item using the zope.traversing.api.getName() concept:
78
+
79
+ >>> from pyramid.testing import DummyRequest
80
+ >>> request = DummyRequest()
81
+ >>> nameTable = NameTable(container, request)
82
+ >>> nameTable.update()
83
+ >>> print(nameTable.render())
84
+ <table>
85
+ <thead>
86
+ <tr>
87
+ <th>Name</th>
88
+ <th>Number</th>
89
+ </tr>
90
+ </thead>
91
+ <tbody>
92
+ <tr>
93
+ <td>first</td>
94
+ <td>number: 1</td>
95
+ </tr>
96
+ <tr>
97
+ <td>fourth</td>
98
+ <td>number: 4</td>
99
+ </tr>
100
+ <tr>
101
+ <td>second</td>
102
+ <td>number: 2</td>
103
+ </tr>
104
+ <tr>
105
+ <td>third</td>
106
+ <td>number: 3</td>
107
+ </tr>
108
+ <tr>
109
+ <td>zero</td>
110
+ <td>number: 0</td>
111
+ </tr>
112
+ </tbody>
113
+ </table>
114
+
115
+
116
+ RadioColumn
117
+ -----------
118
+
119
+ Let's define a table using the RadioColumn:
120
+
121
+ >>> class RadioTable(table.Table):
122
+ ... css_class_sorted_on = None
123
+ ...
124
+ ... def setup_columns(self):
125
+ ... return [
126
+ ... column.add_column(self, column.RadioColumn, 'radioColumn',
127
+ ... weight=1),
128
+ ... column.add_column(self, NumberColumn, name='number',
129
+ ... weight=2, header='Number')
130
+ ... ]
131
+
132
+ Now create, update and render our table:
133
+
134
+ >>> request = DummyRequest()
135
+ >>> radioTable = RadioTable(container, request)
136
+ >>> radioTable.update()
137
+ >>> print(radioTable.render())
138
+ <table>
139
+ <thead>
140
+ <tr>
141
+ <th>X</th>
142
+ <th>Number</th>
143
+ </tr>
144
+ </thead>
145
+ <tbody>
146
+ <tr>
147
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="first" /></td>
148
+ <td>number: 1</td>
149
+ </tr>
150
+ <tr>
151
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="fourth" /></td>
152
+ <td>number: 4</td>
153
+ </tr>
154
+ <tr>
155
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="second" /></td>
156
+ <td>number: 2</td>
157
+ </tr>
158
+ <tr>
159
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="third" /></td>
160
+ <td>number: 3</td>
161
+ </tr>
162
+ <tr>
163
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="zero" /></td>
164
+ <td>number: 0</td>
165
+ </tr>
166
+ </tbody>
167
+ </table>
168
+
169
+ As you can see, we can force to render the radio input field as selected with a
170
+ given request value:
171
+
172
+ >>> radioRequest = DummyRequest(params={'table-radioColumn-0-selected-item': 'third'})
173
+ >>> radioTable = RadioTable(container, radioRequest)
174
+ >>> radioTable.update()
175
+ >>> print(radioTable.render())
176
+ <table>
177
+ <thead>
178
+ <tr>
179
+ <th>X</th>
180
+ <th>Number</th>
181
+ </tr>
182
+ </thead>
183
+ <tbody>
184
+ <tr>
185
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="first" /></td>
186
+ <td>number: 1</td>
187
+ </tr>
188
+ <tr>
189
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="fourth" /></td>
190
+ <td>number: 4</td>
191
+ </tr>
192
+ <tr>
193
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="second" /></td>
194
+ <td>number: 2</td>
195
+ </tr>
196
+ <tr>
197
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="third" checked="checked" /></td>
198
+ <td>number: 3</td>
199
+ </tr>
200
+ <tr>
201
+ <td><input type="radio" class="radio-widget" name="table-radioColumn-0-selected-item" value="zero" /></td>
202
+ <td>number: 0</td>
203
+ </tr>
204
+ </tbody>
205
+ </table>
206
+
207
+
208
+ CheckBoxColumn
209
+ --------------
210
+
211
+ Let's define a table using the RadioColumn:
212
+
213
+ >>> class CheckBoxTable(table.Table):
214
+ ... css_class_sorted_on = None
215
+ ...
216
+ ... def setup_columns(self):
217
+ ... return [
218
+ ... column.add_column(self, column.CheckBoxColumn, 'checkBoxColumn',
219
+ ... weight=1),
220
+ ... column.add_column(self, NumberColumn, name='number',
221
+ ... weight=2, header='Number')
222
+ ... ]
223
+
224
+ Now create, update and render our table:
225
+
226
+
227
+ >>> request = DummyRequest()
228
+ >>> checkBoxTable = CheckBoxTable(container, request)
229
+ >>> checkBoxTable.update()
230
+ >>> print(checkBoxTable.render())
231
+ <table>
232
+ <thead>
233
+ <tr>
234
+ <th>X</th>
235
+ <th>Number</th>
236
+ </tr>
237
+ </thead>
238
+ <tbody>
239
+ <tr>
240
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="first" /></td>
241
+ <td>number: 1</td>
242
+ </tr>
243
+ <tr>
244
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="fourth" /></td>
245
+ <td>number: 4</td>
246
+ </tr>
247
+ <tr>
248
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="second" /></td>
249
+ <td>number: 2</td>
250
+ </tr>
251
+ <tr>
252
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="third" /></td>
253
+ <td>number: 3</td>
254
+ </tr>
255
+ <tr>
256
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="zero" /></td>
257
+ <td>number: 0</td>
258
+ </tr>
259
+ </tbody>
260
+ </table>
261
+
262
+ And again you can set force to render the checkbox input field as selected with
263
+ a given request value:
264
+
265
+ >>> checkBoxRequest = DummyRequest(params={'table-checkBoxColumn-0-selected-items':
266
+ ... ['first', 'third']})
267
+ >>> checkBoxTable = CheckBoxTable(container, checkBoxRequest)
268
+ >>> checkBoxTable.update()
269
+ >>> print(checkBoxTable.render())
270
+ <table>
271
+ <thead>
272
+ <tr>
273
+ <th>X</th>
274
+ <th>Number</th>
275
+ </tr>
276
+ </thead>
277
+ <tbody>
278
+ <tr>
279
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="first" checked="checked" /></td>
280
+ <td>number: 1</td>
281
+ </tr>
282
+ <tr>
283
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="fourth" /></td>
284
+ <td>number: 4</td>
285
+ </tr>
286
+ <tr>
287
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="second" /></td>
288
+ <td>number: 2</td>
289
+ </tr>
290
+ <tr>
291
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="third" checked="checked" /></td>
292
+ <td>number: 3</td>
293
+ </tr>
294
+ <tr>
295
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="zero" /></td>
296
+ <td>number: 0</td>
297
+ </tr>
298
+ </tbody>
299
+ </table>
300
+
301
+ If you select a row, you can also give them an additional CSS style. This could
302
+ be used in combination with alternating ``even`` and ``odd`` styles:
303
+
304
+ >>> checkBoxRequest = DummyRequest(params={'table-checkBoxColumn-0-selected-items':
305
+ ... ['first', 'third']})
306
+ >>> checkBoxTable = CheckBoxTable(container, checkBoxRequest)
307
+ >>> checkBoxTable.css_classes = {'tr': 'tr'}
308
+ >>> checkBoxTable.css_class_selected = 'selected'
309
+ >>> checkBoxTable.css_class_even = 'even'
310
+ >>> checkBoxTable.css_class_odd = 'odd'
311
+ >>> checkBoxTable.update()
312
+ >>> print(checkBoxTable.render())
313
+ <table>
314
+ <thead>
315
+ <tr class="tr">
316
+ <th>X</th>
317
+ <th>Number</th>
318
+ </tr>
319
+ </thead>
320
+ <tbody>
321
+ <tr class="selected even tr">
322
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="first" checked="checked" /></td>
323
+ <td>number: 1</td>
324
+ </tr>
325
+ <tr class="odd tr">
326
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="fourth" /></td>
327
+ <td>number: 4</td>
328
+ </tr>
329
+ <tr class="even tr">
330
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="second" /></td>
331
+ <td>number: 2</td>
332
+ </tr>
333
+ <tr class="selected odd tr">
334
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="third" checked="checked" /></td>
335
+ <td>number: 3</td>
336
+ </tr>
337
+ <tr class="even tr">
338
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="zero" /></td>
339
+ <td>number: 0</td>
340
+ </tr>
341
+ </tbody>
342
+ </table>
343
+
344
+ Let's test the ``css_class_selected`` without any other css class:
345
+
346
+ >>> checkBoxRequest = DummyRequest(params={'table-checkBoxColumn-0-selected-items':
347
+ ... ['first', 'third']})
348
+ >>> checkBoxTable = CheckBoxTable(container, checkBoxRequest)
349
+ >>> checkBoxTable.css_class_selected = u'selected'
350
+ >>> checkBoxTable.update()
351
+ >>> print(checkBoxTable.render())
352
+ <table>
353
+ <thead>
354
+ <tr>
355
+ <th>X</th>
356
+ <th>Number</th>
357
+ </tr>
358
+ </thead>
359
+ <tbody>
360
+ <tr class="selected">
361
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="first" checked="checked" /></td>
362
+ <td>number: 1</td>
363
+ </tr>
364
+ <tr>
365
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="fourth" /></td>
366
+ <td>number: 4</td>
367
+ </tr>
368
+ <tr>
369
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="second" /></td>
370
+ <td>number: 2</td>
371
+ </tr>
372
+ <tr class="selected">
373
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="third" checked="checked" /></td>
374
+ <td>number: 3</td>
375
+ </tr>
376
+ <tr>
377
+ <td><input type="checkbox" class="checkbox-widget" name="table-checkBoxColumn-0-selected-items" value="zero" /></td>
378
+ <td>number: 0</td>
379
+ </tr>
380
+ </tbody>
381
+ </table>
382
+
383
+
384
+ CreatedColumn
385
+ -------------
386
+
387
+ Let's define a table using the CreatedColumn:
388
+
389
+ >>> class CreatedColumnTable(table.Table):
390
+ ... css_class_sorted_on = None
391
+ ...
392
+ ... def setup_columns(self):
393
+ ... return [
394
+ ... column.add_column(self, column.CreatedColumn, u'createdColumn',
395
+ ... weight=1),
396
+ ... ]
397
+
398
+ Now create, update and render our table. Note, we use a Dublin Core stub
399
+ adapter which only returns ``01/01/01 01:01`` as created date:
400
+
401
+ >>> request = DummyRequest()
402
+ >>> createdColumnTable = CreatedColumnTable(container, request)
403
+ >>> createdColumnTable.update()
404
+ >>> print(createdColumnTable.render())
405
+ <table>
406
+ <thead>
407
+ <tr>
408
+ <th>Created</th>
409
+ </tr>
410
+ </thead>
411
+ <tbody>
412
+ <tr>
413
+ <td>on 01/01/2001 at 01:01</td>
414
+ </tr>
415
+ <tr>
416
+ <td>on 01/01/2001 at 01:01</td>
417
+ </tr>
418
+ <tr>
419
+ <td>on 01/01/2001 at 01:01</td>
420
+ </tr>
421
+ <tr>
422
+ <td>on 01/01/2001 at 01:01</td>
423
+ </tr>
424
+ <tr>
425
+ <td>on 01/01/2001 at 01:01</td>
426
+ </tr>
427
+ </tbody>
428
+ </table>
429
+
430
+
431
+ ModifiedColumn
432
+ --------------
433
+
434
+ Let's define a table using the ModifiedColumn and a custom format string:
435
+
436
+ >>> from pyams_utils.date import SH_DATETIME_FORMAT
437
+ >>> class ModifiedColumnTable(table.Table):
438
+ ... css_class_sorted_on = None
439
+ ...
440
+ ... def setup_columns(self):
441
+ ... return [
442
+ ... column.add_column(self, column.ModifiedColumn,
443
+ ... 'modifiedColumn', weight=1,
444
+ ... format_string=SH_DATETIME_FORMAT),
445
+ ... ]
446
+
447
+ Now create, update and render our table. Note, we use a Dublin Core stub
448
+ adapter which only returns ``02/02/02 02:02`` as modified date:
449
+
450
+ >>> request = DummyRequest()
451
+ >>> modifiedColumnTable = ModifiedColumnTable(container, request)
452
+ >>> modifiedColumnTable.update()
453
+ >>> print(modifiedColumnTable.render())
454
+ <table>
455
+ <thead>
456
+ <tr>
457
+ <th>Modified</th>
458
+ </tr>
459
+ </thead>
460
+ <tbody>
461
+ <tr>
462
+ <td>02/02/2002 - 02:02</td>
463
+ </tr>
464
+ <tr>
465
+ <td>02/02/2002 - 02:02</td>
466
+ </tr>
467
+ <tr>
468
+ <td>02/02/2002 - 02:02</td>
469
+ </tr>
470
+ <tr>
471
+ <td>02/02/2002 - 02:02</td>
472
+ </tr>
473
+ <tr>
474
+ <td>02/02/2002 - 02:02</td>
475
+ </tr>
476
+ </tbody>
477
+ </table>
478
+
479
+
480
+ GetAttrColumn
481
+ -------------
482
+
483
+ The ``GetAttrColumn`` column is a handy column that retrieves the value from
484
+ the item by attribute access.
485
+ It also provides a ``default_value`` in case an exception happens.
486
+
487
+ >>> class GetTitleColumn(column.GetAttrColumn):
488
+ ...
489
+ ... attr_name = 'title'
490
+ ... default_value = 'missing'
491
+
492
+ >>> class GetAttrColumnTable(table.Table):
493
+ ... css_class_sorted_on = None
494
+ ...
495
+ ... def setup_columns(self):
496
+ ... return [
497
+ ... column.add_column(self, GetTitleColumn, 'title'),
498
+ ... ]
499
+
500
+ Render and update the table:
501
+
502
+ >>> request = DummyRequest()
503
+ >>> getAttrColumnTable = GetAttrColumnTable(container, request)
504
+ >>> getAttrColumnTable.update()
505
+ >>> print(getAttrColumnTable.render())
506
+ <table>
507
+ <thead>
508
+ <tr>
509
+ <th></th>
510
+ </tr>
511
+ </thead>
512
+ <tbody>
513
+ <tr>
514
+ <td>First</td>
515
+ </tr>
516
+ <tr>
517
+ <td>Fourth</td>
518
+ </tr>
519
+ <tr>
520
+ <td>Second</td>
521
+ </tr>
522
+ <tr>
523
+ <td>Third</td>
524
+ </tr>
525
+ <tr>
526
+ <td>Zero</td>
527
+ </tr>
528
+ </tbody>
529
+ </table>
530
+
531
+ If we use a non-existing Attribute, we do not raise an AttributeError, we will
532
+ get the default value:
533
+
534
+ >>> class UndefinedAttributeColumn(column.GetAttrColumn):
535
+ ...
536
+ ... attr_name = 'undefined'
537
+ ... default_value = 'missing'
538
+
539
+ >>> class GetAttrColumnTable(table.Table):
540
+ ... css_class_sorted_on = None
541
+ ...
542
+ ... def setup_columns(self):
543
+ ... return [
544
+ ... column.add_column(self, UndefinedAttributeColumn, 'missing'),
545
+ ... ]
546
+
547
+ Render and update the table:
548
+
549
+ >>> request = DummyRequest()
550
+ >>> getAttrColumnTable = GetAttrColumnTable(container, request)
551
+ >>> getAttrColumnTable.update()
552
+ >>> print(getAttrColumnTable.render())
553
+ <table>
554
+ <thead>
555
+ <tr>
556
+ <th></th>
557
+ </tr>
558
+ </thead>
559
+ <tbody>
560
+ <tr>
561
+ <td>missing</td>
562
+ </tr>
563
+ <tr>
564
+ <td>missing</td>
565
+ </tr>
566
+ <tr>
567
+ <td>missing</td>
568
+ </tr>
569
+ <tr>
570
+ <td>missing</td>
571
+ </tr>
572
+ <tr>
573
+ <td>missing</td>
574
+ </tr>
575
+ </tbody>
576
+ </table>
577
+
578
+ A missing ``attr_name`` in ``GetAttrColumn`` would also end in return the
579
+ ``default_value``:
580
+
581
+ >>> class BadAttributeColumn(column.GetAttrColumn):
582
+ ...
583
+ ... default_value = u'missing'
584
+
585
+ >>> firstItem = container[u'first']
586
+ >>> simpleTable = table.Table(container, request)
587
+ >>> badColumn = column.add_column(simpleTable, BadAttributeColumn, u'bad')
588
+ >>> badColumn.render_cell(firstItem)
589
+ 'missing'
590
+
591
+ If we try to access a protected attribute the object raises an ``Unauthorized``.
592
+ In this case we also return the default_value. Let's setup an object which
593
+ raises such an error if we access the title:
594
+
595
+ >>> from zope.security.interfaces import Unauthorized
596
+ >>> class ProtectedItem(object):
597
+ ...
598
+ ... @property
599
+ ... def forbidden(self):
600
+ ... raise Unauthorized('forbidden')
601
+
602
+ Setup and test the item:
603
+
604
+ >>> protectedItem = ProtectedItem()
605
+ >>> protectedItem.forbidden
606
+ Traceback (most recent call last):
607
+ ...
608
+ zope.security.interfaces.Unauthorized: forbidden
609
+
610
+ Now define a column:
611
+
612
+ >>> class ForbiddenAttributeColumn(column.GetAttrColumn):
613
+ ...
614
+ ... attr_name = 'forbidden'
615
+ ... default_value = u'missing'
616
+
617
+ And test the attribute access:
618
+
619
+ >>> simpleTable = table.Table(container, request)
620
+ >>> badColumn = column.add_column(simpleTable, ForbiddenAttributeColumn, u'x')
621
+ >>> badColumn.render_cell(protectedItem)
622
+ 'missing'
623
+
624
+
625
+ GetItemColumn
626
+ -------------
627
+
628
+ The ``GetItemColumn`` column is a handy column that retrieves the value from
629
+ the item by index or key access. That means the item can be a tuple, list, dict
630
+ or anything that implements that.
631
+ It also provides a ``default_value`` in case an exception happens.
632
+
633
+ Dict-ish
634
+ .........
635
+
636
+ >>> sampleDictData = [
637
+ ... dict(name='foo', value=1),
638
+ ... dict(name='bar', value=7),
639
+ ... dict(name='moo', value=42),]
640
+
641
+ >>> class GetDictColumnTable(table.Table):
642
+ ... css_class_sorted_on = None
643
+ ...
644
+ ... def setup_columns(self):
645
+ ... return [
646
+ ... column.add_column(self, column.GetItemColumn, 'name',
647
+ ... header='Name',
648
+ ... idx='name', default_value='missing'),
649
+ ... column.add_column(self, column.GetItemColumn, 'value',
650
+ ... header='Value',
651
+ ... idx='value', default_value='missing'),
652
+ ... ]
653
+ ... @property
654
+ ... def values(self):
655
+ ... return sampleDictData
656
+
657
+ Render and update the table:
658
+
659
+ >>> request = DummyRequest()
660
+ >>> getDictColumnTable = GetDictColumnTable(sampleDictData, request)
661
+ >>> getDictColumnTable.update()
662
+ >>> print(getDictColumnTable.render())
663
+ <table>
664
+ <thead>
665
+ <tr>
666
+ <th>Name</th>
667
+ <th>Value</th>
668
+ </tr>
669
+ </thead>
670
+ <tbody>
671
+ <tr>
672
+ <td>bar</td>
673
+ <td>7</td>
674
+ </tr>
675
+ <tr>
676
+ <td>foo</td>
677
+ <td>1</td>
678
+ </tr>
679
+ <tr>
680
+ <td>moo</td>
681
+ <td>42</td>
682
+ </tr>
683
+ </tbody>
684
+ </table>
685
+
686
+ If we use a non-existing index/key, we do not raise an exception, we will
687
+ get the default value:
688
+
689
+ >>> class GetDictColumnTable(table.Table):
690
+ ... css_class_sorted_on = None
691
+ ...
692
+ ... def setup_columns(self):
693
+ ... return [
694
+ ... column.add_column(self, column.GetItemColumn, 'name',
695
+ ... idx='not-existing', default_value='missing'),
696
+ ... ]
697
+ ... @property
698
+ ... def values(self):
699
+ ... return sampleDictData
700
+
701
+ Render and update the table:
702
+
703
+ >>> request = DummyRequest()
704
+ >>> getDictColumnTable = GetDictColumnTable(container, request)
705
+ >>> getDictColumnTable.update()
706
+ >>> print(getDictColumnTable.render())
707
+ <table>
708
+ <thead>
709
+ <tr>
710
+ <th></th>
711
+ </tr>
712
+ </thead>
713
+ <tbody>
714
+ <tr>
715
+ <td>missing</td>
716
+ </tr>
717
+ <tr>
718
+ <td>missing</td>
719
+ </tr>
720
+ <tr>
721
+ <td>missing</td>
722
+ </tr>
723
+ </tbody>
724
+ </table>
725
+
726
+ A missing ``idx`` in ``GetItemColumn`` would also end in return the
727
+ ``default_value``:
728
+
729
+ >>> class BadIdxColumn(column.GetItemColumn):
730
+ ...
731
+ ... default_value = 'missing'
732
+
733
+ >>> firstItem = sampleDictData[0]
734
+ >>> simpleTable = table.Table(sampleDictData, request)
735
+ >>> badColumn = column.add_column(simpleTable, BadIdxColumn, u'bad')
736
+ >>> badColumn.render_cell(firstItem)
737
+ 'missing'
738
+
739
+ Tuple/List-ish
740
+ ...............
741
+
742
+ >>> sampleTupleData = [
743
+ ... (50, 'bar'),
744
+ ... (42, 'cent'),
745
+ ... (7, 'bild'),]
746
+
747
+ >>> class GetTupleColumnTable(table.Table):
748
+ ... css_class_sorted_on = None
749
+ ...
750
+ ... def setup_columns(self):
751
+ ... return [
752
+ ... column.add_column(self, column.GetItemColumn, u'name',
753
+ ... header='Name',
754
+ ... idx=1, default_value='missing'),
755
+ ... column.add_column(self, column.GetItemColumn, 'value',
756
+ ... header=u'Value',
757
+ ... idx=0, default_value='missing'),
758
+ ... ]
759
+ ... @property
760
+ ... def values(self):
761
+ ... return sampleTupleData
762
+
763
+ Render and update the table:
764
+
765
+ >>> request = DummyRequest()
766
+ >>> getTupleColumnTable = GetTupleColumnTable(sampleTupleData, request)
767
+ >>> getTupleColumnTable.update()
768
+ >>> print(getTupleColumnTable.render())
769
+ <table>
770
+ <thead>
771
+ <tr>
772
+ <th>Name</th>
773
+ <th>Value</th>
774
+ </tr>
775
+ </thead>
776
+ <tbody>
777
+ <tr>
778
+ <td>bar</td>
779
+ <td>50</td>
780
+ </tr>
781
+ <tr>
782
+ <td>bild</td>
783
+ <td>7</td>
784
+ </tr>
785
+ <tr>
786
+ <td>cent</td>
787
+ <td>42</td>
788
+ </tr>
789
+ </tbody>
790
+ </table>
791
+
792
+ If we use a non-existing index/key, we do not raise an exception, we will
793
+ get the default value:
794
+
795
+ >>> class GetTupleColumnTable(table.Table):
796
+ ... css_class_sorted_on = None
797
+ ...
798
+ ... def setup_columns(self):
799
+ ... return [
800
+ ... column.add_column(self, column.GetItemColumn, 'name',
801
+ ... idx=42, default_value='missing'),
802
+ ... ]
803
+ ... @property
804
+ ... def values(self):
805
+ ... return sampleTupleData
806
+
807
+ Render and update the table:
808
+
809
+ >>> request = DummyRequest()
810
+ >>> getTupleColumnTable = GetTupleColumnTable(container, request)
811
+ >>> getTupleColumnTable.update()
812
+ >>> print(getTupleColumnTable.render())
813
+ <table>
814
+ <thead>
815
+ <tr>
816
+ <th></th>
817
+ </tr>
818
+ </thead>
819
+ <tbody>
820
+ <tr>
821
+ <td>missing</td>
822
+ </tr>
823
+ <tr>
824
+ <td>missing</td>
825
+ </tr>
826
+ <tr>
827
+ <td>missing</td>
828
+ </tr>
829
+ </tbody>
830
+ </table>
831
+
832
+ A missing ``idx`` in ``GetItemColumn`` would also end in return the
833
+ ``default_value``:
834
+
835
+ >>> class BadIdxColumn(column.GetItemColumn):
836
+ ...
837
+ ... default_value = 'missing'
838
+
839
+ >>> firstItem = sampleTupleData[0]
840
+ >>> simpleTable = table.Table(sampleTupleData, request)
841
+ >>> badColumn = column.add_column(simpleTable, BadIdxColumn, 'bad')
842
+ >>> badColumn.render_cell(firstItem)
843
+ 'missing'
844
+
845
+
846
+ GetAttrFormatterColumn
847
+ ----------------------
848
+
849
+ The ``GetAttrFormatterColumn`` column is a get attr column which is able to
850
+ format the value. Let's use the Dublin Core adapter for our sample:
851
+
852
+ >>> from zope.dublincore.interfaces import IZopeDublinCore
853
+ >>> class GetCreatedColumn(column.GetAttrFormatterColumn):
854
+ ...
855
+ ... def get_value(self, item):
856
+ ... dc = IZopeDublinCore(item, None)
857
+ ... return dc.created
858
+
859
+ >>> class GetAttrFormatterColumnTable(table.Table):
860
+ ... css_class_sorted_on = None
861
+ ...
862
+ ... def setup_columns(self):
863
+ ... return [
864
+ ... column.add_column(self, GetCreatedColumn, 'created'),
865
+ ... ]
866
+
867
+ Render and update the table:
868
+
869
+ >>> request = DummyRequest()
870
+ >>> getAttrFormatterColumnTable = GetAttrFormatterColumnTable(container,
871
+ ... request)
872
+ >>> getAttrFormatterColumnTable.update()
873
+ >>> print(getAttrFormatterColumnTable.render())
874
+ <table>
875
+ <thead>
876
+ <tr>
877
+ <th></th>
878
+ </tr>
879
+ </thead>
880
+ <tbody>
881
+ <tr>
882
+ <td>on 01/01/2001 at 01:01</td>
883
+ </tr>
884
+ <tr>
885
+ <td>on 01/01/2001 at 01:01</td>
886
+ </tr>
887
+ <tr>
888
+ <td>on 01/01/2001 at 01:01</td>
889
+ </tr>
890
+ <tr>
891
+ <td>on 01/01/2001 at 01:01</td>
892
+ </tr>
893
+ <tr>
894
+ <td>on 01/01/2001 at 01:01</td>
895
+ </tr>
896
+ </tbody>
897
+ </table>
898
+
899
+
900
+ We can also change the formatter settings in such a column:
901
+
902
+ >>> class ShortCreatedColumn(column.GetAttrFormatterColumn):
903
+ ...
904
+ ... format_string = SH_DATETIME_FORMAT
905
+ ...
906
+ ... def get_value(self, item):
907
+ ... dc = IZopeDublinCore(item, None)
908
+ ... return dc.created
909
+
910
+ >>> class ShortFormatterColumnTable(table.Table):
911
+ ... css_class_sorted_on = None
912
+ ...
913
+ ... def setup_columns(self):
914
+ ... return [
915
+ ... column.add_column(self, ShortCreatedColumn, 'created'),
916
+ ... ]
917
+
918
+ Render and update the table:
919
+
920
+ >>> request = DummyRequest()
921
+ >>> shortFormatterColumnTable = ShortFormatterColumnTable(container,
922
+ ... request)
923
+ >>> shortFormatterColumnTable.update()
924
+ >>> print(shortFormatterColumnTable.render())
925
+ <table>
926
+ <thead>
927
+ <tr>
928
+ <th></th>
929
+ </tr>
930
+ </thead>
931
+ <tbody>
932
+ <tr>
933
+ <td>01/01/2001 - 01:01</td>
934
+ </tr>
935
+ <tr>
936
+ <td>01/01/2001 - 01:01</td>
937
+ </tr>
938
+ <tr>
939
+ <td>01/01/2001 - 01:01</td>
940
+ </tr>
941
+ <tr>
942
+ <td>01/01/2001 - 01:01</td>
943
+ </tr>
944
+ <tr>
945
+ <td>01/01/2001 - 01:01</td>
946
+ </tr>
947
+ </tbody>
948
+ </table>
949
+
950
+
951
+ EMailColumn
952
+ -----------
953
+
954
+ The ``EMailColumn`` column is ``GetAttrColumn`` which is used to
955
+ display a mailto link. By default in the link content the e-mail
956
+ address is displayed, too.
957
+
958
+
959
+ >>> class EMailColumn(column.EMailColumn):
960
+ ...
961
+ ... attr_name = 'email'
962
+ ... default_value = 'missing'
963
+
964
+ >>> class EMailColumnTable(table.Table):
965
+ ... css_class_sorted_on = None
966
+ ...
967
+ ... def setup_columns(self):
968
+ ... return [
969
+ ... column.add_column(self, EMailColumn, 'email'),
970
+ ... ]
971
+
972
+ When a cell does not contain an e-mail address, the ``default_value``
973
+ is rendered:
974
+
975
+ >>> request = DummyRequest()
976
+ >>> eMailColumnTable = EMailColumnTable(container, request)
977
+ >>> eMailColumnTable.update()
978
+ >>> print(eMailColumnTable.render())
979
+ <table>
980
+ <thead>
981
+ <tr>
982
+ <th>E-Mail</th>
983
+ </tr>
984
+ </thead>
985
+ <tbody>
986
+ <tr>
987
+ <td><a href="mailto:first@example.com">first@example.com</a></td>
988
+ </tr>
989
+ <tr>
990
+ <td><a href="mailto:second@example.com">second@example.com</a></td>
991
+ </tr>
992
+ <tr>
993
+ <td><a href="mailto:third@example.com">third@example.com</a></td>
994
+ </tr>
995
+ <tr>
996
+ <td><a href="mailto:zero@example.com">zero@example.com</a></td>
997
+ </tr>
998
+ <tr>
999
+ <td>missing</td>
1000
+ </tr>
1001
+ </tbody>
1002
+ </table>
1003
+
1004
+ The link content can be overwriten by setting the ``link_content`` attribute:
1005
+
1006
+ >>> class StaticEMailColumn(column.EMailColumn):
1007
+ ...
1008
+ ... attr_name = 'email'
1009
+ ... default_value = 'missing'
1010
+ ... link_content = 'Mail me'
1011
+
1012
+ >>> class StaticEMailColumnTable(table.Table):
1013
+ ... css_class_sorted_on = None
1014
+ ...
1015
+ ... def setup_columns(self):
1016
+ ... return [
1017
+ ... column.add_column(self, StaticEMailColumn, 'mail'),
1018
+ ... ]
1019
+
1020
+ Render and update the table:
1021
+
1022
+ >>> request = DummyRequest()
1023
+ >>> staticEMailColumnTable = StaticEMailColumnTable(container, request)
1024
+ >>> staticEMailColumnTable.update()
1025
+ >>> print(staticEMailColumnTable.render())
1026
+ <table>
1027
+ <thead>
1028
+ <tr>
1029
+ <th>E-Mail</th>
1030
+ </tr>
1031
+ </thead>
1032
+ <tbody>
1033
+ <tr>
1034
+ <td><a href="mailto:first@example.com">Mail me</a></td>
1035
+ </tr>
1036
+ <tr>
1037
+ <td><a href="mailto:second@example.com">Mail me</a></td>
1038
+ </tr>
1039
+ <tr>
1040
+ <td><a href="mailto:third@example.com">Mail me</a></td>
1041
+ </tr>
1042
+ <tr>
1043
+ <td><a href="mailto:zero@example.com">Mail me</a></td>
1044
+ </tr>
1045
+ <tr>
1046
+ <td>missing</td>
1047
+ </tr>
1048
+ </tbody>
1049
+ </table>
1050
+
1051
+
1052
+ LinkColumn
1053
+ ----------
1054
+
1055
+ Let's define a table using the LinkColumn. This column allows us to write
1056
+ columns which can point to a page with the item as context:
1057
+
1058
+ >>> class MyLinkColumns(column.LinkColumn):
1059
+ ... link_name = 'myLink.html'
1060
+ ... link_target = '_blank'
1061
+ ... link_css = 'myClass'
1062
+ ... link_title = 'Click >'
1063
+
1064
+ >>> class MyLinkTable(table.Table):
1065
+ ... css_class_sorted_on = None
1066
+ ...
1067
+ ... def setup_columns(self):
1068
+ ... return [
1069
+ ... column.add_column(self, MyLinkColumns, 'link',
1070
+ ... weight=1),
1071
+ ... column.add_column(self, NumberColumn, name='number',
1072
+ ... weight=2, header='Number')
1073
+ ... ]
1074
+
1075
+ Now create, update and render our table:
1076
+
1077
+ >>> request = DummyRequest()
1078
+ >>> myLinkTable = MyLinkTable(container, request)
1079
+ >>> myLinkTable.__parent__ = container
1080
+ >>> myLinkTable.__name__ = 'myLinkTable.html'
1081
+ >>> myLinkTable.update()
1082
+ >>> print(myLinkTable.render())
1083
+ <table>
1084
+ <thead>
1085
+ <tr>
1086
+ <th>Name</th>
1087
+ <th>Number</th>
1088
+ </tr>
1089
+ </thead>
1090
+ <tbody>
1091
+ <tr>
1092
+ <td><a href="http://example.com/first/myLink.html" target="_blank" class="myClass" title="Click &gt;">first</a></td>
1093
+ <td>number: 1</td>
1094
+ </tr>
1095
+ <tr>
1096
+ <td><a href="http://example.com/fourth/myLink.html" target="_blank" class="myClass" title="Click &gt;">fourth</a></td>
1097
+ <td>number: 4</td>
1098
+ </tr>
1099
+ <tr>
1100
+ <td><a href="http://example.com/second/myLink.html" target="_blank" class="myClass" title="Click &gt;">second</a></td>
1101
+ <td>number: 2</td>
1102
+ </tr>
1103
+ <tr>
1104
+ <td><a href="http://example.com/third/myLink.html" target="_blank" class="myClass" title="Click &gt;">third</a></td>
1105
+ <td>number: 3</td>
1106
+ </tr>
1107
+ <tr>
1108
+ <td><a href="http://example.com/zero/myLink.html" target="_blank" class="myClass" title="Click &gt;">zero</a></td>
1109
+ <td>number: 0</td>
1110
+ </tr>
1111
+ </tbody>
1112
+ </table>
1113
+
1114
+
1115
+ ContentsLinkColumn
1116
+ ------------------
1117
+
1118
+ There are some predefined link columns available. This one will generate a
1119
+ ``contents.html`` link for each item:
1120
+
1121
+ >>> class ContentsLinkTable(table.Table):
1122
+ ... css_class_sorted_on = None
1123
+ ...
1124
+ ... def setup_columns(self):
1125
+ ... return [
1126
+ ... column.add_column(self, column.ContentsLinkColumn, 'link',
1127
+ ... weight=1),
1128
+ ... column.add_column(self, NumberColumn, name='number',
1129
+ ... weight=2, header='Number')
1130
+ ... ]
1131
+
1132
+ >>> contentsLinkTable = ContentsLinkTable(container, request)
1133
+ >>> contentsLinkTable.__parent__ = container
1134
+ >>> contentsLinkTable.__name__ = 'contentsLinkTable.html'
1135
+ >>> contentsLinkTable.update()
1136
+ >>> print(contentsLinkTable.render())
1137
+ <table>
1138
+ <thead>
1139
+ <tr>
1140
+ <th>Name</th>
1141
+ <th>Number</th>
1142
+ </tr>
1143
+ </thead>
1144
+ <tbody>
1145
+ <tr>
1146
+ <td><a href="http://example.com/first/contents.html">first</a></td>
1147
+ <td>number: 1</td>
1148
+ </tr>
1149
+ <tr>
1150
+ <td><a href="http://example.com/fourth/contents.html">fourth</a></td>
1151
+ <td>number: 4</td>
1152
+ </tr>
1153
+ <tr>
1154
+ <td><a href="http://example.com/second/contents.html">second</a></td>
1155
+ <td>number: 2</td>
1156
+ </tr>
1157
+ <tr>
1158
+ <td><a href="http://example.com/third/contents.html">third</a></td>
1159
+ <td>number: 3</td>
1160
+ </tr>
1161
+ <tr>
1162
+ <td><a href="http://example.com/zero/contents.html">zero</a></td>
1163
+ <td>number: 0</td>
1164
+ </tr>
1165
+ </tbody>
1166
+ </table>
1167
+
1168
+
1169
+ IndexLinkColumn
1170
+ ---------------
1171
+
1172
+ This one will generate a ``index.html`` link for each item:
1173
+
1174
+ >>> class IndexLinkTable(table.Table):
1175
+ ... css_class_sorted_on = None
1176
+ ...
1177
+ ... def setup_columns(self):
1178
+ ... return [
1179
+ ... column.add_column(self, column.IndexLinkColumn, 'link',
1180
+ ... weight=1),
1181
+ ... column.add_column(self, NumberColumn, name='number',
1182
+ ... weight=2, header='Number')
1183
+ ... ]
1184
+
1185
+ >>> indexLinkTable = IndexLinkTable(container, request)
1186
+ >>> indexLinkTable.__parent__ = container
1187
+ >>> indexLinkTable.__name__ = 'indexLinkTable.html'
1188
+ >>> indexLinkTable.update()
1189
+ >>> print(indexLinkTable.render())
1190
+ <table>
1191
+ <thead>
1192
+ <tr>
1193
+ <th>Name</th>
1194
+ <th>Number</th>
1195
+ </tr>
1196
+ </thead>
1197
+ <tbody>
1198
+ <tr>
1199
+ <td><a href="http://example.com/first/index.html">first</a></td>
1200
+ <td>number: 1</td>
1201
+ </tr>
1202
+ <tr>
1203
+ <td><a href="http://example.com/fourth/index.html">fourth</a></td>
1204
+ <td>number: 4</td>
1205
+ </tr>
1206
+ <tr>
1207
+ <td><a href="http://example.com/second/index.html">second</a></td>
1208
+ <td>number: 2</td>
1209
+ </tr>
1210
+ <tr>
1211
+ <td><a href="http://example.com/third/index.html">third</a></td>
1212
+ <td>number: 3</td>
1213
+ </tr>
1214
+ <tr>
1215
+ <td><a href="http://example.com/zero/index.html">zero</a></td>
1216
+ <td>number: 0</td>
1217
+ </tr>
1218
+ </tbody>
1219
+ </table>
1220
+
1221
+
1222
+ EditLinkColumn
1223
+ --------------
1224
+
1225
+ And this one will generate a ``edit.html`` link for each item:
1226
+
1227
+ >>> class EditLinkTable(table.Table):
1228
+ ... css_class_sorted_on = None
1229
+ ...
1230
+ ... def setup_columns(self):
1231
+ ... return [
1232
+ ... column.add_column(self, column.EditLinkColumn, 'link',
1233
+ ... weight=1),
1234
+ ... column.add_column(self, NumberColumn, name='number',
1235
+ ... weight=2, header='Number')
1236
+ ... ]
1237
+
1238
+ >>> editLinkTable = EditLinkTable(container, request)
1239
+ >>> editLinkTable.__parent__ = container
1240
+ >>> editLinkTable.__name__ = 'editLinkTable.html'
1241
+ >>> editLinkTable.update()
1242
+ >>> print(editLinkTable.render())
1243
+ <table>
1244
+ <thead>
1245
+ <tr>
1246
+ <th>Name</th>
1247
+ <th>Number</th>
1248
+ </tr>
1249
+ </thead>
1250
+ <tbody>
1251
+ <tr>
1252
+ <td><a href="http://example.com/first/edit.html">first</a></td>
1253
+ <td>number: 1</td>
1254
+ </tr>
1255
+ <tr>
1256
+ <td><a href="http://example.com/fourth/edit.html">fourth</a></td>
1257
+ <td>number: 4</td>
1258
+ </tr>
1259
+ <tr>
1260
+ <td><a href="http://example.com/second/edit.html">second</a></td>
1261
+ <td>number: 2</td>
1262
+ </tr>
1263
+ <tr>
1264
+ <td><a href="http://example.com/third/edit.html">third</a></td>
1265
+ <td>number: 3</td>
1266
+ </tr>
1267
+ <tr>
1268
+ <td><a href="http://example.com/zero/edit.html">zero</a></td>
1269
+ <td>number: 0</td>
1270
+ </tr>
1271
+ </tbody>
1272
+ </table>
1273
+
1274
+ >>> tearDown()