Fixed (really) the session management.

This commit is contained in:
Alexis Lahouze 2013-01-24 15:27:08 +01:00
parent f56e06223f
commit cfef22ee65
3 changed files with 92 additions and 84 deletions

View File

@ -1,7 +1,6 @@
from app import app from app import app
from app import db from app import db
from app import session
from api.model.accounts import Account from api.model.accounts import Account
from api.model.entries import Entry from api.model.entries import Entry
@ -15,8 +14,9 @@ def get_accounts():
""" """
Returns accounts with their solds. Returns accounts with their solds.
""" """
session = db.create_scoped_session()
query=session.query( query = session.query(
Account.id.label("id"), Account.id.label("id"),
Account.name.label("name"), Account.name.label("name"),
func.sum(Entry.value).label("future"), func.sum(Entry.value).label("future"),
@ -34,7 +34,9 @@ def get_accounts():
@app.route("/api/accounts/<account_id>/months") @app.route("/api/accounts/<account_id>/months")
def get_months(account_id): def get_months(account_id):
query=session.query( session = db.create_scoped_session()
query = session.query(
distinct(extract("year", Entry.value_date)).label("year"), distinct(extract("year", Entry.value_date)).label("year"),
extract("month", Entry.value_date).label("month") extract("month", Entry.value_date).label("month")
).filter(Entry.account_id == account_id).order_by("year", "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"]) @app.route("/api/accounts", methods=["PUT"])
def add_account(): def add_account():
with session.begin(): session = db.create_scoped_session()
try:
account = Account(request.json['name'])
session.add(account) try:
session.commit() account = Account(request.json['name'])
return json.dumps("Account added.") session.add(account)
except: session.commit()
session.rollback()
raise return json.dumps("Account added.")
except:
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["PUT"]) @app.route("/api/accounts/<account_id>", methods=["PUT"])
def update_account(account_id): def update_account(account_id):
with session.begin(): session = db.create_scoped_session()
try:
account = session.query(Account).filter(Account.id == account_id).first()
account.name = request.json['name'] try:
account = session.query(Account).filter(Account.id == account_id).first()
session.merge(account) account.name = request.json['name']
session.commit()
return json.dumps("Account #%s updated." % account_id) session.merge(account)
except: session.commit()
session.rollback()
raise return json.dumps("Account #%s updated." % account_id)
except:
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["DELETE"]) @app.route("/api/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id): def delete_account(account_id):
with session.begin(): session = db.create_scoped_session()
try:
account = session.query(Account).filter(Account.id == account_id).first() try:
account = session.query(Account).filter(Account.id == account_id).first()
session.delete(account)
session.commit() session.delete(account)
session.commit()
return json.dumps("Account #%s deleted." % account_id)
except: return json.dumps("Account #%s deleted." % account_id)
session.rollback() except:
raise session.rollback()
raise

View File

@ -1,6 +1,5 @@
from app import app from app import app
from app import db from app import db
from app import session
from api.model.entries import Entry 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. Return entries for an account, year, and month.
""" """
query=session.query( session = db.create_scoped_session()
query = session.query(
Entry Entry
).select_from( ).select_from(
session.query(Entry) session.query(Entry)
@ -45,58 +46,61 @@ def get_entries(account_id, year, month):
@app.route("/api/entries", methods=["PUT"]) @app.route("/api/entries", methods=["PUT"])
def add_entry(): def add_entry():
with session.begin(): session = db.create_scoped_session()
try:
entry = Entry( try:
value_date = request.json['value_date'], entry = Entry(
operation_date = request.json['operation_date'], value_date = request.json['value_date'],
label = request.json['label'], operation_date = request.json['operation_date'],
value = request.json['value'], label = request.json['label'],
category = request.json['category'], value = request.json['value'],
account_id = request.json['account_id'] category = request.json['category'],
) account_id = request.json['account_id']
)
session.add(entry)
session.commit() session.add(entry)
session.commit()
return json.dumps("Entry added.")
except: return json.dumps("Entry added.")
session.rollback() except:
raise session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["PUT"]) @app.route("/api/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id): def update_entry(entry_id):
with session.begin(): session = db.create_scoped_session()
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first() try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()
entry.id = entry_id
entry.value_date = request.json['value_date'] entry.id = entry_id
entry.operation_date = request.json['operation_date'] entry.value_date = request.json['value_date']
entry.label = request.json['label'] entry.operation_date = request.json['operation_date']
entry.value = request.json['value'] entry.label = request.json['label']
entry.category = request.json['category'] entry.value = request.json['value']
entry.account_id = request.json['account_id'] entry.category = request.json['category']
entry.account_id = request.json['account_id']
session.merge(entry)
session.commit() session.merge(entry)
session.commit()
return json.dumps("Entry #%s updated." % entry_id)
except: return json.dumps("Entry #%s updated." % entry_id)
session.rollback() except:
raise session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["DELETE"]) @app.route("/api/entries/<entry_id>", methods=["DELETE"])
def delete_entry(entry_id): def delete_entry(entry_id):
with session.begin(): session = db.create_scoped_session()
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first() try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()
session.delete(entry)
session.commit() session.delete(entry)
session.commit()
return json.dumps("Entry #%s deleted." % entry_id)
except: return json.dumps("Entry #%s deleted." % entry_id)
session.rollback() except:
raise session.rollback()
raise

View File

@ -9,5 +9,4 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://accountant:accountant@loca
app.config['SQLALCHEMY_RECORD_QUERIES'] = True app.config['SQLALCHEMY_RECORD_QUERIES'] = True
db = SQLAlchemy(app) db = SQLAlchemy(app)
session = db.create_scoped_session()