/usr/share/pyshared/zc/table/fieldcolumn.txt is in python-zc.table 0.8.0-0ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | The FieldColumn is intended to be a replacement for the FieldEditColumn. It
has the following goals.
- Support the standard column pattern of instantiating a single module global
column set, and using formatters to store and access information about a
single given rendering. The formatter has annotations, prefix, context, and
request, all of which are desired or even essential for various derived
columns. Generally, the formatter has state that the columns need.
- Support display widgets, in addition to input widgets.
- Support formlib form fields, to support custom widgets and other formlib
field features.
It also has an important design difference. Rather than relying on functions
passed at initialization for column customization, the field column returns to
a standard subclass design. The column expects that any of the following
methods may be overridden:
- getId(item, formatter): given an item whose value is to be displayed in the
column, return a form-friendly string identifier, unique to the item. We
define 'form-friendly' as containing characters that are alphanumeric or a
space, a plus, a slash, or an equals sign (that is, the standard characters
used in MIME-based base64, excluding new lines).
- get(item, formatter): return the current value for the item.
- set(item, value, formatter): set the given value for the item.
- getFieldContext(item, formatter): return a context to which a field should
be bound, or None.
- renderCell(item, formatter): the standard rendering of a cell
- renderHeader(formatter): the standard rendering of a column header.
Without subclassing, the column uses field.get and field.set to get and set
values, returns None for bound context, and just renders the field's widget for
the cell, and the title for the header. Let's look at the default behavior.
We'll use a modified version of the examples given for the FieldEditColumn in
column.txt.
To create a FieldColumn, you must minimally provide a schema field or form
field. If title is not provided, it is the field's title, or empty if the
field has no title. A explicit title of an empty string is acceptable and will
be honored. If name is not provided, it is the field's __name__.
Let's look at a simple example. We have a collection of contacts,
with names and email addresses:
>>> import re
>>> from zope import schema, interface
>>> class IContact(interface.Interface):
... name = schema.TextLine(title=u'Name')
... email = schema.TextLine(
... title=u'Email Address',
... constraint=re.compile('\w+@\w+([.]\w+)+$').match)
... salutation = schema.Choice(
... title=u'Salutation',
... values = ['Mr','Ms'],
... )
>>> class Contact:
... interface.implements(IContact)
... def __init__(self, id, name, email, salutation):
... self.id = id
... self.name = name
... self.email = email
... self.salutation = salutation
>>> contacts = (
... Contact('1', 'Bob Smith', 'bob@zope.com', 'Mr'),
... Contact('2', 'Sally Baker', 'sally@zope.com', 'Ms'),
... Contact('3', 'Jethro Tul', 'jethro@zope.com', 'Mr'),
... Contact('4', 'Joe Walsh', 'joe@zope.com', 'Mr'),
... )
We'll define columns that allow us to display and edit name and
email addresses.
>>> from zc.table import fieldcolumn
>>> class ContactColumn(fieldcolumn.FieldColumn):
... def getId(self, item, formatter):
... return fieldcolumn.toSafe(item.id)
...
>>> class BindingContactColumn(ContactColumn):
... def getFieldContext(self, item, formatter):
... return item
...
>>> columns = (ContactColumn(IContact["name"]),
... ContactColumn(IContact["email"]),
... BindingContactColumn(IContact["salutation"])
... )
Now, with this, we can create a table with input widgets. The columns don't
need a context other than the items themselves, so we ignore that part of the
table formatter instantiation:
>>> from zc import table
>>> import zope.publisher.browser
>>> request = zope.publisher.browser.TestRequest()
>>> context = None
>>> formatter = table.Formatter(
... context, request, contacts, columns=columns, prefix='test')
>>> print formatter()
<table>
<thead>
<tr>
<th>
Name
</th>
<th>
Email Address
</th>
<th>
Salutation
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input class="textType" id="test.1.name" name="test.1.name"
size="20" type="text" value="Bob Smith" />
</td>
<td>
<input class="textType" id="test.1.email" name="test.1.email"
size="20" type="text" value="bob@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.1.salutation" name="test.1.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.1.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.2.name" name="test.2.name"
size="20" type="text" value="Sally Baker" />
</td>
<td>
<input class="textType" id="test.2.email" name="test.2.email"
size="20" type="text" value="sally@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.2.salutation" name="test.2.salutation"
size="1" >
<option value="Mr">Mr</option>
<option selected="selected" value="Ms">Ms</option>
</select>
</div>
<input name="test.2.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.3.name" name="test.3.name"
size="20" type="text" value="Jethro Tul" />
</td>
<td>
<input class="textType" id="test.3.email" name="test.3.email"
size="20" type="text" value="jethro@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.3.salutation" name="test.3.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.3.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.4.name" name="test.4.name"
size="20" type="text" value="Joe Walsh" />
</td>
<td>
<input class="textType" id="test.4.email" name="test.4.email"
size="20" type="text" value="joe@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.4.salutation" name="test.4.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.4.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
</tbody>
</table>
Note that the input names do not include base64 encodings of the item ids
because they already match the necessary constraints.
If the request has input for a value, then this will override item data:
>>> request.form["test.4.email"] = u'walsh@zope.com'
>>> print formatter()
<table>
<thead>
<tr>
<th>
Name
</th>
<th>
Email Address
</th>
<th>
Salutation
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input class="textType" id="test.1.name" name="test.1.name"
size="20" type="text" value="Bob Smith" />
</td>
<td>
<input class="textType" id="test.1.email" name="test.1.email"
size="20" type="text" value="bob@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.1.salutation" name="test.1.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.1.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.2.name" name="test.2.name"
size="20" type="text" value="Sally Baker" />
</td>
<td>
<input class="textType" id="test.2.email" name="test.2.email"
size="20" type="text" value="sally@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.2.salutation" name="test.2.salutation"
size="1" >
<option value="Mr">Mr</option>
<option selected="selected" value="Ms">Ms</option>
</select>
</div>
<input name="test.2.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.3.name" name="test.3.name"
size="20" type="text" value="Jethro Tul" />
</td>
<td>
<input class="textType" id="test.3.email" name="test.3.email"
size="20" type="text" value="jethro@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.3.salutation" name="test.3.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.3.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
<tr>
<td>
<input class="textType" id="test.4.name" name="test.4.name"
size="20" type="text" value="Joe Walsh" />
</td>
<td>
<input class="textType" id="test.4.email" name="test.4.email"
size="20" type="text" value="walsh@zope.com" />
</td>
<td>
<div>
<div class="value">
<select id="test.4.salutation" name="test.4.salutation"
size="1" >
<option selected="selected" value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select>
</div>
<input name="test.4.salutation-empty-marker" type="hidden"
value="1" />
</div>
</td>
</tr>
</tbody>
</table>
and the contact data is unchanged:
>>> contacts[3].email
'joe@zope.com'
Field edit columns provide methods for getting and validating input
data, and for updating the undelying data:
>>> data = columns[1].input(contacts, formatter)
>>> data
{'4': u'walsh@zope.com'}
The data returned is a mapping from item id to input value. Items
that don't have input are ignored. The data can be used with the
update function to update the underlying data:
>>> columns[1].update(contacts, data, formatter)
True
>>> contacts[3].email
u'walsh@zope.com'
Note that the update function returns a boolean value indicating
whether any changes were made:
>>> columns[1].update(contacts, data, formatter)
False
The input function also validates input. If there are any errors, a
WidgetsError will be raised:
>>> request.form["test.4.email"] = u'walsh'
>>> data = columns[1].input(contacts, formatter)
Traceback (most recent call last):
...
WidgetsError: WidgetInputError:
('email', u'Email Address', ConstraintNotSatisfied(u'walsh'))
Custom getters and setters
--------------------------
Normally, the given fields getter and setter is used, however, custom
getters and setters can be provided. Let's look at an example of
a bit table:
>>> data = [0, 0], [1, 1], [2, 2], [3, 3]
>>> class BitColumn(fieldcolumn.FieldColumn):
... def __init__(self, field, bit, title=None, name=''):
... super(BitColumn, self).__init__(field, title, name)
... self.bit = bit
... def getId(self, item, formatter):
... return str(item[0])
... def get(self, item, formatter):
... return (1 << self.bit)&(item[1])
... def set(self, item, value, formatter):
... value = bool(value) << self.bit
... mask = 1 << self.bit
... item[1] = ((item[1] | mask) ^ mask) | value
>>> columns = (
... BitColumn(schema.Bool(__name__='0', title=u'Bit 0'), 0),
... BitColumn(schema.Bool(__name__='1', title=u'Bit 1'), 1))
>>> context = None # not needed
>>> request = zope.publisher.browser.TestRequest()
>>> formatter = table.Formatter(
... context, request, data, columns=columns, prefix='test')
>>> print formatter()
<table>
<thead>
<tr>
<th>
Bit 0
</th>
<th>
Bit 1
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input class="hiddenType" id="test.0.0.used"
name="test.0.0.used" type="hidden" value="" />
<input class="checkboxType" id="test.0.0"
name="test.0.0" type="checkbox" value="on" />
</td>
<td>
<input class="hiddenType" id="test.0.1.used"
name="test.0.1.used" type="hidden" value="" />
<input class="checkboxType" id="test.0.1"
name="test.0.1" type="checkbox" value="on" />
</td>
</tr>
<tr>
<td>
<input class="hiddenType" id="test.1.0.used"
name="test.1.0.used" type="hidden" value="" />
<input class="checkboxType" checked="checked" id="test.1.0"
name="test.1.0" type="checkbox" value="on" />
</td>
<td>
<input class="hiddenType" id="test.1.1.used"
name="test.1.1.used" type="hidden" value="" />
<input class="checkboxType" id="test.1.1"
name="test.1.1" type="checkbox" value="on" />
</td>
</tr>
<tr>
<td>
<input class="hiddenType" id="test.2.0.used"
name="test.2.0.used" type="hidden" value="" />
<input class="checkboxType" id="test.2.0"
name="test.2.0" type="checkbox" value="on" />
</td>
<td>
<input class="hiddenType" id="test.2.1.used"
name="test.2.1.used" type="hidden" value="" />
<input class="checkboxType" checked="checked" id="test.2.1"
name="test.2.1" type="checkbox" value="on" />
</td>
</tr>
<tr>
<td>
<input class="hiddenType" id="test.3.0.used"
name="test.3.0.used" type="hidden" value="" />
<input class="checkboxType" checked="checked" id="test.3.0"
name="test.3.0" type="checkbox" value="on" />
</td>
<td>
<input class="hiddenType" id="test.3.1.used"
name="test.3.1.used" type="hidden" value="" />
<input class="checkboxType" checked="checked" id="test.3.1"
name="test.3.1" type="checkbox" value="on" />
</td>
</tr>
</tbody>
</table>
>>> request.form["test.3.1.used"] = ""
>>> request.form["test.0.1.used"] = ""
>>> request.form["test.0.1"] = "on"
>>> input = columns[1].input(data, formatter)
>>> from zope.testing.doctestunit import pprint
>>> pprint(input)
{'0': True,
'3': False}
>>> columns[1].update(data, input, formatter)
True
>>> data
([0, 2], [1, 1], [2, 2], [3, 1])
|