Rewrote the application in Python/Flask/SQLAlchemy
This commit is contained in:
0
src/api/__init__.py
Normal file
0
src/api/__init__.py
Normal file
0
src/api/controller/__init__.py
Normal file
0
src/api/controller/__init__.py
Normal file
75
src/api/controller/accounts.py
Normal file
75
src/api/controller/accounts.py
Normal file
@ -0,0 +1,75 @@
|
||||
|
||||
from app import app
|
||||
from app import db
|
||||
from app import session
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
query=session.query(
|
||||
Account.id.label("id"),
|
||||
Account.name.label("name"),
|
||||
func.sum(Entry.value).label("future"),
|
||||
func.sum(case([(Entry.operation_date != None, Entry.value,)], else_=cast(0, db.Numeric(15, 2)))).label("pointed"),
|
||||
func.sum(case([(Entry.value_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,
|
||||
"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):
|
||||
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")
|
||||
|
||||
return json.dumps([{
|
||||
"year": i.year,
|
||||
"month": i.month
|
||||
} for i in query.all()])
|
||||
|
||||
@app.route("/api/accounts", methods=["PUT"])
|
||||
def add_account():
|
||||
account = Account(request.json['name'])
|
||||
|
||||
session.add(account)
|
||||
session.commit()
|
||||
|
||||
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()
|
||||
|
||||
account.name = request.json['name']
|
||||
|
||||
session.merge(account)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Account #%s updated." % account_id)
|
||||
|
||||
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
||||
def delete_account(account_id):
|
||||
account = session.query(Account).filter(Account.id == account_id).first()
|
||||
|
||||
session.delete(account)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Account #%s deleted." % account_id)
|
||||
|
88
src/api/controller/entries.py
Normal file
88
src/api/controller/entries.py
Normal file
@ -0,0 +1,88 @@
|
||||
from app import app
|
||||
from app import db
|
||||
from app import session
|
||||
|
||||
from api.model.entries import Entry
|
||||
|
||||
from sqlalchemy import func, desc
|
||||
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
|
||||
from sqlalchemy.orm import sessionmaker, column_property
|
||||
from sqlalchemy.sql import func, select
|
||||
|
||||
#from sqlalchemy import *
|
||||
|
||||
from flask import json, request
|
||||
|
||||
@app.route("/api/entries/<account_id>/<year>/<month>")
|
||||
def get_entries(account_id, year, month):
|
||||
"""
|
||||
Return entries for an account, year, and month.
|
||||
"""
|
||||
query=session.query(
|
||||
Entry
|
||||
).select_from(
|
||||
session.query(Entry)
|
||||
.filter(Entry.account_id == account_id)
|
||||
.order_by(
|
||||
desc(Entry.value_date),
|
||||
desc(Entry.operation_date),
|
||||
Entry.label,
|
||||
Entry.value
|
||||
).subquery()
|
||||
).filter(func.date_trunc('month', Entry.value_date) == "%s-%s-01" % (year, month))
|
||||
|
||||
return json.dumps([{
|
||||
"id": i.id,
|
||||
"value_date": i.value_date.strftime("%Y-%m-%d"),
|
||||
"operation_date": i.operation_date.strftime("%Y-%m-%d") if i.operation_date else None,
|
||||
"label": i.label,
|
||||
"value": str(i.value),
|
||||
"category": i.category,
|
||||
"sold": str(i.sold),
|
||||
"pointedsold": str(i.pointedsold),
|
||||
"account_id": i.account_id
|
||||
} for i in query.all()])
|
||||
|
||||
@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']
|
||||
)
|
||||
|
||||
session.add(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry added.")
|
||||
|
||||
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
||||
def update_entry(entry_id):
|
||||
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']
|
||||
|
||||
session.merge(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry #%s updated." % entry_id)
|
||||
|
||||
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
||||
def delete_entry(entry_id):
|
||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||
|
||||
session.delete(entry)
|
||||
session.commit()
|
||||
|
||||
return json.dumps("Entry #%s deleted." % entry_id)
|
||||
|
||||
|
0
src/api/model/__init__.py
Normal file
0
src/api/model/__init__.py
Normal file
12
src/api/model/accounts.py
Normal file
12
src/api/model/accounts.py
Normal file
@ -0,0 +1,12 @@
|
||||
from app import app
|
||||
from app import db
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
class Account(db.Model):
|
||||
id = db.Column(db.Integer, primary_key = True)
|
||||
name = db.Column(db.String(200), nullable = False)
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
33
src/api/model/entries.py
Normal file
33
src/api/model/entries.py
Normal file
@ -0,0 +1,33 @@
|
||||
from app import app
|
||||
from app import db
|
||||
|
||||
from api.model.accounts import Account
|
||||
|
||||
from sqlalchemy import func, desc
|
||||
from sqlalchemy.orm import column_property
|
||||
from sqlalchemy.sql import func, select
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
class Entry(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
value_date = db.Column(db.Date, nullable = False)
|
||||
operation_date = db.Column(db.Date, nullable = True)
|
||||
label = db.Column(db.String(500), nullable = False)
|
||||
value = db.Column(db.Numeric(15, 2), nullable = False)
|
||||
account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
|
||||
|
||||
account = db.relationship(Account, backref = db.backref('entry', lazy="Dynamic"))
|
||||
|
||||
category = db.Column(db.String(100), nullable = True)
|
||||
sold = column_property(func.sum(value).over(order_by="value_date, operation_date, label desc, value desc"))
|
||||
pointedsold = column_property(func.sum(value).over(partition_by="operation_date is not null", order_by="value_date, operation_date, label desc, value desc"))
|
||||
|
||||
def __init__(self, value_date, label, value, account_id, operation_date = None, category = None):
|
||||
self.value_date = value_date
|
||||
self.operation_date = operation_date
|
||||
self.label = label
|
||||
self.value = value
|
||||
self.account_id = account_id
|
||||
self.category = category
|
||||
|
Reference in New Issue
Block a user