Fixed (really) the session management.
This commit is contained in:
parent
f56e06223f
commit
cfef22ee65
@ -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/<account_id>/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/<account_id>", 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/<account_id>", methods=["DELETE"])
|
||||
def delete_account(account_id):
|
||||
with session.begin():
|
||||
try:
|
||||
account = session.query(Account).filter(Account.id == account_id).first()
|
||||
session = db.create_scoped_session()
|
||||
|
||||
session.delete(account)
|
||||
session.commit()
|
||||
try:
|
||||
account = session.query(Account).filter(Account.id == account_id).first()
|
||||
|
||||
return json.dumps("Account #%s deleted." % account_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
session.delete(account)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Account #%s deleted." % account_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
|
||||
|
@ -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 = db.create_scoped_session()
|
||||
|
||||
session.add(entry)
|
||||
session.commit()
|
||||
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']
|
||||
)
|
||||
|
||||
return json.dumps("Entry added.")
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
session.add(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry added.")
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
|
||||
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
||||
def update_entry(entry_id):
|
||||
with session.begin():
|
||||
try:
|
||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||
session = db.create_scoped_session()
|
||||
|
||||
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']
|
||||
try:
|
||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||
|
||||
session.merge(entry)
|
||||
session.commit()
|
||||
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']
|
||||
|
||||
return json.dumps("Entry #%s updated." % entry_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
session.merge(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry #%s updated." % entry_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
|
||||
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
||||
def delete_entry(entry_id):
|
||||
with session.begin():
|
||||
try:
|
||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||
session = db.create_scoped_session()
|
||||
|
||||
session.delete(entry)
|
||||
session.commit()
|
||||
try:
|
||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||
|
||||
return json.dumps("Entry #%s deleted." % entry_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
session.delete(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry #%s deleted." % entry_id)
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user