reconnect.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » test » acceptance » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » test » acceptance » reconnect.py

"""
An (unfortunately somewhat contrived, and involved) acceptance test which
demonstrates that it is possible to combine a few features of Athena to
re-connect a widget to a newly-created server-side counterpart.  In order to
see the demonstration, click 'show ID', 'disconnect', then 'show ID' again.
You will see that you have received a new ReconnectableElement.

Run as::

    twistd -n athena-widget --element nevow.test.acceptance.reconnect.reconnect

"""

from nevow.athena import LiveElement,expose
from nevow import loaders,tags
from nevow._widget_plugin import ElementRenderingLivePage

class ReconnectableElementRenderingLivePage(OriginalPage):
    """
    A version of the ElementRenderingLivePage from the plugin which also has
    methods suitable for reconnection.

    XXX: this class unfortunately uncovers some nasty corners of the LivePage
    API.  Don't be too surprised if this ends up broken by some future fixes to
    that API.  We should not attempt to keep this working as-is; if you do find
    it in a broken state, just update it to use newer, better documented APIs.
    """
    jsClass = u'Nevow.Test.ReconnectAcceptanceTest.ReconnectingPage'
    def __init__(self, *a, **k):
        """
        Make sure our interface allows the particular method we want to expose.
        """
        super(ReconnectableElementRenderingLivePage, self).__init__(*a, **k)
        self.iface = ['giveElementID']
        self.rootObject = self


    def giveElementID(self, newID):
        """
        Assign an ID to the widget.
        """
        self.element.setFragmentParent(self)
        self._localObjects[newID] = self.element

    expose(giveElementID)


# Monkey patch to cheat a little bit in the context of the widget plugin.
from nevow import _widget_plugin
_widget_plugin.ElementRenderingLivePage = ReconnectableElementRenderingLivePage

import itertools

counter = itertools.count().next

class ReconnectableElement(LiveElement):
    """
    I am a live element that can disconnect and be reconnected.
    """
    docFactory = loaders.stan(
        tags.div(render=tags.directive("liveElement"))[
            tags.button(onclick="Nevow.Athena.page.deliveryChannel.sendCloseMessage(); return true;")["Disconnect"],
            tags.button(onclick="""
            Nevow.Athena.Widget.get(this).callRemote('getID').addCallback(function (result) {
                var e = document.createElement('div');
                e.appendChild(document.createTextNode('ID: '+result));
                document.body.appendChild(e);
            })
            """)["Show ID"]])


    def __init__(self):
        """
        Create a ReconnectableElement with a unique ID
        """
        LiveElement.__init__(self)
        self.currentID = counter()


    def getID(self):
        """
        Retrieve my current ID.
        """
        return self.currentID
    expose(getID)



def reconnect():
    return ReconnectableElement()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.