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