Hi J,
Of course you were here too :)
I solved the issue by using SQLFORM.factory to generate the form for those
pages where there was a dictionary variable. I've edited the code so that I
think I got it working properly.
I save the form.vars of each page to session.page_nro and at the end (four
phases) I add them all to a one dictionary to add it to database:
db.campaign.insert(**db_data).
It seems to work okay.
On Thursday, June 13, 2013 11:55:08 AM UTC+3, Ykä Marjanen wrote:
>
> Hi,
>
> I've been programming Python for 10 years now, mainly algorithms. I found
> out about Web2py 4 months ago, and have been intensively learning it since.
> So thanks for all contributors!
>
> I'm developing a web interface where a user can create a new idea
> campaign. Because the campaign has lots of parameters which can change per
> campaign, I've decided to store most of the settings to the database as a
> dictionary using Pickle. I want to cut the creation of the campaign into
> phases to keep the visible form short.
>
> Based on the documentation (excellent book) I've decided to use Session to
> store the value from each phase and only store the variables into the
> database after the user has filled all forms. I use the same page to show
> different forms to keep the files to a minimum.
>
> My problems are the following:
>
> 1) How to use SQLFORM.dictform with a dictionary so that it shows menu
> options (and checklist) to some of the settings (e.g.
> IS_IN_SET(['one','two'])). The dictionary is created during the process, so
> the database doesn't have no values in it to fetch.
>
> 2) Am I doing to this properly (see code below)?
>
> Here's my code (I've simplified it to make it more understandable)
>
> Model:
>
> db.define_table('campaign',
> Field('name', unique=True),
> Field('pagename', unique=True),
> Field('description','text'),
> Field('created_by','reference auth_user', default=auth.user_id),
> Field('type', requires=IS_IN_SET(['open','private'])),
> Field('general_settings','text', filter_in=(lambda x:
> pickle.dumps(x)), filter_out=(lambda s: s and
> pickle.loads(s)),default=None))
>
> Controller:
>
> if request.args(0,cast=str) == 'prev':
> try:
> if session.step > 0:
> session.step -= 1
> else:
> redirect(URL('index'))
> except:
> session.step = 0
>
> #determine in which step
> if not session.step:
> session.step = 0
>
> if session.step == 0:
> form = SQLFORM(db.campaign, fields=['name','description'])
> form.add_button('Cancel',URL('index'))
> if form.validate():
> session.step1_variables = form.vars
> session.step = 1
>
> redirect(URL('new_campaign'))
>
> elif session.step == 1:
> form = SQLFORM(db.campaign, fields=['pagename'])
> form.add_button('Prev',URL('new_campaign',args='prev'))
> if form.validate():
> session.step2_variables = form.vars
> session.step = 2
>
> redirect(URL('new_campaign'))
>
> elif session.step == 2:
> settings_dict = dict(privacy="", registration="self")
> form = SQLFORM.dictform(settings_dict)
> form.add_button('Prev',URL('new_campaign',args='prev'))
> form.custom.widget.privacy['requires'] =
> IS_IN_SET(['open','closed'])
> if form.validate():
> session.step3_variables = form.vars
> session.step = 3
>
> redirect(URL('new_campaign'))
>
> elif session.step == 3:
> form = FORM.confirm('Are you sure?')
> if form.accepted:
> session.step = 0
> #Store all variables to the database
> #Redirect to the new campaign page
>
> else:
> redirect(URL('index'))
>
> return dict(form=form, step=session.step)
>
> View:
>
> {extend 'layout.html'}}
> {{if step == 0:}}
> <h2>{{=T('Create new campaign')}}</h2>
> {{=T('Welcome to XXX. Create a campaign using this simple wizard')}}
> {{elif step == 1:}}
> <h2>{{=T('Create new campaign')}}</h2>
> {{=T('Please add your pagename')}}
> {{elif step == 2:}}
> <h2>{{=T('Create new campaign')}}</h2>
> {{=T('Make some settings')}}
> {{elif step == 3:}}
> <h2>{{=T('Create new campaign')}}</h2>
> {{=T('Here are all your settings. Click the button the accept them')}}
> {{pass}}
> {{=form}}
>
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.