89 lines
2.5 KiB
Python
89 lines
2.5 KiB
Python
|
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)
|
||
|
|
||
|
|