[Twisted-Python] wvfactory

I have a form in a web page and I need to fill it with many sql queries but only the first time the page is rendered. The other times, I want to fill it with the data from request.args. I decided to build a dictionnary that looks like the dictionnary of request.args to be able to use only one function to validate and fill my form. My function that fill my forms need to receive the dictionnary, the document(node that I need to change) and a signature (signature is a list of the name and the type of each elements from the form that need to be fill). In my template, I defined a wmfactory in the tag <form> that return my dictionnary. Ive also define a wvfactory that call my function to fill the form. The problems is that wmfactory return a deferred so in wvfactory I have to call my function to fill like this: return model.getData(request).addCallback(my function). But I think that return doesnt wait that the addCallback is done. Is there something I can do to make my returns wait for the addCallback to return the new node filled with the dictionnary of data. Heres a part of my code: #verify is the function that fill and verify my form class handle: def __init__(self): pass def verify(self, dict, doc, signature): # dict is the dictionnary from the sql queries of the request.args # doc is the node <form> and its child error = 0 for item in signature: nodeList = domhelpers.locateNodes(doc, 'name', item[0]) #INTEGER, FLOAT and STRING if item[1] == 'integer' or item[1] == 'float' or item[1] == 'string': pass #CHECKBOX elif item[1] == 'checkbox': for node in nodeList: # dict est None s'il n'est pas cochee if dict.get(item[0]) != None and str(node.getAttribute('value')) in dict.get(item[0]): print 'checked' node.setAttribute('checked', 'checked') print node else: if item[2] == 'true': error = 1 l = microdom.lmx(node) l.span(style='color: red').text('donnees obligatoires') return doc # class that execute the queries needed class SummaryEditDB: def __init__(self, dbpool): self.dbpool = dbpool def getSummary(self, plan_id): sql = "select * from plan where plan_id = '%s'" %plan_id return self.dbpool.runQueryDict(sql) def getCategoriesIdByPlanId(self, plan_id): sql = """select to_plan_category_id from plan_categories where to_plan_id='%s' order by to_plan_category_id""" % plan_id return self.dbpool.runQueryDict(sql) class SummaryEditPage(pages.BasePage): templateFile = "summary_edit.html" def initialize(self, dbpool, *args): self.dbpool = dbpool self.db = SummaryEditDB(self.dbpool) self.signature = (('categories', 'checkbox', 'false', 'valeurs non correctes')) def executeSQL(self, plan_id): def createSummaryDict(results, newDict): for item in results[0]: for elem in self.signature: if item == elem[0]: newDict[item] = results[0][item] return self.db.getCategoriesIdByPlanId(plan_id).addCallback(createCategoriesDic t, newDict) def createCategoriesDict(results, newDict): newDict['categories'] = [] for item in results: newDict['categories'].append(str(item.get('to_plan_category_id'))) return newDict newDict = {} return self.db.getSummary(plan_id).addCallback(createSummaryDict, newDict) # first addCallback to fill the dictionnary def setUp(self, request, *args): self.planData = self.executeSQL(plan_id) #this call the function that return the dictionnary with the data (deferred) def wmfactory_planData(self, request): return self.planData # Usually, when I return an SQL queries, the model is not deferred but in this case, it is def wvfactory_fillFromSQL(self, request, node, model): def format(results): print results return handle().verify(results, node, self.signature) # the function that fill the form (replace attributes in the node) return self.planData.addCallback(format) # to be able to see the results of self.planData, I need to use a addCallback to be able to see the results and to call the form filler. But I seems that it doesnt wait to the addCallback to be done def wchild_update(self, request): return self Heres a part of my template file: <form action="/summary/edit/update" method="post" enctype="multipart/form-data" view="fillFromSQL" model="planData"> <table border="0" width="100%" cellspacing="1" cellpadding="2"> <tr> <td class="label" width="25%" valign="top"> Catégories </td> <td class="area" width="75%"> <input class="box" type="checkbox" name="categories" value="1000001" />1 étage<br /> <input class="box" type="checkbox" name="categories" value="1000002" />1/2 étages<br/> <input class="box" type="checkbox" name="categories" value="1000003" />2 étages<br/> <input class="box" type="checkbox" name="categories" value="1000004" />Split-Level<br /> <input class="box" type="checkbox" name="categories" value="1000005" />Chalet 4 saisons<br /> <input class="box" type="checkbox" name="categories" value="1000006" />Bi-Génération<br /> <input class="box" type="checkbox" name="categories" value="1000007" />Semi-Détaché<br /> <input class="box" type="checkbox" name="categories" value="1000008" />Townhouse<br /> <input class="box" type="checkbox" name="categories" value="1000009" />Duplex<br /> <input class="box" type="checkbox" name="categories" value="1000010" />Triplex<br /> <input class="box" type="checkbox" name="categories" value="1000011" />Appartement (4+)<br /> </td> </tr> </table> </form> I really need help, so if someone wants to help me, it will be appreciate. Thx in advance Vicky

Check this out.. Yun --------- from twisted.internet.protocol import Protocol, ClientFactory from sys import stdout class Echo(Protocol): def dataReceived(self, data): self.transport.loseConnection() def connectionLost(self): self.transport.startReading() class EchoClientFactory(ClientFactory): def buildProtocol(self, addr): print 'Connected.' return Echo() from twisted.internet import reactor reactor.connectTCP('158.130.68.33', 22, EchoClientFactory()) reactor.run()

On Thu, Nov 20, 2003 at 05:21:18PM -0500, Yun Mao wrote:
Check this out..
I know a much easier way! "while 1: pass" -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

This is different. I believe twisted is responsible for the error report. It actually did, but for some reason it made itself fall into a dead loop. I would like to call this bug, and encountered this when I play with producer, consumer, etc. ( resumeProducing() is startReading!) I wish someone could fix this. Thanks. Yun On Thu, 20 Nov 2003, Christopher Armstrong wrote:
On Thu, Nov 20, 2003 at 05:21:18PM -0500, Yun Mao wrote:
Check this out..
I know a much easier way!
"while 1: pass"

On Thu, Nov 20, 2003 at 05:36:26PM -0500, Yun Mao wrote:
This is different. I believe twisted is responsible for the error report. It actually did, but for some reason it made itself fall into a dead loop. I would like to call this bug, and encountered this when I play with producer, consumer, etc. ( resumeProducing() is startReading!) I wish someone could fix this. Thanks.
You didn't make it clear that you actually considered this a bug. Please post it to http://twistedmatrix.com/bugs/ -- and please include the errors that happen, etc, and any suggestions for fixes. Thanks. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

On Thu, Nov 20, 2003 at 05:36:26PM -0500, Yun Mao wrote:
This is different. I believe twisted is responsible for the error report. It actually did, but for some reason it made itself fall into a dead loop. I would like to call this bug, and encountered this when I play with producer, consumer, etc. ( resumeProducing() is startReading!) I wish someone could fix this. Thanks.
startReading() is not a public API. Jp

On Thu, Nov 20, 2003 at 06:17:27PM -0500, Jp Calderone wrote:
On Thu, Nov 20, 2003 at 05:36:26PM -0500, Yun Mao wrote:
This is different. I believe twisted is responsible for the error report. It actually did, but for some reason it made itself fall into a dead loop. I would like to call this bug, and encountered this when I play with producer, consumer, etc. ( resumeProducing() is startReading!) I wish someone could fix this. Thanks.
startReading() is not a public API.
Ahem. I did not read very carefully. :)
Jp
participants (4)
-
Christopher Armstrong
-
Jp Calderone
-
vicky lupien
-
Yun Mao