Changeset 50:a37c65be8203


Ignore:
Timestamp:
Jan 12, 2009, 9:10:58 PM (12 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@158
Message:

Selectively render Data Forms fields depending on form type.

Author: ralphm.
Reviewer: tofu.
Fixes #40.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/data_form.py

    r48 r50  
    11# -*- test-case-name: wokkel.test.test_data_form -*-
    22#
    3 # Copyright (c) 2003-2008 Ralph Meijer
     3# Copyright (c) 2003-2009 Ralph Meijer
    44# See LICENSE for details.
    55
     
    229229            self.values = newValues
    230230
    231     def toElement(self):
     231    def toElement(self, asForm=False):
    232232        """
    233233        Return the DOM representation of this Field.
     
    239239
    240240        field = domish.Element((NS_X_DATA, 'field'))
    241         field['type'] = self.fieldType
     241
     242        if asForm or self.fieldType != 'text-single':
     243            field['type'] = self.fieldType
    242244
    243245        if self.var is not None:
     
    249251            field.addElement('value', content=value)
    250252
    251         if self.fieldType in ('list-single', 'list-multi'):
    252             for option in self.options:
    253                 field.addChild(option.toElement())
    254 
    255         if self.label is not None:
    256             field['label'] = self.label
    257 
    258         if self.desc is not None:
    259             field.addElement('desc', content=self.desc)
    260 
    261         if self.required:
    262             field.addElement('required')
     253        if asForm:
     254            if self.fieldType in ('list-single', 'list-multi'):
     255                for option in self.options:
     256                    field.addChild(option.toElement())
     257
     258            if self.label is not None:
     259                field['label'] = self.label
     260
     261            if self.desc is not None:
     262                field.addElement('desc', content=self.desc)
     263
     264            if self.required:
     265                field.addElement('required')
    263266
    264267        return field
     
    425428
    426429        for field in self.fieldList:
    427             form.addChild(field.toElement())
     430            form.addChild(field.toElement(self.formType=='form'))
    428431
    429432        return form
  • wokkel/test/test_data_form.py

    r29 r50  
    1 # Copyright (c) 2003-2008 Ralph Meijer
     1# Copyright (c) 2003-2009 Ralph Meijer
    22# See LICENSE for details.
    33
     
    3838
    3939    def test_basic(self):
     40        """
     41        Test basic field initialization.
     42        """
    4043        field = Field(var='test')
    4144        self.assertEqual('text-single', field.fieldType)
    4245        self.assertEqual('test', field.var)
    4346
     47
     48    def test_noFieldName(self):
     49        field = Field()
     50        self.assertRaises(FieldNameRequiredError, field.toElement)
     51
     52
     53    def test_toElement(self):
     54        """
     55        Test rendering to a DOM.
     56        """
     57        field = Field(var='test')
    4458        element = field.toElement()
    4559
     
    4761        self.assertEquals('field', element.name)
    4862        self.assertEquals(NS_X_DATA, element.uri)
    49         self.assertEquals('text-single', element['type'])
     63        self.assertEquals('text-single',
     64                          element.getAttribute('type', 'text-single'))
    5065        self.assertEquals('test', element['var'])
    5166        self.assertEquals([], element.children)
    5267
    5368
    54     def test_noFieldName(self):
    55         field = Field()
    56         self.assertRaises(FieldNameRequiredError, field.toElement)
     69    def test_toElementTypeNotListSingle(self):
     70        """
     71        Always render the field type, if different from list-single.
     72        """
     73        field = Field('hidden', var='test')
     74        element = field.toElement()
     75
     76        self.assertEquals('hidden', element.getAttribute('type'))
     77
     78
     79    def test_toElementAsForm(self):
     80        """
     81        Always render the field type, if asForm is True.
     82        """
     83        field = Field(var='test')
     84        element = field.toElement(True)
     85
     86        self.assertEquals('text-single', element.getAttribute('type'))
     87
     88
     89    def test_toElementOptions(self):
     90        """
     91        Test rendering to a DOM with options.
     92        """
     93        field = Field('list-single', var='test')
     94        field.options = [Option(u'option1'), Option(u'option2')]
     95        element = field.toElement(True)
     96
     97        self.assertEqual(2, len(element.children))
     98
     99
     100    def test_toElementLabel(self):
     101        """
     102        Test rendering to a DOM with a label.
     103        """
     104        field = Field(var='test', label=u'my label')
     105        element = field.toElement(True)
     106
     107        self.assertEqual(u'my label', element.getAttribute('label'))
     108
     109
     110    def test_toElementDescription(self):
     111        """
     112        Test rendering to a DOM with options.
     113        """
     114        field = Field(var='test', desc=u'My desc')
     115        element = field.toElement(True)
     116
     117        self.assertEqual(1, len(element.children))
     118        child = element.children[0]
     119        self.assertEqual('desc', child.name)
     120        self.assertEqual(NS_X_DATA, child.uri)
     121        self.assertEqual(u'My desc', unicode(child))
     122
     123
     124    def test_toElementRequired(self):
     125        """
     126        Test rendering to a DOM with options.
     127        """
     128        field = Field(var='test', required=True)
     129        element = field.toElement(True)
     130
     131        self.assertEqual(1, len(element.children))
     132        child = element.children[0]
     133        self.assertEqual('required', child.name)
     134        self.assertEqual(NS_X_DATA, child.uri)
    57135
    58136
Note: See TracChangeset for help on using the changeset viewer.