source: ralphm-patches/pubsub_request_options.patch @ 54:03ec57713c90

Last change on this file since 54:03ec57713c90 was 54:03ec57713c90, checked in by Ralph Meijer <ralphm@…>, 8 years ago

Upstreamed Request patches, split out c2s patches in managable chunks, prepare for release of Wokkel 0.7.0.

File size: 11.2 KB
  • wokkel/pubsub.py

    # HG changeset patch
    # Parent 7de2ae0a6a884cf594ba232ee5d909c6d10e1906
    Address incompatible change concerning PubSubRequest.options.
    
    In changeset [1334124db2fd], `PubSubRequest.options` was changed to hold the
    `wokkel.data_form.Form` that represents the data form sent in the original
    request. This makes it easier to type check the form in a step separate from
    parsing the request. However, this is an incompatible change.
    
    To remedy this, we move the form to `optionsForm` and make options a property
    that returns the values of the form.
    
    diff -r 7de2ae0a6a88 wokkel/pubsub.py
    a b  
    210210    @ivar nodeType: The type of node that should be created, or for which the
    211211                    configuration is retrieved. C{'leaf'} or C{'collection'}.
    212212    @type nodeType: C{str}
    213     @ivar options: Configurations options for nodes, subscriptions and publish
    214                    requests.
    215     @type options: L{data_form.Form}
     213    @ivar optionsForm: Configurations options for nodes, subscriptions and
     214                       publish requests.
     215    @type optionsForm: L{data_form.Form}
     216    @ivar options: Read-only property reflecting the values of the data form in
     217                   C{optionsForm}. When C{optionsForm} is set, this is equal to
     218                   the result of C{optionsForm.getValues()}.
     219    @type options: C{dict}
    216220    @ivar subscriber: The subscribing entity.
    217221    @type subscriber: L{JID}
    218222    @ivar subscriptionIdentifier: Identifier for a specific subscription.
     
    233237    maxItems = None
    234238    nodeIdentifier = None
    235239    nodeType = None
    236     options = None
     240    optionsForm = None
    237241    subscriber = None
    238242    subscriptionIdentifier = None
    239243    subscriptions = None
     
    292296        self.verb = verb
    293297
    294298
     299    def _getOptions(self):
     300        if (self.optionsForm is not None and
     301            self.optionsForm.formType != 'cancel'):
     302            return self.optionsForm.getValues()
     303        else:
     304            return None
     305
     306    options = property(_getOptions)
     307
    295308    def _parse_node(self, verbElement):
    296309        """
    297310        Parse the required node identifier out of the verbElement.
     
    398411        form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
    399412        if form:
    400413            if form.formType in ('submit', 'cancel'):
    401                 self.options = form
     414                self.optionsForm = form
    402415            else:
    403416                raise BadRequest(text=u"Unexpected form type '%s'" % form.formType)
    404417        else:
     
    419432                else:
    420433                    form = data_form.Form('submit',
    421434                                          formNamespace=NS_PUBSUB_NODE_CONFIG)
    422                 self.options = form
     435                self.optionsForm = form
    423436
    424437
    425438    def _render_configureOrNone(self, verbElement):
    426439        """
    427440        Render optional node configuration form in create request.
    428441        """
    429         if self.options is not None:
     442        if self.optionsForm is not None:
    430443            configure = verbElement.parent.addElement('configure')
    431             configure.addChild(self.options.toElement())
     444            configure.addChild(self.optionsForm.toElement())
    432445
    433446
    434447    def _parse_itemIdentifiers(self, verbElement):
     
    498511        form = data_form.findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
    499512        if form:
    500513            if form.formType in ('submit', 'cancel'):
    501                 self.options = form
     514                self.optionsForm = form
    502515            else:
    503516                raise BadRequest(text=u"Unexpected form type '%s'" % form.formType)
    504517        else:
     
    507520
    508521
    509522    def _render_options(self, verbElement):
    510         verbElement.addChild(self.options.toElement())
     523        verbElement.addChild(self.optionsForm.toElement())
    511524
    512525
    513526    def _parse_optionsWithSubscribe(self, verbElement):
     
    522535                else:
    523536                    form = data_form.Form('submit',
    524537                                          formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS)
    525                 self.options = form
     538                self.optionsForm = form
    526539
    527540
    528541    def _render_optionsWithSubscribe(self, verbElement):
    529         if self.options:
     542        if self.optionsForm is not None:
    530543            optionsElement = verbElement.parent.addElement('options')
    531544            self._render_options(optionsElement)
    532545
     
    772785            form = data_form.Form(formType='submit',
    773786                                  formNamespace=NS_PUBSUB_NODE_CONFIG)
    774787            form.makeFields(options)
    775             request.options = form
     788            request.optionsForm = form
    776789
    777790        def cb(iq):
    778791            try:
     
    835848            form = data_form.Form(formType='submit',
    836849                                  formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS)
    837850            form.makeFields(options)
    838             request.options = form
     851            request.optionsForm = form
    839852
    840853        def cb(iq):
    841854            subscription = Subscription.fromElement(iq.pubsub.subscription)
     
    10061019        form = data_form.Form(formType='submit',
    10071020                              formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS)
    10081021        form.makeFields(options)
    1009         request.options = form
     1022        request.optionsForm = form
    10101023
    10111024        d = request.send(self.xmlstream)
    10121025        return d
     
    11941207                return defer.fail(Unsupported('', text))
    11951208
    11961209            handler = getattr(self, handlerName)
    1197 
    11981210            args = [getattr(request, arg) for arg in argNames]
    1199             if 'options' in argNames:
    1200                 args[argNames.index('options')] = request.options.getValues()
    1201 
    12021211            d = handler(*args)
    12031212
    12041213        # If needed, translate the result into a response
     
    12621271
    12631272
    12641273    def _preProcess_create(self, resource, request):
    1265         if request.options:
    1266             self._checkConfiguration(resource, request.options)
     1274        if request.optionsForm:
     1275            self._checkConfiguration(resource, request.optionsForm)
    12671276        return request
    12681277
    12691278
     
    12951304
    12961305
    12971306    def _preProcess_configureSet(self, resource, request):
    1298         if request.options.formType == 'cancel':
     1307        if request.optionsForm.formType == 'cancel':
    12991308            return None
    13001309        else:
    1301             self._checkConfiguration(resource, request.options)
     1310            self._checkConfiguration(resource, request.optionsForm)
    13021311            return request
    13031312
    13041313
  • wokkel/test/test_pubsub.py

    diff -r 7de2ae0a6a88 wokkel/test/test_pubsub.py
    a b  
    11861186
    11871187        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    11881188        self.assertEqual('subscribe', request.verb)
    1189         request.options.typeCheck({'pubsub#deliver': {'type': 'boolean'}})
    1190         self.assertEqual({'pubsub#deliver': True}, request.options.getValues())
     1189        request.optionsForm.typeCheck({'pubsub#deliver': {'type': 'boolean'}})
     1190        self.assertEqual({'pubsub#deliver': True}, request.options)
    11911191
    11921192
    11931193    def test_fromElementSubscribeWithOptionsBadFormType(self):
     
    12401240
    12411241        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    12421242        self.assertEqual('subscribe', request.verb)
    1243         self.assertEqual({}, request.options.getValues())
     1243        self.assertEqual({}, request.options)
    12441244
    12451245
    12461246    def test_fromElementUnsubscribe(self):
     
    13721372        self.assertEqual(JID('pubsub.example.org'), request.recipient)
    13731373        self.assertEqual('test', request.nodeIdentifier)
    13741374        self.assertEqual(JID('user@example.org/Home'), request.subscriber)
    1375         self.assertEqual({'pubsub#deliver': '1'}, request.options.getValues())
     1375        self.assertEqual({'pubsub#deliver': '1'}, request.options)
    13761376
    13771377
    13781378    def test_fromElementOptionsSetWithSubscriptionIdentifier(self):
     
    14181418        """
    14191419
    14201420        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    1421         self.assertEqual('cancel', request.options.formType)
     1421        self.assertEqual('cancel', request.optionsForm.formType)
     1422        self.assertIdentical(None, request.options)
    14221423
    14231424
    14241425    def test_fromElementOptionsSetBadFormType(self):
     
    15291530        self.assertEqual(JID('user@example.org'), request.sender)
    15301531        self.assertEqual(JID('pubsub.example.org'), request.recipient)
    15311532        self.assertEqual('mynode', request.nodeIdentifier)
     1533        self.assertIdentical(None, request.optionsForm)
    15321534        self.assertIdentical(None, request.options)
    15331535
    15341536
     
    15661568        """
    15671569
    15681570        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    1569         self.assertEqual({}, request.options.getValues())
     1571        self.assertEqual({}, request.options)
    15701572        self.assertEqual(u'mynode', request.nodeIdentifier)
    15711573
    15721574
     
    15891591        """
    15901592
    15911593        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    1592         self.assertEqual({}, request.options.getValues())
     1594        self.assertEqual({}, request.options)
    15931595        self.assertEqual(u'mynode', request.nodeIdentifier)
    15941596
    15951597
     
    16171619        """
    16181620
    16191621        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    1620         values = request.options.getValues()
     1622        values = request.options
    16211623        self.assertIn('pubsub#access_model', values)
    16221624        self.assertEqual(u'open', values['pubsub#access_model'])
    16231625        self.assertIn('pubsub#persist_items', values)
     
    17571759        self.assertEqual('test', request.nodeIdentifier)
    17581760        self.assertEqual({'pubsub#deliver_payloads': '0',
    17591761                          'pubsub#persist_items': '1'},
    1760                          request.options.getValues())
     1762                         request.options)
    17611763
    17621764
    17631765    def test_fromElementConfigureSetCancel(self):
     
    17771779        """
    17781780
    17791781        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    1780         self.assertEqual('cancel', request.options.formType)
     1782        self.assertEqual('cancel', request.optionsForm.formType)
     1783        self.assertIdentical(None, request.options)
    17811784
    17821785
    17831786    def test_fromElementConfigureSetBadFormType(self):
     
    26332636        def create(request):
    26342637            self.assertEqual({'pubsub#deliver_payloads': False,
    26352638                              'pubsub#persist_items': True},
    2636                              request.options.getValues())
     2639                             request.options)
    26372640            return defer.succeed(None)
    26382641
    26392642        self.resource.getConfigurationOptions = getConfigurationOptions
     
    28412844        def configureSet(request):
    28422845            self.assertEqual({'pubsub#deliver_payloads': False,
    28432846                              'pubsub#persist_items': True},
    2844                              request.options.getValues())
     2847                             request.options)
    28452848            return defer.succeed(None)
    28462849
    28472850        self.resource.getConfigurationOptions = getConfigurationOptions
     
    29132916
    29142917        def configureSet(request):
    29152918            self.assertEquals(['pubsub#deliver_payloads'],
    2916                               request.options.fields.keys())
     2919                              request.options.keys())
    29172920
    29182921        self.resource.getConfigurationOptions = getConfigurationOptions
    29192922        self.resource.configureSet = configureSet
Note: See TracBrowser for help on using the repository browser.