[Twisted-Python] domtemplate examples

Bruce Mitchener bruce at cubik.org
Fri Sep 13 14:25:34 EDT 2002

I got around to solving this today for doing DB integration with webMVC:

Bruce Mitchener wrote:
> Does anyone have more involved examples (even ones they can only share 
> privately for now) of using DOMTemplate than the one in doc/howto?
> Examples of form integration and DB integration like outputting lists of 
> things from a result set of a query would all be welcome.

The patch below makes the standard domwidgets.List friendly for 
Deferreds.  Similar things need to be done to the other widgets as well 
if that's the path that should be taken.

This code sucks for various reasons, but actually works:

class MIdentityList(wmvc.WModel):
     def __init__(self):
         self.dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',
                                             'bruce', 'XXXXXXXXX',
         self.dbauth = authorizer.Authorizer(self.dbpool)
         self.identityList = self.getIdentities()

     def setIdentities(self, result):
         self.identityList = result
     def getIdentities(self):
         return self.dbauth.getIdentities().addCallback(self.setIdentities)

class IdentityListWidget(domwidgets.Widget):
     def generateDOM(self, request, node):
         Generate DOM to represent a list of identities.
         if isinstance(self.model.identityList, defer.Deferred):
             return self.identityList
         return domwidgets.Widget.generateDOM(self, request, node)

class VIdentityList(wmvc.WView):
     templateFile = "identity_list.xhtml"
     def factory_identityList(self, request, node):
         return IdentityListWidget(self.model)

class CIdentityList(wmvc.WController):

wmvc.registerViewForModel(VIdentityList, MIdentityList)

Am I doing anything that is horribly wrong (apart from obvious things, 
like not grabbing a service and starting up the authorizer inside of the 
model), and that isn't likely to work beyond a simple test case like this?

I do like how the Deferreds integrate in with the DOMTemplate and webMVC 
stuff.  Pretty nice work, once I figured it out. :)

  - Bruce

Index: domwidgets.py
RCS file: /cvs/Twisted/twisted/web/domwidgets.py,v
retrieving revision 1.19
diff -u -r1.19 domwidgets.py
--- domwidgets.py       19 Aug 2002 03:22:01 -0000      1.19
+++ domwidgets.py       13 Sep 2002 18:13:59 -0000
@@ -3,6 +3,7 @@
  import urllib
  from xml.dom.minidom import parseString

+from twisted.internet import defer
  from twisted.python.mvc import View, Model
  from twisted.python import domhelpers, log

@@ -262,11 +263,14 @@
      tagName = None
      def generateDOM(self, request, node):
+        data = self.getData()
+        if isinstance(data, defer.Deferred):
+            return data.addCallback(self._cbGenerateDOM, request, node)
          node = Widget.generateDOM(self, request, node)
          # xxx with this implementation all elements of the list must 
use the same view widget
          listItem = domhelpers.get(node, 'listItem')
-        for itemNum in range(len(self.getData())):
+        for itemNum in range(len(data)):
              # theory: by appending copies of the li node
              # each node will be handled once we exit from
              # here because handleNode will then recurse into
@@ -277,6 +281,10 @@
              domhelpers.superPrependAttribute(newNode, 'model', 
self.submodel + '[' + str(itemNum) + ']')
          return node
+    def _cbGenerateDOM(self, result, request, node):
+        return self.generateDOM(request, node)

  class ColumnList(List):
      def __init__(self, model, columns=1, start=0, end=0):

More information about the Twisted-Python mailing list