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,388 @@
1
+ Sorting Table
2
+ -------------
3
+
4
+ Another table feature is the support for sorting data given from columns. Since
5
+ sorting table data is an important feature, we offer this by default. But it
6
+ only gets used if there is a ``sort-on`` value set. You can set this value at
7
+ class level by adding a ``defaultSortOn`` value or set it as a request value.
8
+ We show you how to do this later. We also need a columns which allows us to do
9
+ a better sort sample. Our new sorting column will use the content items number
10
+ value for sorting:
11
+
12
+ >>> from pyramid.testing import setUp, tearDown, DummyRequest
13
+ >>> from pyams_table.testing import setup_adapters
14
+
15
+ >>> config = setUp()
16
+ >>> setup_adapters(config.registry)
17
+ >>> root = {}
18
+
19
+ >>> from pyams_table import column, table
20
+ >>> class NumberColumn(column.Column):
21
+ ...
22
+ ... header = 'Number'
23
+ ... weight = 20
24
+ ...
25
+ ... def get_sort_key(self, item):
26
+ ... return item.number
27
+ ...
28
+ ... def render_cell(self, item):
29
+ ... return 'number: %s' % item.number
30
+
31
+
32
+ Now let's set up a table:
33
+
34
+ >>> from pyams_table.testing import TitleColumn
35
+ >>> class SortingTable(table.Table):
36
+ ...
37
+ ... def setup_columns(self):
38
+ ... firstColumn = TitleColumn(self.context, self.request, self)
39
+ ... firstColumn.__name__ = 'title'
40
+ ... firstColumn.__parent__ = self
41
+ ... secondColumn = NumberColumn(self.context, self.request, self)
42
+ ... secondColumn.__name__ = 'number'
43
+ ... secondColumn.__parent__ = self
44
+ ... return [firstColumn, secondColumn]
45
+
46
+ Create a container:
47
+
48
+ >>> from pyams_table.testing import OrderedContainer
49
+ >>> container = OrderedContainer()
50
+
51
+ We also need some container items that we can use for sorting:
52
+
53
+ >>> from pyams_table.testing import Content
54
+ >>> container['first'] = Content('First', 1)
55
+ >>> container['second'] = Content('Second', 2)
56
+ >>> container['third'] = Content('Third', 3)
57
+ >>> container['fourth'] = Content('Fourth', 4)
58
+ >>> container['zero'] = Content('Zero', 0)
59
+
60
+ And render them without set a ``sort-on`` value:
61
+
62
+ >>> request = DummyRequest()
63
+ >>> sortingTable = SortingTable(container, request)
64
+ >>> sortingTable.update()
65
+ >>> print(sortingTable.render())
66
+ <table>
67
+ <thead>
68
+ <tr>
69
+ <th class="sorted-on ascending">Title</th>
70
+ <th>Number</th>
71
+ </tr>
72
+ </thead>
73
+ <tbody>
74
+ <tr>
75
+ <td class="sorted-on ascending">Title: First</td>
76
+ <td>number: 1</td>
77
+ </tr>
78
+ <tr>
79
+ <td class="sorted-on ascending">Title: Fourth</td>
80
+ <td>number: 4</td>
81
+ </tr>
82
+ <tr>
83
+ <td class="sorted-on ascending">Title: Second</td>
84
+ <td>number: 2</td>
85
+ </tr>
86
+ <tr>
87
+ <td class="sorted-on ascending">Title: Third</td>
88
+ <td>number: 3</td>
89
+ </tr>
90
+ <tr>
91
+ <td class="sorted-on ascending">Title: Zero</td>
92
+ <td>number: 0</td>
93
+ </tr>
94
+ </tbody>
95
+ </table>
96
+
97
+ Ooops, well, by default the table is sorted on the first column, ascending.
98
+
99
+ >>> sortingTable.sort_on
100
+ 0
101
+
102
+ Now switch off sorting, now we get the original order:
103
+
104
+ >>> sortingTable.sort_on = None
105
+ >>> sortingTable.update()
106
+ >>> print(sortingTable.render())
107
+ <table>
108
+ <thead>
109
+ <tr>
110
+ <th>Title</th>
111
+ <th>Number</th>
112
+ </tr>
113
+ </thead>
114
+ <tbody>
115
+ <tr>
116
+ <td>Title: First</td>
117
+ <td>number: 1</td>
118
+ </tr>
119
+ <tr>
120
+ <td>Title: Second</td>
121
+ <td>number: 2</td>
122
+ </tr>
123
+ <tr>
124
+ <td>Title: Third</td>
125
+ <td>number: 3</td>
126
+ </tr>
127
+ <tr>
128
+ <td>Title: Fourth</td>
129
+ <td>number: 4</td>
130
+ </tr>
131
+ <tr>
132
+ <td>Title: Zero</td>
133
+ <td>number: 0</td>
134
+ </tr>
135
+ </tbody>
136
+ </table>
137
+
138
+
139
+ As you can see this table doesn't provide any explicit order. Let's find out
140
+ the index of our column that we like to sort on:
141
+
142
+ >>> sortOnId = sortingTable.rows[0][1][1].id
143
+ >>> sortOnId
144
+ 'table-number-1'
145
+
146
+ And let's use this id as ``sort-on`` value:
147
+
148
+ >>> sortingTable.sort_on = sortOnId
149
+
150
+ An important thing is to update the table after set an ``sort-on`` value:
151
+
152
+ >>> sortingTable.update()
153
+ >>> print(sortingTable.render())
154
+ <table>
155
+ <thead>
156
+ <tr>
157
+ <th>Title</th>
158
+ <th class="sorted-on ascending">Number</th>
159
+ </tr>
160
+ </thead>
161
+ <tbody>
162
+ <tr>
163
+ <td>Title: Zero</td>
164
+ <td class="sorted-on ascending">number: 0</td>
165
+ </tr>
166
+ <tr>
167
+ <td>Title: First</td>
168
+ <td class="sorted-on ascending">number: 1</td>
169
+ </tr>
170
+ <tr>
171
+ <td>Title: Second</td>
172
+ <td class="sorted-on ascending">number: 2</td>
173
+ </tr>
174
+ <tr>
175
+ <td>Title: Third</td>
176
+ <td class="sorted-on ascending">number: 3</td>
177
+ </tr>
178
+ <tr>
179
+ <td>Title: Fourth</td>
180
+ <td class="sorted-on ascending">number: 4</td>
181
+ </tr>
182
+ </tbody>
183
+ </table>
184
+
185
+ We can also reverse the sorting order:
186
+
187
+ >>> sortingTable.sort_order = 'reverse'
188
+ >>> sortingTable.update()
189
+ >>> print(sortingTable.render())
190
+ <table>
191
+ <thead>
192
+ <tr>
193
+ <th>Title</th>
194
+ <th class="sorted-on reverse">Number</th>
195
+ </tr>
196
+ </thead>
197
+ <tbody>
198
+ <tr>
199
+ <td>Title: Fourth</td>
200
+ <td class="sorted-on reverse">number: 4</td>
201
+ </tr>
202
+ <tr>
203
+ <td>Title: Third</td>
204
+ <td class="sorted-on reverse">number: 3</td>
205
+ </tr>
206
+ <tr>
207
+ <td>Title: Second</td>
208
+ <td class="sorted-on reverse">number: 2</td>
209
+ </tr>
210
+ <tr>
211
+ <td>Title: First</td>
212
+ <td class="sorted-on reverse">number: 1</td>
213
+ </tr>
214
+ <tr>
215
+ <td>Title: Zero</td>
216
+ <td class="sorted-on reverse">number: 0</td>
217
+ </tr>
218
+ </tbody>
219
+ </table>
220
+
221
+ The table implementation is also able to get the sorting criteria given from a
222
+ request. Let's setup such a request:
223
+
224
+ >>> sorterRequest = DummyRequest(params={'table-sort-on': 'table-number-1',
225
+ ... 'table-sort-order':'descending'})
226
+
227
+ and another time, update and render. As you can see the new table gets sorted
228
+ by the second column and ordered in reverse order:
229
+
230
+ >>> requestSortedTable = SortingTable(container, sorterRequest)
231
+ >>> requestSortedTable.update()
232
+ >>> print(requestSortedTable.render())
233
+ <table>
234
+ <thead>
235
+ <tr>
236
+ <th>Title</th>
237
+ <th class="sorted-on descending">Number</th>
238
+ </tr>
239
+ </thead>
240
+ <tbody>
241
+ <tr>
242
+ <td>Title: Fourth</td>
243
+ <td class="sorted-on descending">number: 4</td>
244
+ </tr>
245
+ <tr>
246
+ <td>Title: Third</td>
247
+ <td class="sorted-on descending">number: 3</td>
248
+ </tr>
249
+ <tr>
250
+ <td>Title: Second</td>
251
+ <td class="sorted-on descending">number: 2</td>
252
+ </tr>
253
+ <tr>
254
+ <td>Title: First</td>
255
+ <td class="sorted-on descending">number: 1</td>
256
+ </tr>
257
+ <tr>
258
+ <td>Title: Zero</td>
259
+ <td class="sorted-on descending">number: 0</td>
260
+ </tr>
261
+ </tbody>
262
+ </table>
263
+
264
+ There's a header renderer, which provides a handy link rendering for sorting:
265
+
266
+ >>> from pyams_table import interfaces
267
+ >>> from pyams_table.header import SortingColumnHeader
268
+ >>> config.registry.registerAdapter(SortingColumnHeader,
269
+ ... (None, None, interfaces.ITable, interfaces.IColumn),
270
+ ... provided=interfaces.IColumnHeader)
271
+
272
+ Let's see now various sortings:
273
+
274
+ >>> request = DummyRequest()
275
+ >>> sortingTable = SortingTable(container, request)
276
+ >>> sortingTable.update()
277
+ >>> sortingTable.sort_on
278
+ 0
279
+ >>> sortingTable.sort_order
280
+ 'ascending'
281
+ >>> print(sortingTable.render())
282
+ <table>
283
+ <thead>
284
+ <tr>
285
+ <th class="sorted-on ascending"><a href="?table-sort-on=table-title-0&table-sort-order=descending" title="Sort">Title</a></th>
286
+ <th><a href="?table-sort-on=table-number-1&table-sort-order=ascending" title="Sort">Number</a></th>
287
+ </tr>
288
+ </thead>
289
+ <tbody>
290
+ <tr>
291
+ <td class="sorted-on ascending">Title: First</td>
292
+ <td>number: 1</td>
293
+ </tr>
294
+ <tr>
295
+ <td class="sorted-on ascending">Title: Fourth</td>
296
+ <td>number: 4</td>
297
+ </tr>
298
+ <tr>
299
+ <td class="sorted-on ascending">Title: Second</td>
300
+ <td>number: 2</td>
301
+ </tr>
302
+ <tr>
303
+ <td class="sorted-on ascending">Title: Third</td>
304
+ <td>number: 3</td>
305
+ </tr>
306
+ <tr>
307
+ <td class="sorted-on ascending">Title: Zero</td>
308
+ <td>number: 0</td>
309
+ </tr>
310
+ </tbody>
311
+ </table>
312
+
313
+ Let's see the `number` column:
314
+
315
+ >>> sortingTable.sort_on = 'table-number-1'
316
+
317
+ >>> sortingTable.update()
318
+ >>> print(sortingTable.render())
319
+ <table>
320
+ <thead>
321
+ <tr>
322
+ <th><a href="?table-sort-on=table-title-0&table-sort-order=ascending" title="Sort">Title</a></th>
323
+ <th class="sorted-on ascending"><a href="?table-sort-on=table-number-1&table-sort-order=descending" title="Sort">Number</a></th>
324
+ </tr>
325
+ </thead>
326
+ <tbody>
327
+ <tr>
328
+ <td>Title: Zero</td>
329
+ <td class="sorted-on ascending">number: 0</td>
330
+ </tr>
331
+ <tr>
332
+ <td>Title: First</td>
333
+ <td class="sorted-on ascending">number: 1</td>
334
+ </tr>
335
+ <tr>
336
+ <td>Title: Second</td>
337
+ <td class="sorted-on ascending">number: 2</td>
338
+ </tr>
339
+ <tr>
340
+ <td>Title: Third</td>
341
+ <td class="sorted-on ascending">number: 3</td>
342
+ </tr>
343
+ <tr>
344
+ <td>Title: Fourth</td>
345
+ <td class="sorted-on ascending">number: 4</td>
346
+ </tr>
347
+ </tbody>
348
+ </table>
349
+
350
+ Let's see the `title` column but descending:
351
+
352
+ >>> sortingTable.sort_on = 'table-title-0'
353
+ >>> sortingTable.sort_order = 'descending'
354
+
355
+ >>> sortingTable.update()
356
+ >>> print(sortingTable.render())
357
+ <table>
358
+ <thead>
359
+ <tr>
360
+ <th class="sorted-on descending"><a href="?table-sort-on=table-title-0&table-sort-order=ascending" title="Sort">Title</a></th>
361
+ <th><a href="?table-sort-on=table-number-1&table-sort-order=descending" title="Sort">Number</a></th>
362
+ </tr>
363
+ </thead>
364
+ <tbody>
365
+ <tr>
366
+ <td class="sorted-on descending">Title: Zero</td>
367
+ <td>number: 0</td>
368
+ </tr>
369
+ <tr>
370
+ <td class="sorted-on descending">Title: Third</td>
371
+ <td>number: 3</td>
372
+ </tr>
373
+ <tr>
374
+ <td class="sorted-on descending">Title: Second</td>
375
+ <td>number: 2</td>
376
+ </tr>
377
+ <tr>
378
+ <td class="sorted-on descending">Title: Fourth</td>
379
+ <td>number: 4</td>
380
+ </tr>
381
+ <tr>
382
+ <td class="sorted-on descending">Title: First</td>
383
+ <td>number: 1</td>
384
+ </tr>
385
+ </tbody>
386
+ </table>
387
+
388
+ >>> tearDown()
pyams_table/header.py ADDED
@@ -0,0 +1,102 @@
1
+ #
2
+ # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """PyAMS_table.header module
14
+
15
+ This module defines columns headers.
16
+ """
17
+
18
+ from urllib.parse import urlencode
19
+
20
+ from zope.interface import implementer
21
+
22
+ from pyams_table.interfaces import IColumnHeader
23
+
24
+
25
+ __docformat__ = "reStructuredText"
26
+
27
+ from pyams_table import _
28
+
29
+
30
+ @implementer(IColumnHeader)
31
+ class ColumnHeader:
32
+ """ColumnHeader renderer provider"""
33
+
34
+ _request_args = []
35
+
36
+ def __init__(self, context, request, table, column):
37
+ self.__parent__ = context
38
+ self.context = context
39
+ self.request = request
40
+ self.table = table
41
+ self.column = column
42
+
43
+ def update(self):
44
+ """Override this method in subclasses if required"""
45
+
46
+ def render(self):
47
+ """Override this method in subclasses"""
48
+ return self.column.header
49
+
50
+ def get_query_string_args(self):
51
+ """
52
+ Collect additional terms from the request and include in sorting column
53
+ headers
54
+
55
+ Perhaps this should be in separate interface only for sorting headers?
56
+
57
+ """
58
+ args = {}
59
+ for key in self._request_args:
60
+ value = self.request.params.get(key, None)
61
+ if value:
62
+ args.update({key: value})
63
+ return args
64
+
65
+
66
+ class SortingColumnHeader(ColumnHeader):
67
+ """Sorting column header."""
68
+
69
+ def render(self):
70
+ table = self.table
71
+ prefix = table.prefix
72
+ col_id = self.column.id
73
+
74
+ # this may return a string 'id-name-idx' if coming from request,
75
+ # otherwise in Table class it is intialised as a integer string
76
+ current_sort_id = table.get_sort_on()
77
+ try:
78
+ current_sort_id = int(current_sort_id)
79
+ except ValueError:
80
+ current_sort_id = current_sort_id.rsplit("-", 1)[-1]
81
+
82
+ current_sort_order = table.get_sort_order()
83
+
84
+ sort_id = col_id.rsplit("-", 1)[-1]
85
+
86
+ sort_order = table.sort_order
87
+ if int(sort_id) == int(current_sort_id):
88
+ # ordering the same column so we want to reverse the order
89
+ if current_sort_order in table.reverse_sort_order_names:
90
+ sort_order = "ascending"
91
+ elif current_sort_order == "ascending":
92
+ sort_order = table.reverse_sort_order_names[0]
93
+
94
+ args = self.get_query_string_args()
95
+ args.update(
96
+ {"%s-sort-on" % prefix: col_id, "%s-sort-order" % prefix: sort_order}
97
+ )
98
+ query_string = "?%s" % (urlencode(sorted(args.items())))
99
+
100
+ translate = self.request.localizer.translate
101
+ return '<a href="%s" title="%s">%s</a>' % (
102
+ query_string, translate(_("Sort")), translate(self.column.header))
pyams_table/include.py ADDED
@@ -0,0 +1,27 @@
1
+ #
2
+ # Copyright (c) 2015-2019 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """PyAMS_table.include module
14
+
15
+ This module is used for Pyramid integration
16
+ """
17
+
18
+ __docformat__ = 'restructuredtext'
19
+
20
+
21
+ def include_package(config):
22
+ """Pyramid package include"""
23
+
24
+ # add translations
25
+ config.add_translation_dirs('pyams_table:locales')
26
+
27
+ config.scan()