source: ralphm-patches/pubsub-item.patch @ 45:51ac2274f989

Last change on this file since 45:51ac2274f989 was 45:51ac2274f989, checked in by Ralph Meijer <ralphm@…>, 10 years ago

Work on using a real Item class and managing affiliations support.

File size: 4.2 KB
  • wokkel/pubsub.py

    diff -r e58b9f2cf8a3 wokkel/pubsub.py
    a b  
    119119
    120120
    121121
    122 class Item(domish.Element):
     122class Item(object):
    123123    """
    124124    Publish subscribe item.
    125125
    126     This behaves like an object providing L{domish.IElement}.
    127 
    128     Item payload can be added using C{addChild} or C{addRawXml}, or using the
    129     C{payload} keyword argument to C{__init__}.
     126    @ivar itemIdentifier: optional item identifier
     127    @type itemIdentifier: L{unicode}
     128    @ivar payload: optional item payload. Either as a domish element, or as
     129        serialized XML.
     130    @type payload: object providing L{domish.IElement} or L{unicode}.
    130131    """
    131132
    132     def __init__(self, id=None, payload=None):
     133    def __init__(self, itemIdentifier=None, payload=None):
     134        self.itemIdentifier = itemIdentifier
     135        self.payload = payload
     136
     137
     138    def toElement(self, namespace=NS_PUBSUB):
    133139        """
    134         @param id: optional item identifier
    135         @type id: L{unicode}
    136         @param payload: optional item payload. Either as a domish element, or
    137                         as serialized XML.
    138         @type payload: object providing L{domish.IElement} or L{unicode}.
     140        Render to a DOM representation.
     141
     142        Usually called by when rendering requests or responses. The optional
     143        C{namespace} argument is for rendering the item in that namespace, so
     144        L{Item} is usable in all contexts involving items.
     145
     146        @param namespace: The namespace of the item. Usually L{NS_PUBSUB} or
     147            L{NS_PUBSUB_EVENT}.
     148        @type: C{unicode}
     149        @rtype: L{domish.Element}
    139150        """
     151        element = domish.Element((namespace, 'item'))
    140152
    141         domish.Element.__init__(self, (None, 'item'))
    142         if id is not None:
    143             self['id'] = id
    144         if payload is not None:
    145             if isinstance(payload, basestring):
    146                 self.addRawXml(payload)
     153        if self.itemIdentifier is not None:
     154            element['id'] = self.itemIdentifier
     155
     156        if self.payload is not None:
     157            if isinstance(self.payload, basestring):
     158                self.addRawXml(self.payload)
    147159            else:
    148                 self.addChild(payload)
     160                self.addChild(self.payload)
     161
     162        return element
    149163
    150164
    151165
     
    309323        self.items = []
    310324        for element in verbElement.elements():
    311325            if element.uri == NS_PUBSUB and element.name == 'item':
    312                 self.items.append(element)
     326                item = Item()
     327                item.itemIdentifier = element.getAttribute('id')
     328                try:
     329                    item.payload = element.elements().next()
     330                except StopIteration:
     331                    pass
     332                self.items.append(item)
    313333
    314334
    315335    def _render_items(self, verbElement):
     
    318338        """
    319339        if self.items:
    320340            for item in self.items:
    321                 verbElement.addChild(item)
     341                verbElement.addChild(item.toElement())
    322342
    323343
    324344    def _parse_jid(self, verbElement):
     
    11241144        items["node"] = request.nodeIdentifier
    11251145
    11261146        for item in result:
    1127             items.addChild(item)
     1147            items.addChild(item.toElement())
    11281148
    11291149        return response
    11301150
  • wokkel/test/test_pubsub.py

    diff -r e58b9f2cf8a3 wokkel/test/test_pubsub.py
    a b  
    365365        items = list(domish.generateElementsQNamed(child.children,
    366366                                                   'item', NS_PUBSUB))
    367367        self.assertEquals(1, len(items))
    368         self.assertIdentical(item, items[0])
    369368
    370369        response = toResponse(iq, 'result')
    371370        self.stub.send(response)
     
    699698
    700699        request = pubsub.PubSubRequest.fromElement(parseXml(xml))
    701700        self.assertEqual(2, len(request.items))
    702         self.assertEqual(u'item1', request.items[0]["id"])
    703         self.assertEqual(u'item2', request.items[1]["id"])
     701        self.assertEqual(u'item1', request.items[0].itemIdentifier)
     702        self.assertEqual(u'item2', request.items[1].itemIdentifier)
    704703
    705704
    706705    def test_fromElementPublishNoNode(self):
Note: See TracBrowser for help on using the repository browser.