Changeset 80:3ee3e922549d


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

Move findForm to wokkel.data_form

Location:
wokkel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/data_form.py

    r79 r80  
    654654        for field in filtered:
    655655            self.removeField(field)
     656
     657
     658
     659def findForm(element, formNamespace):
     660    """
     661    Find a Data Form.
     662
     663    Look for an element that represents a Data Form with the specified
     664    form namespace as a child element of the given element.
     665    """
     666    if not element:
     667        return None
     668
     669    for child in element.elements():
     670        if (child.uri, child.name) == ((NS_X_DATA, 'x')):
     671            form = Form.fromElement(child)
     672
     673            if (form.formNamespace == formNamespace or
     674                not form.formNamespace and form.formType=='cancel'):
     675                return form
     676
     677    return None
  • wokkel/pubsub.py

    r79 r80  
    253253
    254254
    255     @staticmethod
    256     def _findForm(element, formNamespace):
    257         """
    258         Find a Data Form.
    259 
    260         Look for an element that represents a Data Form with the specified
    261         form namespace as a child element of the given element.
    262         """
    263         if not element:
    264             return None
    265 
    266         form = None
    267         for child in element.elements():
    268             try:
    269                 form = data_form.Form.fromElement(child)
    270             except data_form.Error:
    271                 continue
    272 
    273             if form.formNamespace != NS_PUBSUB_NODE_CONFIG:
    274                 continue
    275 
    276         return form
    277 
    278 
    279255    def _parse_node(self, verbElement):
    280256        """
     
    367343        Parse node type out of a request for the default node configuration.
    368344        """
    369         form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
     345        form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
    370346        if form and form.formType == 'submit':
    371347            values = form.getValues()
     
    379355        Parse options out of a request for setting the node configuration.
    380356        """
    381         form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
     357        form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
    382358        if form:
    383359            if form.formType == 'submit':
     
    438414
    439415    def _parse_options(self, verbElement):
    440         form = PubSubRequest._findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
     416        form = data_form.findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
    441417        if form:
    442418            if form.formType == 'submit':
  • wokkel/test/test_data_form.py

    r79 r80  
    11521152
    11531153
     1154
     1155class FindFormTest(unittest.TestCase):
     1156    """
     1157    Tests for L{data_form.findForm}.
     1158    """
     1159
     1160    def test_findForm(self):
     1161        element = domish.Element((None, 'test'))
     1162        theForm = data_form.Form('submit', formNamespace='myns')
     1163        element.addChild(theForm.toElement())
     1164        form = data_form.findForm(element, 'myns')
     1165        self.assertEqual('myns', form.formNamespace)
     1166
     1167
     1168    def test_noFormType(self):
     1169        element = domish.Element((None, 'test'))
     1170        otherForm = data_form.Form('submit')
     1171        element.addChild(otherForm.toElement())
     1172        form = data_form.findForm(element, 'myns')
     1173        self.assertIdentical(None, form)
     1174
     1175
     1176    def test_noFormTypeCancel(self):
     1177        """
     1178        Cancelled forms don't have a FORM_TYPE field, the first is returned.
     1179        """
     1180        element = domish.Element((None, 'test'))
     1181        cancelledForm = data_form.Form('cancel')
     1182        element.addChild(cancelledForm.toElement())
     1183        form = data_form.findForm(element, 'myns')
     1184        self.assertEqual('cancel', form.formType)
     1185
     1186
     1187    def test_otherFormType(self):
     1188        """
     1189        Forms with other FORM_TYPEs are ignored.
     1190        """
     1191        element = domish.Element((None, 'test'))
     1192        otherForm = data_form.Form('submit', formNamespace='otherns')
     1193        element.addChild(otherForm.toElement())
     1194        form = data_form.findForm(element, 'myns')
     1195        self.assertIdentical(None, form)
     1196
     1197
     1198    def test_otherFormTypeCancel(self):
     1199        """
     1200        Cancelled forms with another FORM_TYPE are ignored.
     1201        """
     1202        element = domish.Element((None, 'test'))
     1203        cancelledForm = data_form.Form('cancel', formNamespace='otherns')
     1204        element.addChild(cancelledForm.toElement())
     1205        form = data_form.findForm(element, 'myns')
     1206        self.assertIdentical(None, form)
     1207
     1208
     1209    def test_noElement(self):
     1210        """
     1211        When None is passed as element, None is returned.
     1212        """
     1213        element = None
     1214        form = data_form.findForm(element, 'myns')
     1215        self.assertIdentical(None, form)
     1216
     1217
     1218    def test_noForm(self):
     1219        """
     1220        When no child element is a form, None is returned.
     1221        """
     1222        element = domish.Element((None, 'test'))
     1223        form = data_form.findForm(element, 'myns')
     1224        self.assertIdentical(None, form)
    11541225    def test_typeCheckNoFieldDefs(self):
    11551226        """
Note: See TracChangeset for help on using the changeset viewer.