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"]) @app.route("/api/accounts", methods=["PUT"])
def add_account(): def add_account():
account = Account(request.json['name']) with session.begin():
try:
account = Account(request.json['name'])
session.add(account) session.add(account)
session.commit() session.commit()
return json.dumps("Account added.")
except:
session.rollback()
raise
return json.dumps("Account added.")
@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):
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.merge(account)
session.commit() 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"]) @app.route("/api/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id): def delete_account(account_id):
account = session.query(Account).filter(Account.id == account_id).first() with session.begin():
try:
session.delete(account) account = session.query(Account).filter(Account.id == account_id).first()
session.commit()
session.delete(account)
return json.dumps("Account #%s deleted." % account_id) session.commit()
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"]) @app.route("/api/entries", methods=["PUT"])
def add_entry(): def add_entry():
entry = Entry( with session.begin():
value_date = request.json['value_date'], try:
operation_date = request.json['operation_date'], entry = Entry(
label = request.json['label'], value_date = request.json['value_date'],
value = request.json['value'], operation_date = request.json['operation_date'],
category = request.json['category'], label = request.json['label'],
account_id = request.json['account_id'] value = request.json['value'],
) category = request.json['category'],
account_id = request.json['account_id']
session.add(entry) )
session.commit()
session.add(entry)
return json.dumps("Entry added.") session.commit()
return json.dumps("Entry added.")
except:
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):
entry = session.query(Entry).filter(Entry.id == entry_id).first() with session.begin():
try:
entry.id = entry_id entry = session.query(Entry).filter(Entry.id == entry_id).first()
entry.value_date = request.json['value_date']
entry.operation_date = request.json['operation_date'] entry.id = entry_id
entry.label = request.json['label'] entry.value_date = request.json['value_date']
entry.value = request.json['value'] entry.operation_date = request.json['operation_date']
entry.category = request.json['category'] entry.label = request.json['label']
entry.account_id = request.json['account_id'] entry.value = request.json['value']
entry.category = request.json['category']
session.merge(entry) entry.account_id = request.json['account_id']
session.commit()
session.merge(entry)
return json.dumps("Entry #%s updated." % entry_id) session.commit()
return json.dumps("Entry #%s updated." % entry_id)
except:
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):
entry = session.query(Entry).filter(Entry.id == entry_id).first() with session.begin():
try:
session.delete(entry) entry = session.query(Entry).filter(Entry.id == entry_id).first()
session.commit()
session.delete(entry)
return json.dumps("Entry #%s deleted." % entry_id) session.commit()
return json.dumps("Entry #%s deleted." % entry_id)
except:
session.rollback()
raise