From f56e06223f863fd411b1ce42e8acb3be3778dfff Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 24 Jan 2013 14:58:28 +0100 Subject: [PATCH] Added transaction management in controllers. --- src/api/controller/accounts.py | 46 ++++++++++++------- src/api/controller/entries.py | 82 ++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/api/controller/accounts.py b/src/api/controller/accounts.py index 8c98d22..a095b5e 100644 --- a/src/api/controller/accounts.py +++ b/src/api/controller/accounts.py @@ -46,30 +46,46 @@ def get_months(account_id): @app.route("/api/accounts", methods=["PUT"]) def add_account(): - account = Account(request.json['name']) + with session.begin(): + try: + account = Account(request.json['name']) - session.add(account) - session.commit() + session.add(account) + session.commit() + + return json.dumps("Account added.") + except: + session.rollback() + raise - return json.dumps("Account added.") @app.route("/api/accounts/", methods=["PUT"]) def update_account(account_id): - account = session.query(Account).filter(Account.id == account_id).first() + with session.begin(): + try: + account = session.query(Account).filter(Account.id == account_id).first() - account.name = request.json['name'] + account.name = request.json['name'] - session.merge(account) - session.commit() + session.merge(account) + session.commit() - return json.dumps("Account #%s updated." % account_id) + return json.dumps("Account #%s updated." % account_id) + except: + session.rollback() + raise @app.route("/api/accounts/", methods=["DELETE"]) def delete_account(account_id): - account = session.query(Account).filter(Account.id == account_id).first() - - session.delete(account) - session.commit() - - return json.dumps("Account #%s deleted." % account_id) + with session.begin(): + try: + account = session.query(Account).filter(Account.id == account_id).first() + + session.delete(account) + session.commit() + + return json.dumps("Account #%s deleted." % account_id) + except: + session.rollback() + raise diff --git a/src/api/controller/entries.py b/src/api/controller/entries.py index 262fe1d..21ee4eb 100644 --- a/src/api/controller/entries.py +++ b/src/api/controller/entries.py @@ -45,44 +45,58 @@ def get_entries(account_id, year, month): @app.route("/api/entries", methods=["PUT"]) def add_entry(): - entry = Entry( - value_date = request.json['value_date'], - operation_date = request.json['operation_date'], - label = request.json['label'], - value = request.json['value'], - category = request.json['category'], - account_id = request.json['account_id'] - ) - - session.add(entry) - session.commit() - - return json.dumps("Entry added.") + with session.begin(): + try: + entry = Entry( + value_date = request.json['value_date'], + operation_date = request.json['operation_date'], + label = request.json['label'], + value = request.json['value'], + category = request.json['category'], + account_id = request.json['account_id'] + ) + + session.add(entry) + session.commit() + + return json.dumps("Entry added.") + except: + session.rollback() + raise @app.route("/api/entries/", methods=["PUT"]) def update_entry(entry_id): - entry = session.query(Entry).filter(Entry.id == entry_id).first() - - entry.id = entry_id - entry.value_date = request.json['value_date'] - entry.operation_date = request.json['operation_date'] - entry.label = request.json['label'] - entry.value = request.json['value'] - entry.category = request.json['category'] - entry.account_id = request.json['account_id'] - - session.merge(entry) - session.commit() - - return json.dumps("Entry #%s updated." % entry_id) + with session.begin(): + try: + entry = session.query(Entry).filter(Entry.id == entry_id).first() + + entry.id = entry_id + entry.value_date = request.json['value_date'] + entry.operation_date = request.json['operation_date'] + entry.label = request.json['label'] + entry.value = request.json['value'] + entry.category = request.json['category'] + entry.account_id = request.json['account_id'] + + session.merge(entry) + session.commit() + + return json.dumps("Entry #%s updated." % entry_id) + except: + session.rollback() + raise @app.route("/api/entries/", methods=["DELETE"]) def delete_entry(entry_id): - entry = session.query(Entry).filter(Entry.id == entry_id).first() - - session.delete(entry) - session.commit() - - return json.dumps("Entry #%s deleted." % entry_id) - + with session.begin(): + try: + entry = session.query(Entry).filter(Entry.id == entry_id).first() + + session.delete(entry) + session.commit() + + return json.dumps("Entry #%s deleted." % entry_id) + except: + session.rollback() + raise