SQLAlchemy Cookbook related problem

8 views
Skip to first unread message

andrei

unread,
Feb 10, 2009, 3:28:04 PM2/10/09
to web.py
in this page http://webpy.org/cookbook/sqlalchemy

def load_sqla(handler):
web.ctx.orm = scoped_session(sessionmaker(bind=engine))
try:
return handler()
except:
web.ctx.orm.rollback()
raise
finally:
web.ctx.orm.commit()

But web.redirect is an exception, so this code will do
web.ctx.orm.rollback() on every redirect. What if i want to redirect
after i post some data to create a new entry - it won't be saved in
the database.

Brent Pedersen

unread,
Feb 10, 2009, 3:30:59 PM2/10/09
to we...@googlegroups.com
hi,
add a clause toi except for web.redirect:
like below.
though i dont know from memory the exact Exception you have to catch,
but you get the idea.
-b



On Tue, Feb 10, 2009 at 12:28 PM, andrei <Andr...@gmail.com> wrote:
>
> in this page http://webpy.org/cookbook/sqlalchemy
>
> def load_sqla(handler):
> web.ctx.orm = scoped_session(sessionmaker(bind=engine))
> try:
> return handler()
except web.Redirect
pass

andrei

unread,
Feb 10, 2009, 4:28:40 PM2/10/09
to web.py
I did the same workaround.
But still i wonder if its correct, or maybe web.Redirect needs to be
raised further like this:

def load_sqla(handler):
web.ctx.orm = scoped_session(sessionmaker(bind=engine))
try:
return handler()
except web.Redirect:
web.ctx.orm.commit()
raise
except:
web.ctx.orm.rollback()
raise
finally:
web.ctx.orm.commit()


On Feb 10, 11:30 pm, Brent Pedersen <bpede...@gmail.com> wrote:
> hi,
> add a clause toi except for web.redirect:
> like below.
> though i dont know from memory the exact Exception you have to catch,
> but you get the idea.
> -b
>
> On Tue, Feb 10, 2009 at 12:28 PM, andrei <Andre...@gmail.com> wrote:
>
> > in this pagehttp://webpy.org/cookbook/sqlalchemy

Brent Pedersen

unread,
Feb 10, 2009, 11:06:37 PM2/10/09
to we...@googlegroups.com
good call, i updated the cookbook page, and just used:

except web.HTTPError:

which is the parent class of the web.py Exceptions.
-b
Reply all
Reply to author
Forward
0 new messages