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 db
from app import session
from api.model.accounts import Account
from api.model.entries import Entry
@ -15,6 +14,7 @@ def get_accounts():
"""
Returns accounts with their solds.
"""
session = db.create_scoped_session()
query = session.query(
Account.id.label("id"),
@ -34,6 +34,8 @@ def get_accounts():
@app.route("/api/accounts/<account_id>/months")
def get_months(account_id):
session = db.create_scoped_session()
query = session.query(
distinct(extract("year", Entry.value_date)).label("year"),
extract("month", Entry.value_date).label("month")
@ -46,7 +48,8 @@ def get_months(account_id):
@app.route("/api/accounts", methods=["PUT"])
def add_account():
with session.begin():
session = db.create_scoped_session()
try:
account = Account(request.json['name'])
@ -61,7 +64,8 @@ def add_account():
@app.route("/api/accounts/<account_id>", methods=["PUT"])
def update_account(account_id):
with session.begin():
session = db.create_scoped_session()
try:
account = session.query(Account).filter(Account.id == account_id).first()
@ -77,7 +81,8 @@ def update_account(account_id):
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id):
with session.begin():
session = db.create_scoped_session()
try:
account = session.query(Account).filter(Account.id == account_id).first()

View File

@ -1,6 +1,5 @@
from app import app
from app import db
from app import session
from api.model.entries import Entry
@ -18,6 +17,8 @@ def get_entries(account_id, year, month):
"""
Return entries for an account, year, and month.
"""
session = db.create_scoped_session()
query = session.query(
Entry
).select_from(
@ -45,7 +46,8 @@ def get_entries(account_id, year, month):
@app.route("/api/entries", methods=["PUT"])
def add_entry():
with session.begin():
session = db.create_scoped_session()
try:
entry = Entry(
value_date = request.json['value_date'],
@ -66,7 +68,8 @@ def add_entry():
@app.route("/api/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id):
with session.begin():
session = db.create_scoped_session()
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()
@ -88,7 +91,8 @@ def update_entry(entry_id):
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
def delete_entry(entry_id):
with session.begin():
session = db.create_scoped_session()
try:
entry = session.query(Entry).filter(Entry.id == entry_id).first()

View File

@ -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()