from .. import api from ..model import db, session_scope from ..model.scheduled_operations import ScheduledOperation from flask import json, request 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 @api.route("/scheduled_operations/") def get_scheduled_operations(account_id): """ Return entries for an account, year, and month. """ 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() ) 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()]) @api.route("/scheduled_operations", methods=["PUT"]) def add_scheduled_operation(): with session_scope() as session: 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'] ) session.add(scheduledOperation) return json.dumps("Scheduled operation added.") @api.route("/scheduled_operations/", methods=["PUT"]) def update_scheduled_operation(scheduled_operation_id): with session_scope() as session: scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first() 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'] session.merge(scheduledOperation) return json.dumps("Scheduled operation #%s updated." % scheduled_operation_id) @api.route("/scheduled_operations/", methods=["DELETE"]) def delete_scheduled_operation(scheduled_operation_id): with session_scope() as session: scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first() session.delete(scheduledOperation) return json.dumps("Scheduled operation #%s deleted." % scheduled_operation_id)