From cfef22ee653648b22dd3861844a19f5d7da0846a Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 24 Jan 2013 15:27:08 +0100 Subject: [PATCH] Fixed (really) the session management. --- src/api/controller/accounts.py | 71 +++++++++++----------- src/api/controller/entries.py | 104 +++++++++++++++++---------------- src/app.py | 1 - 3 files changed, 92 insertions(+), 84 deletions(-) diff --git a/src/api/controller/accounts.py b/src/api/controller/accounts.py index a095b5e..b59d1f4 100644 --- a/src/api/controller/accounts.py +++ b/src/api/controller/accounts.py @@ -1,7 +1,6 @@ from app import app from app import db -from app import session from api.model.accounts import Account from api.model.entries import Entry @@ -15,8 +14,9 @@ def get_accounts(): """ Returns accounts with their solds. """ + session = db.create_scoped_session() - query=session.query( + query = session.query( Account.id.label("id"), Account.name.label("name"), func.sum(Entry.value).label("future"), @@ -34,7 +34,9 @@ def get_accounts(): @app.route("/api/accounts//months") def get_months(account_id): - query=session.query( + session = db.create_scoped_session() + + query = session.query( distinct(extract("year", Entry.value_date)).label("year"), extract("month", Entry.value_date).label("month") ).filter(Entry.account_id == account_id).order_by("year", "month") @@ -46,46 +48,49 @@ def get_months(account_id): @app.route("/api/accounts", methods=["PUT"]) def add_account(): - with session.begin(): - try: - account = Account(request.json['name']) + session = db.create_scoped_session() - session.add(account) - session.commit() + try: + account = Account(request.json['name']) - return json.dumps("Account added.") - except: - session.rollback() - raise + session.add(account) + session.commit() + + return json.dumps("Account added.") + except: + session.rollback() + raise @app.route("/api/accounts/", methods=["PUT"]) def update_account(account_id): - with session.begin(): - try: - account = session.query(Account).filter(Account.id == account_id).first() + session = db.create_scoped_session() - account.name = request.json['name'] + try: + account = session.query(Account).filter(Account.id == account_id).first() - session.merge(account) - session.commit() + account.name = request.json['name'] - return json.dumps("Account #%s updated." % account_id) - except: - session.rollback() - raise + session.merge(account) + session.commit() + + return json.dumps("Account #%s updated." % account_id) + except: + session.rollback() + raise @app.route("/api/accounts/", methods=["DELETE"]) def delete_account(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 + session = db.create_scoped_session() + + 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 21ee4eb..5bda88f 100644 --- a/src/api/controller/entries.py +++ b/src/api/controller/entries.py @@ -1,6 +1,5 @@ from app import app from app import db -from app import session from api.model.entries import Entry @@ -18,7 +17,9 @@ def get_entries(account_id, year, month): """ Return entries for an account, year, and month. """ - query=session.query( + session = db.create_scoped_session() + + query = session.query( Entry ).select_from( session.query(Entry) @@ -45,58 +46,61 @@ def get_entries(account_id, year, month): @app.route("/api/entries", methods=["PUT"]) def add_entry(): - 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 + session = db.create_scoped_session() + + 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): - 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 + session = db.create_scoped_session() + + 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): - 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 + session = db.create_scoped_session() + + 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 diff --git a/src/app.py b/src/app.py index 9290396..74eecd5 100644 --- a/src/app.py +++ b/src/app.py @@ -9,5 +9,4 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://accountant:accountant@loca app.config['SQLALCHEMY_RECORD_QUERIES'] = True db = SQLAlchemy(app) -session = db.create_scoped_session()