-
-
Save yashh/14d12595f3dfa307a354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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