2013-01-24 00:01:42 +01:00
|
|
|
from app import app
|
|
|
|
from app import db
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2013-01-25 12:53:49 +01:00
|
|
|
session = db.session
|
2013-01-24 15:27:08 +01:00
|
|
|
|
|
|
|
query = session.query(
|
2013-01-24 00:01:42 +01:00
|
|
|
Entry
|
|
|
|
).select_from(
|
|
|
|
session.query(Entry)
|
|
|
|
.filter(Entry.account_id == account_id)
|
|
|
|
.order_by(
|
|
|
|
desc(Entry.operation_date),
|
2013-01-25 01:10:03 +01:00
|
|
|
Entry.value,
|
2013-01-24 00:01:42 +01:00
|
|
|
Entry.label,
|
|
|
|
).subquery()
|
2013-01-26 01:15:07 +01:00
|
|
|
).filter(func.date_trunc('month', Entry.operation_date) == "%s-%s-01" % (year, month))
|
2013-01-24 00:01:42 +01:00
|
|
|
|
|
|
|
return json.dumps([{
|
|
|
|
"id": i.id,
|
2013-01-26 01:15:07 +01:00
|
|
|
"pointed": i.pointed,
|
|
|
|
"operation_date": i.operation_date.strftime("%Y-%m-%d"),
|
2013-01-24 00:01:42 +01:00
|
|
|
"label": i.label,
|
|
|
|
"value": str(i.value),
|
|
|
|
"category": i.category,
|
|
|
|
"sold": str(i.sold),
|
|
|
|
"account_id": i.account_id
|
|
|
|
} for i in query.all()])
|
|
|
|
|
|
|
|
@app.route("/api/entries", methods=["PUT"])
|
|
|
|
def add_entry():
|
2013-01-25 12:53:49 +01:00
|
|
|
session = db.session
|
2013-01-24 15:27:08 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
entry = Entry(
|
|
|
|
operation_date = request.json['operation_date'],
|
2013-01-26 01:15:07 +01:00
|
|
|
pointed = request.json['pointed'],
|
2013-01-24 15:27:08 +01:00
|
|
|
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.")
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
2013-01-24 00:01:42 +01:00
|
|
|
|
|
|
|
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
|
|
|
def update_entry(entry_id):
|
2013-01-25 12:53:49 +01:00
|
|
|
session = db.session
|
2013-01-24 15:27:08 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
|
|
|
|
|
|
|
entry.id = entry_id
|
|
|
|
entry.operation_date = request.json['operation_date']
|
2013-01-26 01:15:07 +01:00
|
|
|
entry.pointed = request.json['pointed']
|
2013-01-24 15:27:08 +01:00
|
|
|
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)
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
2013-01-24 00:01:42 +01:00
|
|
|
|
|
|
|
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
|
|
|
def delete_entry(entry_id):
|
2013-01-25 12:53:49 +01:00
|
|
|
session = db.session
|
2013-01-24 15:27:08 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
|
|
|
|
|
|
|
session.delete(entry)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
return json.dumps("Entry #%s deleted." % entry_id)
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
2013-01-24 00:01:42 +01:00
|
|
|
|