Changeset 31:76642b424906 in ralphm-patches


Ignore:
Timestamp:
Jan 5, 2010, 2:54:29 PM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Don't render a type attribute for a field if the fieldType is None.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pubsub-forms.patch

    r28 r31  
    11Move makeFields to wokkel.data_form, add form type checking.
    22
    3 diff -r ba58db23e8b9 wokkel/data_form.py
    4 --- a/wokkel/data_form.py       Fri Jan 01 15:54:26 2010 +0100
    5 +++ b/wokkel/data_form.py       Sat Jan 02 22:49:40 2010 +0100
     3diff -r c85ce41a75bc wokkel/data_form.py
     4--- a/wokkel/data_form.py       Tue Jan 05 11:46:50 2010 +0100
     5+++ b/wokkel/data_form.py       Tue Jan 05 14:53:43 2010 +0100
    66@@ -76,6 +76,7 @@
    77             option['label'] = self.label
     
    1717             if (self.fieldType not in ('hidden', 'jid-multi', 'list-multi',
    1818-                                 'text-multi') and
    19 +                                       'text-multi') and
     19+                                       'text-multi', None) and
    2020                 len(self.values) > 1):
    2121                 raise TooManyValuesError()
     
    2929         """
    3030         Return the DOM representation of this Field.
     31@@ -244,7 +246,7 @@
     32 
     33         field = domish.Element((NS_X_DATA, 'field'))
     34 
     35-        if asForm or self.fieldType != 'text-single':
     36+        if self.fieldType:
     37             field['type'] = self.fieldType
     38 
     39         if self.var is not None:
    3140@@ -322,16 +324,24 @@
    3241 
     
    6978         adding new fields. Multiple fields with the same name are disallowed.
    7079         """
    71 @@ -428,7 +438,85 @@
     80@@ -428,7 +438,72 @@
    7281         self.fieldList.append(field)
    7382 
     
    98107+        C{fieldDefs}.
    99108+
    100 +        If the field type is unknown, the field type is derived from the field
    101 +        value. For single values, if the value is a C{bool}, C{'boolean'} s
    102 +        used, for L{JID} C{'jid-single'}, and C{'text-single'} otherwise. For
    103 +        multiple values, if the first item is a L{JID} C{'jid-multi'} is used.
    104 +        Otherwise C{'list-multi'}.
    105 +
    106 +        Note that the values are used as given, no type coersion or checking
    107 +        is performed. Use L{typeCheck} to check types and coerce values of
    108 +        fields added to a form.
     109+        If the field type is unknown, the field type is C{None}. When the form
     110+        is rendered using L{toElement}, these fields will have no C{'type'}
     111+        attribute, and it is up to the receiving party to interpret the values
     112+        properly (e.g. by knowing about the FORM_TYPE in L{formNamespace} and
     113+        the field name).
    109114+
    110115+        @param values: Values to create fields from.
     
    120125+        """
    121126+        for name, value in values.iteritems():
    122 +            fieldDict = {'var': name}
     127+            fieldDict = {'var': name,
     128+                         'type': None}
    123129+
    124130+            if fieldDefs is not None:
     
    130136+            if isinstance(value, list):
    131137+                fieldDict['values'] = value
    132 +                if 'type' not in fieldDict:
    133 +                    if value and hasattr(value[0], 'full'):
    134 +                        fieldDict['type'] = 'jid-multi'
    135 +                    else:
    136 +                        fieldDict['type'] = 'list-multi'
    137138+            else:
    138139+                fieldDict['value'] = value
    139 +                if 'type' not in fieldDict:
    140 +                    if hasattr(value, 'full'):
    141 +                        fieldDict['type'] = 'jid-single'
    142 +                    elif isinstance(value, bool):
    143 +                        fieldDict['type'] = 'boolean'
    144140+
    145141+            self.addField(Field.fromDict(fieldDict))
     
    155151         form['type'] = self.formType
    156152 
    157 @@ -516,3 +604,65 @@
     153@@ -516,3 +591,65 @@
    158154             values[name] = value
    159155 
     
    221217+        for field in filtered:
    222218+            self.removeField(field)
    223 diff -r ba58db23e8b9 wokkel/pubsub.py
    224 --- a/wokkel/pubsub.py  Fri Jan 01 15:54:26 2010 +0100
    225 +++ b/wokkel/pubsub.py  Sat Jan 02 22:49:40 2010 +0100
     219diff -r c85ce41a75bc wokkel/pubsub.py
     220--- a/wokkel/pubsub.py  Tue Jan 05 11:46:50 2010 +0100
     221+++ b/wokkel/pubsub.py  Tue Jan 05 14:53:43 2010 +0100
    226222@@ -1,6 +1,6 @@
    227223 # -*- test-case-name: wokkel.test.test_pubsub -*-
     
    265261 
    266262 
    267 diff -r ba58db23e8b9 wokkel/test/test_data_form.py
    268 --- a/wokkel/test/test_data_form.py     Fri Jan 01 15:54:26 2010 +0100
    269 +++ b/wokkel/test/test_data_form.py     Sat Jan 02 22:49:40 2010 +0100
    270 @@ -787,6 +787,128 @@
     263diff -r c85ce41a75bc wokkel/test/test_data_form.py
     264--- a/wokkel/test/test_data_form.py     Tue Jan 05 11:46:50 2010 +0100
     265+++ b/wokkel/test/test_data_form.py     Tue Jan 05 14:53:43 2010 +0100
     266@@ -134,9 +134,9 @@
     267         self.assertEquals([], element.children)
     268 
     269 
     270-    def test_toElementTypeNotListSingle(self):
     271+    def test_toElementTypeNotTextSingle(self):
     272         """
     273-        Always render the field type, if different from list-single.
     274+        Always render the field type, if different from text-single.
     275         """
     276         field = data_form.Field('hidden', var='test')
     277         element = field.toElement()
     278@@ -144,6 +144,29 @@
     279         self.assertEquals('hidden', element.getAttribute('type'))
     280 
     281 
     282+    def test_toElementSingleValue(self):
     283+        """
     284+        A single value should yield only one value element.
     285+        """
     286+        field = data_form.Field('list-multi', var='test', value='test')
     287+        element = field.toElement()
     288+
     289+        children = list(element.elements())
     290+        self.assertEqual(1, len(children))
     291+
     292+
     293+    def test_toElementMultipleValues(self):
     294+        """
     295+        A field with no type and multiple values should render all values.
     296+        """
     297+        field = data_form.Field('list-multi', var='test',
     298+                                values=['test', 'test2'])
     299+        element = field.toElement()
     300+
     301+        children = list(element.elements())
     302+        self.assertEqual(2, len(children))
     303+
     304+
     305     def test_toElementAsForm(self):
     306         """
     307         Always render the field type, if asForm is True.
     308@@ -242,6 +265,27 @@
     309         self.assertEqual(u'true', unicode(element.value))
     310 
     311 
     312+    def test_toElementNoType(self):
     313+        """
     314+        A field with no type should not have a type attribute.
     315+        """
     316+        field = data_form.Field(None, var='test', value='test')
     317+        element = field.toElement()
     318+        self.assertFalse(element.hasAttribute('type'))
     319+
     320+
     321+    def test_toElementNoTypeMultipleValues(self):
     322+        """
     323+        A field with no type and multiple values should render all values.
     324+        """
     325+        field = data_form.Field(None, var='test', values=['test', 'test2'])
     326+        element = field.toElement()
     327+
     328+        self.assertFalse(element.hasAttribute('type'))
     329+        children = list(element.elements())
     330+        self.assertEqual(2, len(children))
     331+
     332+
     333     def test_typeCheckNoFieldName(self):
     334         """
     335         A field not of type fixed must have a var.
     336@@ -787,6 +831,154 @@
    271337         self.assertRaises(data_form.Error, form.addField, field2)
    272338 
     
    334400+
    335401+        # This field is not created because it does not appear in fieldDefs
     402+        # and filterUnknown defaults to True
    336403+        self.assertNotIn('x-myfield', form.fields)
    337404+
     
    347414+
    348415+
     416+    def test_makeFieldNotFilterUnknown(self):
     417+        """
     418+        Fields can be created from a dict of values and a dict of field defs.
     419+        """
     420+        fieldDefs = {
     421+                "pubsub#persist_items":
     422+                    {"type": "boolean",
     423+                     "label": "Persist items to storage"},
     424+                }
     425+        values = {'x-myfield': ['a', 'b']}
     426+
     427+        form = data_form.Form('submit')
     428+        form.makeFields(values, fieldDefs, filterUnknown=False)
     429+
     430+        field = form.fields['x-myfield']
     431+        self.assertEqual(None, field.fieldType)
     432+        self.assertEqual(values, form.getValues())
    349433+
    350434+
     
    356440+        form = data_form.Form('result')
    357441+        form.makeFields(values)
     442+
    358443+        field = form.fields['pubsub#creator']
    359 +        self.assertEqual('jid-single', field.fieldType)
     444+        self.assertEqual(None, field.fieldType)
     445+        self.assertEqual(values, form.getValues())
    360446+
    361447+
     
    368454+        form = data_form.Form('result')
    369455+        form.makeFields(values)
     456+
    370457+        field = form.fields['pubsub#contact']
    371 +        self.assertEqual('jid-multi', field.fieldType)
     458+        self.assertEqual(None, field.fieldType)
     459+        self.assertEqual(values, form.getValues())
    372460+
    373461+
     
    379467+        form = data_form.Form('result')
    380468+        form.makeFields(values)
     469+
    381470+        field = form.fields['pubsub#persist_items']
    382 +        self.assertEqual('boolean', field.fieldType)
     471+        self.assertEqual(None, field.fieldType)
     472+        self.assertEqual(values, form.getValues())
    383473+
    384474+
     
    390480+        form = data_form.Form('result')
    391481+        form.makeFields(values)
     482+
    392483+        field = form.fields['pubsub#show-values']
    393 +        self.assertEqual('list-multi', field.fieldType)
     484+        self.assertEqual(None, field.fieldType)
     485+        self.assertEqual(values, form.getValues())
    394486+
    395487+
     
    397489         """
    398490         Each named field is represented in the values, keyed by name.
    399 @@ -834,3 +956,142 @@
     491@@ -834,3 +1026,142 @@
    400492         form = data_form.Form('submit', fields=fields)
    401493         values = form.getValues()
Note: See TracChangeset for help on using the changeset viewer.