Ignore:
Timestamp:
Aug 4, 2008, 8:55:45 AM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@59
Message:

Implement type checking in data forms and incoming pubsub node config.

Author: ralphm.
Fixes #15.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/data_form.py

    r25 r29  
    204204
    205205
    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.
    211209        """
    212210        if self.var is None and self.fieldType != 'fixed':
    213211            raise FieldNameRequiredError()
    214 
    215         field = domish.Element((NS_X_DATA, 'field'))
    216         field['type'] = self.fieldType
    217 
    218         if self.var is not None:
    219             field['var'] = self.var
    220212
    221213        if self.values:
     
    225217                raise TooManyValuesError()
    226218
     219            newValues = []
    227220            for value in self.values:
    228221                if self.fieldType == 'boolean':
    229222                    # We send out the textual representation of boolean values
    230                     value = unicode(bool(value)).lower()
     223                    value = bool(int(value))
    231224                elif self.fieldType in ('jid-single', 'jid-multi'):
    232225                    value = value.full()
    233226
    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)
    235250
    236251        if self.fieldType in ('list-single', 'list-multi'):
     
    302317    @staticmethod
    303318    def fromDict(dictionary):
     319        kwargs = dictionary.copy()
     320
    304321        if 'type' in dictionary:
    305             dictionary['fieldType'] = dictionary['type']
    306             del dictionary['type']
     322            kwargs['fieldType'] = dictionary['type']
     323            del kwargs['type']
     324
    307325        if 'options' in dictionary:
    308326            options = []
    309327            for value, label in dictionary['options'].iteritems():
    310328                options.append(Option(value, label))
    311             dictionary['options'] = options
    312         return Field(**dictionary)
     329            kwargs['options'] = options
     330
     331        return Field(**kwargs)
    313332
    314333
     
    335354                         C{'FORM_TYPE'}, if set.
    336355    @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}
    337360    """
    338361
     
    343366        self.instructions = instructions or []
    344367        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)
    347375
    348376    def __repr__(self):
     
    360388        if self.fields:
    361389            r.append(", fields=")
    362             r.append(repr(self.fields))
     390            r.append(repr(self.fieldList))
    363391        r.append(")")
    364392        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)
    365410
    366411
     
    379424            form.addChild(field.toElement())
    380425
    381         for field in self.fields:
     426        for field in self.fieldList:
    382427            form.addChild(field.toElement())
    383428
     
    407452            form.formNamespace = field.value
    408453        else:
    409             form.fields.append(field)
     454            form.addField(field)
    410455
    411456    @staticmethod
     
    429474        values = {}
    430475
    431         for field in self.fields:
     476        for name, field in self.fields.iteritems():
    432477            if len(field.values) > 1:
    433478                value = field.values
     
    435480                value = field.value
    436481
    437             if field.var:
    438                 values[field.var] = value
     482            values[name] = value
    439483
    440484        return values
Note: See TracChangeset for help on using the changeset viewer.