Changeset 29:dd4e908b9d12 for wokkel/data_form.py
- Timestamp:
- Aug 4, 2008, 8:55:45 AM (14 years ago)
- Branch:
- default
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@59
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/data_form.py
r25 r29 204 204 205 205 206 def toElement(self): 207 """ 208 Return the DOM representation of this Field. 209 210 @rtype L{domish.Element}. 206 def typeCheck(self): 207 """ 208 Check field properties agains the set field type. 211 209 """ 212 210 if self.var is None and self.fieldType != 'fixed': 213 211 raise FieldNameRequiredError() 214 215 field = domish.Element((NS_X_DATA, 'field'))216 field['type'] = self.fieldType217 218 if self.var is not None:219 field['var'] = self.var220 212 221 213 if self.values: … … 225 217 raise TooManyValuesError() 226 218 219 newValues = [] 227 220 for value in self.values: 228 221 if self.fieldType == 'boolean': 229 222 # We send out the textual representation of boolean values 230 value = unicode(bool(value)).lower()223 value = bool(int(value)) 231 224 elif self.fieldType in ('jid-single', 'jid-multi'): 232 225 value = value.full() 233 226 234 field.addElement('value', content=value) 227 newValues.append(value) 228 229 self.values = newValues 230 231 def toElement(self): 232 """ 233 Return the DOM representation of this Field. 234 235 @rtype L{domish.Element}. 236 """ 237 238 self.typeCheck() 239 240 field = domish.Element((NS_X_DATA, 'field')) 241 field['type'] = self.fieldType 242 243 if self.var is not None: 244 field['var'] = self.var 245 246 for value in self.values: 247 if self.fieldType == 'boolean': 248 value = unicode(value).lower() 249 field.addElement('value', content=value) 235 250 236 251 if self.fieldType in ('list-single', 'list-multi'): … … 302 317 @staticmethod 303 318 def fromDict(dictionary): 319 kwargs = dictionary.copy() 320 304 321 if 'type' in dictionary: 305 dictionary['fieldType'] = dictionary['type'] 306 del dictionary['type'] 322 kwargs['fieldType'] = dictionary['type'] 323 del kwargs['type'] 324 307 325 if 'options' in dictionary: 308 326 options = [] 309 327 for value, label in dictionary['options'].iteritems(): 310 328 options.append(Option(value, label)) 311 dictionary['options'] = options 312 return Field(**dictionary) 329 kwargs['options'] = options 330 331 return Field(**kwargs) 313 332 314 333 … … 335 354 C{'FORM_TYPE'}, if set. 336 355 @type formNamespace: C{str}. 356 @ivar fields: Dictionary of fields that have a name. Note that this is 357 meant to be used for reading, only. One should use 358 L{addField} for adding fields. 359 @type fields: C{dict} 337 360 """ 338 361 … … 343 366 self.instructions = instructions or [] 344 367 self.formNamespace = formNamespace 345 self.fields = fields or [] 346 368 369 self.fieldList = [] 370 self.fields = {} 371 372 if fields: 373 for field in fields: 374 self.addField(field) 347 375 348 376 def __repr__(self): … … 360 388 if self.fields: 361 389 r.append(", fields=") 362 r.append(repr(self.field s))390 r.append(repr(self.fieldList)) 363 391 r.append(")") 364 392 return u"".join(r) 393 394 395 def addField(self, field): 396 """ 397 Add a field to this form. 398 399 Fields are added in order, and L{fields} is a dictionary of the 400 named fields, that is kept in sync only if this method is used for 401 adding new fields. Multiple fields with the same name are disallowed. 402 """ 403 if field.var is not None: 404 if field.var in self.fields: 405 raise Error("Duplicate field %r" % field.var) 406 407 self.fields[field.var] = field 408 409 self.fieldList.append(field) 365 410 366 411 … … 379 424 form.addChild(field.toElement()) 380 425 381 for field in self.field s:426 for field in self.fieldList: 382 427 form.addChild(field.toElement()) 383 428 … … 407 452 form.formNamespace = field.value 408 453 else: 409 form. fields.append(field)454 form.addField(field) 410 455 411 456 @staticmethod … … 429 474 values = {} 430 475 431 for field in self.fields:476 for name, field in self.fields.iteritems(): 432 477 if len(field.values) > 1: 433 478 value = field.values … … 435 480 value = field.value 436 481 437 if field.var: 438 values[field.var] = value 482 values[name] = value 439 483 440 484 return values
Note: See TracChangeset
for help on using the changeset viewer.