137 lines
3.9 KiB
Python
137 lines
3.9 KiB
Python
"""
|
|
This file is part of Accountant.
|
|
|
|
Accountant is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Accountant is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
from flask.ext.restful import Resource, fields, marshal_with_field
|
|
|
|
from sqlalchemy import true
|
|
|
|
from accountant import db
|
|
|
|
from ..models.scheduled_operations import ScheduledOperation
|
|
from ..models.operations import Operation
|
|
|
|
from .. import api
|
|
|
|
from .models import scheduled_operation_model
|
|
from .parsers import account_id_parser, scheduled_operation_parser
|
|
from .users import requires_auth
|
|
|
|
from ..fields import Object
|
|
|
|
|
|
class ScheduledOperationListResource(Resource):
|
|
@requires_auth
|
|
@marshal_with_field(fields.List(Object(scheduled_operation_model)))
|
|
def get(self):
|
|
"""
|
|
Get all scheduled operation for an account.
|
|
"""
|
|
data = account_id_parser.parse_args()
|
|
|
|
return ScheduledOperation.query().filter_by(**data).all(), 200
|
|
|
|
@requires_auth
|
|
@marshal_with_field(Object(scheduled_operation_model))
|
|
def post(self):
|
|
"""
|
|
Add a new scheduled operation.
|
|
"""
|
|
data = scheduled_operation_parser.parse_args()
|
|
|
|
scheduled_operation = ScheduledOperation(**data)
|
|
|
|
db.session.add(scheduled_operation)
|
|
|
|
scheduled_operation.reschedule()
|
|
|
|
db.session.flush()
|
|
|
|
return scheduled_operation, 201
|
|
|
|
|
|
class ScheduledOperationResource(Resource):
|
|
@requires_auth
|
|
@marshal_with_field(Object(scheduled_operation_model))
|
|
def get(self, id):
|
|
"""
|
|
Get scheduled operation.
|
|
"""
|
|
scheduled_operation = ScheduledOperation.query().get(id)
|
|
|
|
if not scheduled_operation:
|
|
return None, 404
|
|
|
|
return scheduled_operation, 200
|
|
|
|
@requires_auth
|
|
@marshal_with_field(Object(scheduled_operation_model))
|
|
def post(self, id):
|
|
"""
|
|
Update a scheduled operation.
|
|
"""
|
|
data = scheduled_operation_parser.parse_args()
|
|
|
|
assert (id not in data or data.id is None
|
|
or data.id == id)
|
|
|
|
scheduled_operation = ScheduledOperation.query().get(id)
|
|
|
|
if not scheduled_operation:
|
|
return None, 404
|
|
|
|
# SQLAlchemy objects ignore __dict__.update() with merge.
|
|
for k, v in data.items():
|
|
setattr(scheduled_operation, k, v)
|
|
|
|
db.session.merge(scheduled_operation)
|
|
|
|
scheduled_operation.reschedule()
|
|
|
|
db.session.flush()
|
|
|
|
return scheduled_operation, 200
|
|
|
|
@requires_auth
|
|
@marshal_with_field(Object(scheduled_operation_model))
|
|
def delete(self, id):
|
|
"""
|
|
Delete a scheduled operation.
|
|
"""
|
|
scheduled_operation = ScheduledOperation.query().get(id)
|
|
|
|
if not scheduled_operation:
|
|
return None, 404
|
|
|
|
operations = scheduled_operation.operations.filter(
|
|
Operation.confirmed == true()
|
|
).count()
|
|
|
|
if operations:
|
|
return "There are still confirmed operations associated to this \
|
|
scheduled operation.", 409
|
|
|
|
# Delete unconfirmed operations
|
|
operations = scheduled_operation.operations.delete()
|
|
|
|
db.session.delete(scheduled_operation)
|
|
|
|
return None, 204
|
|
|
|
|
|
api.add_resource(ScheduledOperationListResource, "/scheduled_operation")
|
|
api.add_resource(ScheduledOperationResource,
|
|
"/scheduled_operation/<int:id>")
|