How to reuse a form in webpy

54 views
Skip to first unread message

tahnoon pasha

unread,
Dec 10, 2014, 8:19:09 AM12/10/14
to web...@googlegroups.com
Hi

I'm trying to reuse a form to de-clutter my code. The idea was to create a form class using a SQLFORM.factory with all the validation rules set up and then use it for each view in the module.

class form ():
 
'''
 USAGE:

 thisform = form(([clients],defaultc),([portfolios],defaultp),....)
 '''

 
import datetime

 
def __init__ (self, cset, pset, bset, l1set, l2set):
 form
= SQLFORM.factory (Field ("client", requires=IS_IN_SET (cset [0]), default=cset [1]),
 
Field ("portfolio", requires=IS_IN_SET (pset [0]), default=pset [1]),
 
Field ("benchmark", requires=IS_IN_SET (bset [0]), default=bset [1]),
 
Field ("startdate", "datetime", default=datetime.datetime (2013, 1, 11, 0, 0, 0)),
 
Field ("enddate", "datetime", default=datetime.datetime (2013, 3, 30, 0, 0, 0)),
 
Field ("level1", requires=IS_IN_SET (l1set [0]), default=l1set [1]),
 
Field ("level2", requires=IS_IN_SET (l2set [0])), default=l2set [1])


 
def accepted (self, form):
 startdate
= enddate = client = portfolio = benchmark = level1 = level1 = []
 
if form.process().accepted:
 client
= form.vars.client
 portfolio
= form.vars.portfolio
 benchmark
= form.vars.benchmark
 startdate
= form.vars.startdate
 enddate
= form.vars.enddate
 level1
= form.vars.level1
 level2
= form.vars.level2

 
if startdate: startdate = datetime.datetime.strftime (startdate, "%Y-%m-%d %H:%M:%S")
 
if enddate: enddate = datetime.datetime.strftime (enddate, "%Y-%m-%d %H:%M:%S")

 
return dict (client=client, portfolio=portfolio, benchmark=benchmark, startdate=startdate,
 enddate
=enddate, level1=level1, level2=level2)

that failed completely with an `AttributeError: form has no attribute process` so I tried defining a method instead with the code below:

def form():
 form
= SQLFORM.factory(Field('Client'),
 
Field('Portfolio'))
 
return form

def sandbox():
 f2
= []
 f1
= form().process()

 
if f1.process().accepted:
 f2
= "rhubarb"
 
return dict(f2=f2)

and the view `{{=f2}}` returns `[]` without ever displaying a form

Would anyone have any thoughts on how to acomplish this? Thanks..

Massimo Di Pierro

unread,
Dec 10, 2014, 11:55:05 AM12/10/14
to web...@googlegroups.com
The problem in the second code is that f1 = form().process() so f1 is a procesed form but then you call f1.process() again. Also f1 is not returned which means it is not displayed. If it is not displayed it is not processed, and if not processed it is not accepted.

This should work:

def myform():
     form = SQLFORM.factory(Field('client'), Field('portfolio'))
     return myform.process()

def sandbox():
    f1  = myform()
    if f1.accepted: 
        f2 = 'rhubarb'
    else:
        f2 = []
    return dict(f1=f1, f2=f2)

You can do it with classes too but I do not think it buys you anything since you do not need to call process from a separate method than the one that generate the form. 
Reply all
Reply to author
Forward
0 new messages