source: ralphm-patches/findform.patch @ 27:a9704141033a

Last change on this file since 27:a9704141033a was 27:a9704141033a, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Ensure all modified code has unittests, add setOptions to PubSubClient?.

File size: 5.4 KB
  • wokkel/data_form.py

    Move findForm to wokkel.data_form
    
    diff -r ff3064cad1af wokkel/data_form.py
    a b  
    657657
    658658        for field in filtered:
    659659            self.removeField(field)
     660
     661
     662
     663def findForm(element, formNamespace):
     664    """
     665    Find a Data Form.
     666
     667    Look for an element that represents a Data Form with the specified
     668    form namespace as a child element of the given element.
     669    """
     670    if not element:
     671        return None
     672
     673    for child in element.elements():
     674        if (child.uri, child.name) == ((NS_X_DATA, 'x')):
     675            form = Form.fromElement(child)
     676
     677            if (form.formNamespace == formNamespace or
     678                not form.formNamespace and form.formType=='cancel'):
     679                return form
     680
     681    return None
  • wokkel/pubsub.py

    diff -r ff3064cad1af wokkel/pubsub.py
    a b  
    252252        self.verb = verb
    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        """
    281257        Parse the required node identifier out of the verbElement.
     
    366342        """
    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()
    372348            self.nodeType = values.get('pubsub#node_type', 'leaf')
     
    378354        """
    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':
    384360                self.options = form.getValues()
     
    437413
    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':
    443419                self.options = form.getValues()
  • wokkel/test/test_data_form.py

    diff -r ff3064cad1af wokkel/test/test_data_form.py
    a b  
    10641064        self.assertEqual([], checked)
    10651065
    10661066
     1067
     1068class FindFormTest(unittest.TestCase):
     1069    """
     1070    Tests for L{data_form.findForm}.
     1071    """
     1072
     1073    def test_findForm(self):
     1074        element = domish.Element((None, 'test'))
     1075        theForm = data_form.Form('submit', formNamespace='myns')
     1076        element.addChild(theForm.toElement())
     1077        form = data_form.findForm(element, 'myns')
     1078        self.assertEqual('myns', form.formNamespace)
     1079
     1080
     1081    def test_noFormType(self):
     1082        element = domish.Element((None, 'test'))
     1083        otherForm = data_form.Form('submit')
     1084        element.addChild(otherForm.toElement())
     1085        form = data_form.findForm(element, 'myns')
     1086        self.assertIdentical(None, form)
     1087
     1088
     1089    def test_noFormTypeCancel(self):
     1090        """
     1091        Cancelled forms don't have a FORM_TYPE field, the first is returned.
     1092        """
     1093        element = domish.Element((None, 'test'))
     1094        cancelledForm = data_form.Form('cancel')
     1095        element.addChild(cancelledForm.toElement())
     1096        form = data_form.findForm(element, 'myns')
     1097        self.assertEqual('cancel', form.formType)
     1098
     1099
     1100    def test_otherFormType(self):
     1101        """
     1102        Forms with other FORM_TYPEs are ignored.
     1103        """
     1104        element = domish.Element((None, 'test'))
     1105        otherForm = data_form.Form('submit', formNamespace='otherns')
     1106        element.addChild(otherForm.toElement())
     1107        form = data_form.findForm(element, 'myns')
     1108        self.assertIdentical(None, form)
     1109
     1110
     1111    def test_otherFormTypeCancel(self):
     1112        """
     1113        Cancelled forms with another FORM_TYPE are ignored.
     1114        """
     1115        element = domish.Element((None, 'test'))
     1116        cancelledForm = data_form.Form('cancel', formNamespace='otherns')
     1117        element.addChild(cancelledForm.toElement())
     1118        form = data_form.findForm(element, 'myns')
     1119        self.assertIdentical(None, form)
     1120
     1121
     1122    def test_noElement(self):
     1123        """
     1124        When None is passed as element, None is returned.
     1125        """
     1126        element = None
     1127        form = data_form.findForm(element, 'myns')
     1128        self.assertIdentical(None, form)
     1129
     1130
     1131    def test_noForm(self):
     1132        """
     1133        When no child element is a form, None is returned.
     1134        """
     1135        element = domish.Element((None, 'test'))
     1136        form = data_form.findForm(element, 'myns')
     1137        self.assertIdentical(None, form)
Note: See TracBrowser for help on using the repository browser.