[Twisted-Python] Woven View factories: defered rendering?

Donovan Preston dp at twistedmatrix.com
Thu Nov 20 10:50:30 EST 2003

On Nov 19, 2003, at 10:20 PM, Michal Pasternak wrote:

> Hi,
> I have a webpage, that is rendered via twisted.web.woven.page.Page 
> class.
> In one of it's wvfactory_ methods, I need to send an e-mail. I would 
> also
> like to check if sending of the e-mail was successful (eg. mailserver 
> is
> up, running, and reachable via network).
> I thought about something like this:


Woven has some, ahem, object identity issues. It really, really wants 
you to return an actual View instance from a wvfactory_. However, it 
does support deferreds being returned from the Widget.generate method. 
A deferred callback should mutate the incoming template node in place 
in order to cause some changes to the final page:

class MyDeferredWidget(widgets.Widget):
	def generate(self, request, node):
		theDeferred = doSomeAction()
		def theCallback(result):
				request.d.createTextNode("the result was: %s" % result)
		return theDeferred

class MyPage(page.Page):
	template = """<html><span view="theDeferredThing" /></html>"""

	def wvfactory_theDeferredThing(self, request, node, model):
		return MyDeferredWidget(model)

This is completely untested, but it gives you an idea of what to do. 
One of the main reasons for embarking upon nevow was that I knew a lot 
more about Deferreds than when I wrote woven, and woven's architecture 
made it very difficult to support deferreds properly. In nevow, the 
code might look like:

class MyPage(renderer.Renderer):
	def render_doSomeAction(self, context, data):
		return doSomeAction().addCallback(
			lambda result: "The result was: %s" % result)

	document = html[

The point is nevow was designed from the beginning to handle Deferreds 
more gracefully than woven was. Also, because the paradigm in nevow 
sticks far better to "the return value of a callable replaces whatever 
came in" than woven ended up doing, it's impossible to get into funny 
identity situations where you're mutating an object but you don't see 
the result of it in the final page, because you were mutating a copy 
and it was the wrong object.


More information about the Twisted-Python mailing list