[Twisted-Python] wvfactory

vicky lupien vlupien at drummonddesigns.com
Thu Nov 20 16:06:18 EST 2003


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.  I’ve 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 doesn’t 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.
 
Here’s 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 it’s 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 doesn’t wait to the
addCallback to be done
        
    def wchild_update(self, request):
        return self
        
Here’s 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">
                                                &nbsp;Cat&eacute;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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20031120/043719e1/attachment.htm 


More information about the Twisted-Python mailing list