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.