form.append repetition

40 views
Skip to first unread message

annet

unread,
Jun 23, 2009, 1:29:22 PM6/23/09
to web2py Web Framework
In my cms application I have a number of functions which all have the
same structure, i.e:

form=crud.create(db.openingstijd)
form[0][-1][1].append(INPUT(_type='reset',_value='Reset'))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick="window.location='%s';"%URL
(r=request,f='index')))


I wonder whether there is a way to keep me from repeating this part of
the function:

form[0][-1][1].append(INPUT(_type='reset',_value='Reset'))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick="window.location='%s';"%URL
(r=request,f='index')))


Kind regards,

Annet.

mdipierro

unread,
Jun 23, 2009, 1:43:32 PM6/23/09
to web2py Web Framework
def myform(table):
form=crud.create(table)
form[0][-1][1].append(INPUT(_type='reset',_value='Reset'))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick="window.location='%s';"%URL
(r=request,f='index')))
return form

form=myform(db.openingstijd)

annet

unread,
Jun 23, 2009, 2:20:54 PM6/23/09
to web2py Web Framework
Massimo,

Perfect.

I also had a look at my update functions

form=crud.update(db.adres,request.args[0],next=(URL
(r=request,f='crud_address')))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick='javascript:history.go
(-1)'))

I tried:

def update_form(table,nxt)
form=crud.update(table,request.args[0],next=(URL
(r=request,f='nxt')))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick='javascript:history.go(-1)'))
return form

But it doesn't work, do you know why not?


Kind regards,

Annet.

mdipierro

unread,
Jun 23, 2009, 2:56:50 PM6/23/09
to web2py Web Framework
def update_form(table,nxt)
form=crud.update(table,request.args[0],next=(URL
(r=request,f=nxt)))
form[0][-1][1].append(INPUT
(_type='button',_value='Cancel',_onclick='javascript:history.go(-1)'))
return form

annet

unread,
Jun 24, 2009, 3:16:22 AM6/24/09
to web2py Web Framework
Massimo,

Thanks. 'do not repeat yourself' has become quite a challenge.

In one of my applications I have a controller containing nine
functions. Instead of creating an application based on a scaffold
application I thought of creating an application based on that
controller, the models and views for all applications will be the
same, they will differ from each other in CSS.

In the controller I set:

response.title
response.author
response.keywords
response.description

response.navigation=[]


company_id=1
session.companyname

if session.company_id!=company_id:
company=db(db.bedrijf.id==company_id).select
(db.bedrijf.bedrijfsnaam)
session.companyname=company[0].bedrijfsnaam


From this point onward the controller reads the same for all
applications. I gave putting them in a module some thought, I guess I
need to put them in a Class and instantiate that class in each
controller and call the functions on that object. However, my Python
knowledge isn't sufficient to get this syntactically and semantically
correct.

This is what I came up with sofar:

Every single function returns a number of SQLRows, e.g. the index
function reads like:

def index():
openinghours=db((db.openingstijd.bedrijf==company_id)&(...)).select
()
timetable=db((db.lesrooster.bedrijf==company_id)&(...)).select
(...)
frontpage=db(db.frontpage.bedrijf==company_id).select(...)
events=db((db.event.bedrijf==company_id)&(...)).select(...)
news=db((db.nieuws.bedrijf==company_id)&(...)).select(...))
leftmodule=module(15)
rightmodule=module(16)
return dict
(openinghours=openinghours,timetable=timetable,frontpage=frontpage,events=events,news=news,
\
leftmodule=leftmodule,rightmodule=rightmodule)


... in the module it would read like:

def index(company_id):
openinghours=db((db.openingstijd.bedrijf==company_id)&(...)).select
()
timetable=db((db.lesrooster.bedrijf==company_id)&(...)).select
(...)
frontpage=db(db.frontpage.bedrijf==company_id).select(...)
events=db((db.event.bedrijf==company_id)&(...)).select(...)
news=db((db.nieuws.bedrijf==company_id)&(...)).select(...))
leftmodule=module(15)
rightmodule=module(16)
return dict
(openinghours=openinghours,timetable=timetable,frontpage=frontpage,events=events,news=news,
\
leftmodule=leftmodule,rightmodule=rightmodule)


Import the module in the controllers:

from applications.demo.modules.site import*


In the controllers call the function with the company_id as an
argument:

def index():
site.index(company_id)
return dict()


I took a look at the auth class in the tools.py file and the way you
instantiate it in db.py, but that didn't get me any further ...


I hope you can provide me with a skeleton class that I can use to
implement the module ...


Kind regards,

Annet.
Reply all
Reply to author
Forward
0 new messages