Skip to content

Instantly share code, notes, and snippets.

@yashh
Created July 19, 2010 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashh/14d12595f3dfa307a354 to your computer and use it in GitHub Desktop.
Save yashh/14d12595f3dfa307a354 to your computer and use it in GitHub Desktop.
# A django like commit on success decorator for flask-sqlalchmey. Need to think if its right to spit the db
# object from the func function object ?
# sample usage:
# @login_required
# @app.route('/<category>/<article>/', methods=['GET'])
# @commit_on_success
def commit_on_success(func):
def _commit_on_success(*args, **kw):
db = func.func_globals['db']
try:
res = func(*args, **kw)
except:
if db.session.dirty:
db.session.rollback()
raise
else:
if db.session.dirty:
try:
db.session.commit()
except:
db.session.rollback()
raise
return res
return wraps(func)(_commit_on_success)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment