MVC in webpy?

60 views
Skip to first unread message

cirfu

unread,
Jun 8, 2008, 2:50:17 PM6/8/08
to web.py
does webpy support model-view-controller design?

Aaron Swartz

unread,
Jun 8, 2008, 4:34:42 PM6/8/08
to we...@googlegroups.com
Yes.

MilesTogoe

unread,
Jun 8, 2008, 8:04:10 PM6/8/08
to we...@googlegroups.com
Aaron Swartz wrote:
> Yes.

cirfu wrote:
> does webpy support model-view-controller design?
>
well if I can disagree with the author, I'll say mostly. code.py acts
like the controller and template files (under /templates) are the
views. There is no "model" per say - I've had to create my own model.py
file since webpy doesn't provide one (although webpy allows you to add
an ORM or just a model.py). In the model.py file I add the db
connection and create a class for each table - in each class I add the
CRUD type methods

paul jobs

unread,
Jun 8, 2008, 11:34:33 PM6/8/08
to we...@googlegroups.com
how do we add orms such as sqlalchemy in webpy
in an elegant manner such as using add_processor or something like that?
and can some one please share an example of using sqlalchemy with webpy
thanks a lot

MilesTogoe

unread,
Jun 9, 2008, 12:24:51 AM6/9/08
to we...@googlegroups.com
paul jobs wrote:
> how do we add orms such as sqlalchemy in webpy
> in an elegant manner such as using add_processor or something like that?
> and can some one please share an example of using sqlalchemy with webpy
there was an article on this - google for it (I think it was in Russian
but google translates it)


paul jobs

unread,
Jun 9, 2008, 4:50:43 AM6/9/08
to we...@googlegroups.com
http://www.google.com/search?q=sqlachemy+webpy
cant find it
can u pls share
On 6/8/08, MilesTogoe <miles...@gmail.com> wrote:

blaf

unread,
Jun 9, 2008, 12:54:03 PM6/9/08
to web.py
Hi,

this code is a bit old and I'm not sure it'll work with latest webpy
or SQLAlchemy so use with caution:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(
'mysql://user:passwd@localhost/yourdb?unix_socket=/path/to/
mysqld.sock',
echo = True,
pool_recycle = 60,
pool_timeout = 30,
pool_size = 1,
max_overflow = 5
)

def loadsa():
session = scoped_session(
sessionmaker(
autoflush=True,
transactional=True,
bind=engine
)
)
web.ctx.sadbsession = session
web.ctx.sadb = session()
app.add_processor(web.loadhook(loadsa))

def unloadsa():
web.ctx.sadb.close()
web.ctx.sadbsession.remove()
app.add_processor(web.unloadhook(unloadsa))

After you'll be able to call something like:

web.ctx.sadbsession.query(YourClass).filter(YourClass.id==int(id)).all()


On Jun 9, 4:50 am, "paul jobs" <webjog...@gmail.com> wrote:
> http://www.google.com/search?q=sqlachemy+webpy
> cant find it
> can u pls share

Anand Chitipothu

unread,
Jun 9, 2008, 1:02:31 PM6/9/08
to we...@googlegroups.com
> def loadsa():
> session = scoped_session(
> sessionmaker(
> autoflush=True,
> transactional=True,
> bind=engine
> )
> )
> web.ctx.sadbsession = session
> web.ctx.sadb = session()
> app.add_processor(web.loadhook(loadsa))
>
> def unloadsa():
> web.ctx.sadb.close()
> web.ctx.sadbsession.remove()
> app.add_processor(web.unloadhook(unloadsa))

You can combine them into single processor.

def sa_processor(handler):


session = scoped_session(
sessionmaker(
autoflush=True,
transactional=True,
bind=engine
)
)
web.ctx.sadbsession = session
web.ctx.sadb = session()

try:
return handler()
finally:
web.ctx.sadb.close()
web.ctx.sadbsession.remove()

app.add_processor(sa_processor)

paul jobs

unread,
Jun 9, 2008, 5:20:10 PM6/9/08
to we...@googlegroups.com
Can you please give the complete example
in code.py
or share the code.py file
thanks

blaf

unread,
Jun 9, 2008, 7:55:54 PM6/9/08
to web.py
Shorter one ;)
Reply all
Reply to author
Forward
0 new messages