2013-12-03 22:22:25 +01:00
|
|
|
from .. import api
|
2013-12-03 23:01:17 +01:00
|
|
|
from ..model import db, session_scope
|
2013-12-03 22:22:25 +01:00
|
|
|
from ..model.scheduled_operations import ScheduledOperation
|
|
|
|
from flask import json, request
|
2013-07-28 23:25:51 +02:00
|
|
|
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
|
|
|
|
|
2013-12-03 22:22:25 +01:00
|
|
|
@api.route("/scheduled_operations/<account_id>")
|
2013-07-28 23:25:51 +02:00
|
|
|
def get_scheduled_operations(account_id):
|
|
|
|
"""
|
|
|
|
Return entries for an account, year, and month.
|
|
|
|
"""
|
2013-12-03 23:01:17 +01:00
|
|
|
with session_scope() as session:
|
|
|
|
query = session.query(
|
|
|
|
ScheduledOperation
|
|
|
|
).select_from(
|
|
|
|
session.query(ScheduledOperation)
|
|
|
|
.filter(ScheduledOperation.account_id == account_id)
|
|
|
|
.order_by(
|
|
|
|
desc(ScheduledOperation.day),
|
|
|
|
ScheduledOperation.value,
|
|
|
|
ScheduledOperation.label,
|
|
|
|
).subquery()
|
|
|
|
)
|
2013-07-28 23:25:51 +02:00
|
|
|
|
2013-12-03 23:01:17 +01:00
|
|
|
return json.dumps([{
|
|
|
|
"id": i.id,
|
|
|
|
"start_date": i.start_date.strftime("%Y-%m-%d"),
|
|
|
|
"stop_date": i.stop_date.strftime("%Y-%m-%d"),
|
|
|
|
"day": str(i.day),
|
|
|
|
"frequency": str(i.frequency),
|
|
|
|
"label": i.label,
|
|
|
|
"value": str(i.value),
|
|
|
|
"category": i.category,
|
|
|
|
"account_id": i.account_id
|
|
|
|
} for i in query.all()])
|
2013-07-28 23:25:51 +02:00
|
|
|
|
2013-12-03 22:22:25 +01:00
|
|
|
@api.route("/scheduled_operations", methods=["PUT"])
|
2013-07-28 23:25:51 +02:00
|
|
|
def add_scheduled_operation():
|
2013-12-03 23:01:17 +01:00
|
|
|
with session_scope() as session:
|
2013-07-28 23:25:51 +02:00
|
|
|
scheduledOperation = ScheduledOperation(
|
|
|
|
start_date = request.json['start_date'],
|
|
|
|
stop_date = request.json['stop_date'],
|
|
|
|
day = request.json['day'],
|
|
|
|
frequency = request.json['frequency'],
|
|
|
|
label = request.json['label'],
|
|
|
|
value = request.json['value'],
|
|
|
|
category = request.json['category'],
|
|
|
|
account_id = request.json['account_id']
|
|
|
|
)
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
session.add(scheduledOperation)
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
return json.dumps("Scheduled operation added.")
|
|
|
|
|
2013-12-03 22:22:25 +01:00
|
|
|
@api.route("/scheduled_operations/<scheduled_operation_id>", methods=["PUT"])
|
2013-07-28 23:25:51 +02:00
|
|
|
def update_scheduled_operation(scheduled_operation_id):
|
2013-12-03 23:01:17 +01:00
|
|
|
with session_scope() as session:
|
2013-07-28 23:25:51 +02:00
|
|
|
scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first()
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
scheduledOperation.id = scheduled_operation_id
|
|
|
|
scheduledOperation.start_date = request.json['start_date'],
|
|
|
|
scheduledOperation.stop_date = request.json['stop_date'],
|
|
|
|
scheduledOperation.day = request.json['day'],
|
|
|
|
scheduledOperation.frequency = request.json['frequency'],
|
|
|
|
scheduledOperation.label = request.json['label']
|
|
|
|
scheduledOperation.value = request.json['value']
|
|
|
|
scheduledOperation.category = request.json['category']
|
|
|
|
scheduledOperation.account_id = request.json['account_id']
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
session.merge(scheduledOperation)
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
return json.dumps("Scheduled operation #%s updated." % scheduled_operation_id)
|
|
|
|
|
2013-12-03 22:22:25 +01:00
|
|
|
@api.route("/scheduled_operations/<scheduled_operation_id>", methods=["DELETE"])
|
2013-07-28 23:25:51 +02:00
|
|
|
def delete_scheduled_operation(scheduled_operation_id):
|
2013-12-03 23:01:17 +01:00
|
|
|
with session_scope() as session:
|
2013-07-28 23:25:51 +02:00
|
|
|
scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first()
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
session.delete(scheduledOperation)
|
2013-12-03 22:22:25 +01:00
|
|
|
|
2013-07-28 23:25:51 +02:00
|
|
|
return json.dumps("Scheduled operation #%s deleted." % scheduled_operation_id)
|
|
|
|
|