accountant-ui/src/api/controller/accounts.py

115 lines
3.5 KiB
Python
Raw Normal View History

2013-02-08 13:07:27 +01:00
"""
This file is part of Accountant.
2013-02-08 13:07:27 +01:00
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from app import db
from api.model.accounts import Account
from api.model.entries import Entry
from flask import json, request
from sqlalchemy import func, case, cast, extract, distinct
@app.route("/api/accounts", methods=["GET"])
def get_accounts():
"""
Returns accounts with their solds.
"""
2013-01-25 12:53:49 +01:00
session = db.session
2013-01-24 15:27:08 +01:00
query = session.query(
Account.id.label("id"),
Account.name.label("name"),
2013-01-24 20:44:09 +01:00
Account.authorized_overdraft.label("authorized_overdraft"),
func.sum(Entry.value).label("future"),
func.sum(case([(Entry.pointed, Entry.value,)], else_=cast(0, db.Numeric(15, 2)))).label("pointed"),
func.sum(case([(Entry.operation_date < func.now(), Entry.value,)], else_=cast(0, db.Numeric(15, 2)))).label("current")
).outerjoin(Entry).group_by(Account.id).order_by(Account.id)
return json.dumps([{
"id": i.id,
"name": i.name,
2013-01-24 20:44:09 +01:00
"authorized_overdraft": i.authorized_overdraft,
"current": str(i.current),
"pointed": str(i.pointed),
"future": str(i.future)
} for i in query.all()])
@app.route("/api/accounts/<account_id>/months")
def get_months(account_id):
2013-01-25 12:53:49 +01:00
session = db.session
2013-01-24 15:27:08 +01:00
query = session.query(
distinct(cast(extract("year", Entry.operation_date), db.String)).label("year"),
cast(extract("month", Entry.operation_date), db.String).label("month")
).filter(Entry.account_id == account_id).order_by("year", "month")
return json.dumps([{
"year": i.year,
"month": i.month.rjust(2, '0')
} for i in query.all()])
@app.route("/api/accounts", methods=["PUT"])
def add_account():
2013-01-25 12:53:49 +01:00
session = db.session
2013-01-24 15:27:08 +01:00
try:
2013-01-24 20:44:09 +01:00
account = Account(request.json['name'], request.json['authorized_overdraft'])
2013-01-24 15:27:08 +01:00
session.add(account)
session.commit()
2013-01-24 15:27:08 +01:00
return json.dumps("Account added.")
except:
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["PUT"])
def update_account(account_id):
2013-01-25 12:53:49 +01:00
session = db.session
2013-01-24 15:27:08 +01:00
try:
account = session.query(Account).filter(Account.id == account_id).first()
2013-01-24 15:27:08 +01:00
account.name = request.json['name']
2013-01-24 20:44:09 +01:00
account.authorized_overdraft = request.json['authorized_overdraft']
2013-01-24 15:27:08 +01:00
session.merge(account)
session.commit()
2013-01-24 15:27:08 +01:00
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):
2013-01-25 12:53:49 +01:00
session = db.session
2013-01-24 15:27:08 +01:00
try:
account = session.query(Account).filter(Account.id == account_id).first()
session.delete(account)
session.commit()
return json.dumps("Account #%s deleted." % account_id)
except:
session.rollback()
raise