Added transaction management in controllers.

This commit is contained in:
Alexis Lahouze 2013-01-24 14:58:28 +01:00
parent a7e26e7d96
commit f56e06223f
2 changed files with 79 additions and 49 deletions

View File

@ -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/<account_id>", 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/<account_id>", methods=["DELETE"])
def delete_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()
session.delete(account)
session.commit()
session.delete(account)
session.commit()
return json.dumps("Account #%s deleted." % account_id)
return json.dumps("Account #%s deleted." % account_id)
except:
session.rollback()
raise

View File

@ -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']
)
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()
session.add(entry)
session.commit()
return json.dumps("Entry added.")
return json.dumps("Entry added.")
except:
session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id):
entry = session.query(Entry).filter(Entry.id == entry_id).first()
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']
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()
session.merge(entry)
session.commit()
return json.dumps("Entry #%s updated." % entry_id)
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):
entry = session.query(Entry).filter(Entry.id == entry_id).first()
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)
session.delete(entry)
session.commit()
return json.dumps("Entry #%s deleted." % entry_id)
except:
session.rollback()
raise