Changeset 196:80e9a80845ba for wokkel
- Timestamp:
- Oct 3, 2016, 8:49:10 AM (6 years ago)
- Branch:
- default
- rebase_source:
- 125a984a608f7e698bde26531ed1306885cb8bf6
- Location:
- wokkel
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/data_form.py
r182 r196 13 13 """ 14 14 15 from zope.interface import implements 15 from __future__ import division, absolute_import 16 17 from zope.interface import implementer 16 18 from zope.interface.common import mapping 19 20 from twisted.python.compat import iteritems, unicode, _PY3 17 21 from twisted.words.protocols.jabber.jid import JID 18 22 from twisted.words.xish import domish … … 48 52 49 53 @ivar value: Value of this option. 50 @type value: C{unicode}54 @type value: L{unicode} 51 55 @ivar label: Optional label for this option. 52 @type label: C{unicode} or C{NoneType}56 @type label: L{unicode} or L{NoneType} 53 57 """ 54 58 … … 101 105 102 106 The default is C{'text-single'}. 103 @type fieldType: C{str}107 @type fieldType: L{str} 104 108 @ivar var: Field name. Optional if C{fieldType} is C{'fixed'}. 105 @type var: C{str}109 @type var: L{str} 106 110 @ivar label: Human readable label for this field. 107 @type label: C{unicode}111 @type label: L{unicode} 108 112 @ivar values: The values for this field, for multi-valued field 109 types, as a list of C{bool}, C{unicode} or L{JID}.110 @type values: C{list}113 types, as a list of L{bool}, L{unicode} or L{JID}. 114 @type values: L{list} 111 115 @ivar options: List of possible values to choose from in a response 112 116 to this form as a list of L{Option}s. 113 @type options: C{list}117 @type options: L{list} 114 118 @ivar desc: Human readable description for this field. 115 @type desc: C{unicode}119 @type desc: L{unicode} 116 120 @ivar required: Whether the field is required to be provided in a 117 121 response to this form. 118 @type required: C{bool}122 @type required: L{bool} 119 123 """ 120 124 … … 127 131 See the identically named instance variables for descriptions. 128 132 129 If C{value} is not C{None}, it overrides C{values}, setting the133 If C{value} is not L{None}, it overrides C{values}, setting the 130 134 given value as the only value for this field. 131 135 """ … … 144 148 self.options = [Option(optionValue, optionLabel) 145 149 for optionValue, optionLabel 146 in options.iteritems()]150 in iteritems(options)] 147 151 except AttributeError: 148 152 self.options = options or [] … … 182 186 Sets C{value} as the only element of L{values}. 183 187 184 @type value: C{bool}, C{unicode} or L{JID}188 @type value: L{bool}, L{unicode} or L{JID} 185 189 """ 186 190 self.values = [value] … … 191 195 Getter of value property. 192 196 193 Returns the first element of L{values}, if present, or C{None}.197 Returns the first element of L{values}, if present, or L{None}. 194 198 """ 195 199 … … 310 314 field = Field(None) 311 315 312 for eAttr, fAttr in {'type': 'fieldType',313 'var': 'var',314 'label': 'label'}.iteritems():316 for eAttr, fAttr in iteritems({'type': 'fieldType', 317 'var': 'var', 318 'label': 'label'}): 315 319 value = element.getAttribute(eAttr) 316 320 if value: … … 347 351 if 'options' in fieldDict: 348 352 options = [] 349 for value, label in fieldDict['options'].iteritems():353 for value, label in iteritems(fieldDict['options']): 350 354 options.append(Option(value, label)) 351 355 kwargs['options'] = options … … 354 358 355 359 360 361 @implementer(mapping.IIterableMapping, 362 mapping.IEnumerableMapping, 363 mapping.IReadMapping, 364 mapping.IItemMapping) 356 365 357 366 class Form(object): … … 374 383 @ivar formType: Type of form. One of C{'form'}, C{'submit'}, {'cancel'}, 375 384 or {'result'}. 376 @type formType: C{str}385 @type formType: L{str} 377 386 378 387 @ivar title: Natural language title of the form. 379 @type title: C{unicode}380 381 @ivar instructions: Natural language instructions as a list of C{unicode}388 @type title: L{unicode} 389 390 @ivar instructions: Natural language instructions as a list of L{unicode} 382 391 strings without line breaks. 383 @type instructions: C{list}392 @type instructions: L{list} 384 393 385 394 @ivar formNamespace: The optional namespace of the field names for this 386 395 form. This goes in the special field named C{'FORM_TYPE'}, if set. 387 @type formNamespace: C{str}396 @type formNamespace: L{str} 388 397 389 398 @ivar fields: Dictionary of named fields. Note that this is meant to be 390 399 used for reading, only. One should use L{addField} or L{makeFields} and 391 400 L{removeField} for adding and removing fields. 392 @type fields: C{dict}401 @type fields: L{dict} 393 402 394 403 @ivar fieldList: List of all fields, in the order they are added. Like 395 404 C{fields}, this is meant to be used for reading, only. 396 @type fieldList: C{list} 397 """ 398 399 implements(mapping.IIterableMapping, 400 mapping.IEnumerableMapping, 401 mapping.IReadMapping, 402 mapping.IItemMapping) 405 @type fieldList: L{list} 406 """ 403 407 404 408 def __init__(self, formType, title=None, instructions=None, … … 469 473 this form. It is typically used for generating outgoing forms. 470 474 471 If C{fieldDefs} is not C{None}, this is used to fill in475 If C{fieldDefs} is not L{None}, this is used to fill in 472 476 additional properties of fields, like the field types, labels and 473 477 possible options. 474 478 475 If C{filterUnknown} is C{True} and C{fieldDefs} is not C{None}, fields479 If C{filterUnknown} is L{True} and C{fieldDefs} is not L{None}, fields 476 480 will only be created from C{values} with a corresponding entry in 477 481 C{fieldDefs}. 478 482 479 If the field type is unknown, the field type is C{None}. When the form483 If the field type is unknown, the field type is L{None}. When the form 480 484 is rendered using L{toElement}, these fields will have no C{'type'} 481 485 attribute, and it is up to the receiving party to interpret the values … … 484 488 485 489 @param values: Values to create fields from. 486 @type values: C{dict}490 @type values: L{dict} 487 491 488 492 @param fieldDefs: Field definitions as a dictionary. See 489 493 L{wokkel.iwokkel.IPubSubService.getConfigurationOptions} 490 @type fieldDefs: C{dict}491 492 @param filterUnknown: If C{True}, ignore fields that are not in494 @type fieldDefs: L{dict} 495 496 @param filterUnknown: If L{True}, ignore fields that are not in 493 497 C{fieldDefs}. 494 @type filterUnknown: C{bool}495 """ 496 for name, value in values.iteritems():498 @type filterUnknown: L{bool} 499 """ 500 for name, value in iteritems(values): 497 501 fieldDict = {'var': name, 498 502 'type': None} … … 634 638 yield (key, self[key]) 635 639 636 637 def keys(self): 638 return list(self) 639 640 641 def values(self): 642 return list(self.itervalues()) 643 644 645 def items(self): 646 return list(self.iteritems()) 640 if _PY3: 641 keys = iterkeys 642 values = itervalues 643 items = iteritems 644 else: 645 def keys(self): 646 return list(self) 647 648 649 def values(self): 650 return list(self.itervalues()) 651 652 653 def items(self): 654 return list(self.iteritems()) 647 655 648 656 … … 653 661 For all named fields, the corresponding value or values are 654 662 returned in a dictionary keyed by the field name. This is equivalent 655 do C{dict(f)}, where C{f} is a L{Form}.663 do L{dict(f)}, where C{f} is a L{Form}. 656 664 657 665 @see: L{__getitem__} 658 @rtype: C{dict}666 @rtype: L{dict} 659 667 """ 660 668 return dict(self) … … 669 677 field definition in C{fieldDefs} is used to check the field type. 670 678 671 If C{filterUnknown} is C{True}, fields that are not present in679 If C{filterUnknown} is L{True}, fields that are not present in 672 680 C{fieldDefs} are removed from the form. 673 681 674 If the field type is C{None} (when not set by the sending entity),682 If the field type is L{None} (when not set by the sending entity), 675 683 the type from the field definitition is used, or C{'text-single'} if 676 684 that is not set. … … 682 690 @param fieldDefs: Field definitions as a dictionary. See 683 691 L{wokkel.iwokkel.IPubSubService.getConfigurationOptions} 684 @type fieldDefs: C{dict}685 686 @param filterUnknown: If C{True}, remove fields that are not in692 @type fieldDefs: L{dict} 693 694 @param filterUnknown: If L{True}, remove fields that are not in 687 695 C{fieldDefs}. 688 @type filterUnknown: C{bool}696 @type filterUnknown: L{bool} 689 697 """ 690 698 … … 694 702 filtered = [] 695 703 696 for name, field in self.fields.iteritems():704 for name, field in iteritems(self.fields): 697 705 if name in fieldDefs: 698 706 fieldDef = fieldDefs[name] -
wokkel/test/test_data_form.py
r105 r196 6 6 """ 7 7 8 from __future__ import division, absolute_import 9 8 10 from zope.interface import verify 9 11 from zope.interface.common.mapping import IIterableMapping 10 12 13 from twisted.python.compat import unicode, _PY3 11 14 from twisted.trial import unittest 12 15 from twisted.words.xish import domish … … 1135 1138 form = data_form.Form('submit', fields=fields) 1136 1139 keys = form.keys() 1137 self.assertIsInstance(keys, list) 1140 if not _PY3: 1141 self.assertIsInstance(keys, list) 1138 1142 self.assertEqual(set(['botname', 'public', 'features']), 1139 1143 set(keys)) … … 1148 1152 form = data_form.Form('submit', fields=fields) 1149 1153 values = form.values() 1150 self.assertIsInstance(values, list) 1154 if not _PY3: 1155 self.assertIsInstance(values, list) 1151 1156 self.assertEqual(set(['The Jabber Bot', True]), set(values)) 1152 1157 … … 1160 1165 form = data_form.Form('submit', fields=fields) 1161 1166 items = form.items() 1162 self.assertIsInstance(items, list) 1167 if not _PY3: 1168 self.assertIsInstance(items, list) 1163 1169 self.assertEqual(set([('botname', 'The Jabber Bot'), 1164 1170 ('public', True)]),
Note: See TracChangeset
for help on using the changeset viewer.