168 lines
4.8 KiB
Python
168 lines
4.8 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/>.
|
|
"""
|
|
import dateutil.parser
|
|
|
|
from flask.ext.restful import Resource, fields, reqparse, marshal_with_field
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from accountant import session_aware
|
|
|
|
from ..models.scheduled_operations import ScheduledOperation
|
|
|
|
from .. import api_api
|
|
|
|
from ..fields import Object
|
|
|
|
|
|
resource_fields = {
|
|
'id': fields.Integer,
|
|
'start_date': fields.DateTime(dt_format='iso8601'),
|
|
'stop_date': fields.DateTime(dt_format='iso8601'),
|
|
'day': fields.Integer,
|
|
'frequency': fields.Integer,
|
|
'label': fields.String,
|
|
'value': fields.Float,
|
|
'category': fields.String,
|
|
'account_id': fields.Integer,
|
|
}
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument('start_date', type=lambda a: dateutil.parser.parse(a))
|
|
parser.add_argument('stop_date', type=lambda a: dateutil.parser.parse(a))
|
|
parser.add_argument('day', type=int)
|
|
parser.add_argument('frequency', type=int)
|
|
parser.add_argument('label', type=str)
|
|
parser.add_argument('value', type=float)
|
|
parser.add_argument('category', type=str)
|
|
parser.add_argument('account_id', type=int)
|
|
|
|
|
|
get_parser = reqparse.RequestParser()
|
|
get_parser.add_argument('account', type=int)
|
|
|
|
|
|
class ScheduledOperationListResource(Resource):
|
|
@session_aware
|
|
@marshal_with_field(fields.List(Object(resource_fields)))
|
|
def get(self, session):
|
|
"""
|
|
Get all scheduled operation for the account.
|
|
"""
|
|
kwargs = get_parser.parse_args()
|
|
|
|
return ScheduledOperation.query(
|
|
session
|
|
).filter(
|
|
ScheduledOperation.account_id == kwargs.account
|
|
).all()
|
|
|
|
@session_aware
|
|
@marshal_with_field(Object(resource_fields))
|
|
def post(self, session):
|
|
"""
|
|
Add a new scheduled operation.
|
|
"""
|
|
kwargs = parser.parse_args()
|
|
|
|
scheduled_operation = ScheduledOperation(**kwargs)
|
|
|
|
session.add(scheduled_operation)
|
|
|
|
scheduled_operation.reschedule(session)
|
|
|
|
session.flush()
|
|
|
|
return scheduled_operation, 201
|
|
|
|
|
|
class ScheduledOperationResource(Resource):
|
|
@session_aware
|
|
@marshal_with_field(Object(resource_fields))
|
|
def get(self, scheduled_operation_id, session):
|
|
"""
|
|
Get scheduled operation.
|
|
"""
|
|
try:
|
|
return ScheduledOperation.query(
|
|
session
|
|
).filter(
|
|
ScheduledOperation.id == scheduled_operation_id
|
|
).one()
|
|
except NoResultFound:
|
|
return None, 404
|
|
|
|
@session_aware
|
|
@marshal_with_field(Object(resource_fields))
|
|
def delete(self, scheduled_operation_id, session):
|
|
"""
|
|
Delete a scheduled operation.
|
|
"""
|
|
|
|
raise NotImplementedError("Must be fixed.")
|
|
|
|
try:
|
|
scheduled_operation = ScheduledOperation.query(
|
|
session
|
|
).filter(
|
|
ScheduledOperation.id == scheduled_operation_id
|
|
).one()
|
|
except NoResultFound:
|
|
return None, 404
|
|
|
|
session.delete(scheduled_operation)
|
|
|
|
return scheduled_operation
|
|
|
|
@session_aware
|
|
@marshal_with_field(Object(resource_fields))
|
|
def post(self, scheduled_operation_id, session):
|
|
"""
|
|
Update a scheduled operation.
|
|
"""
|
|
kwargs = parser.parse_args()
|
|
|
|
assert (id not in kwargs or kwargs.id is None
|
|
or kwargs.id == scheduled_operation_id)
|
|
|
|
try:
|
|
scheduled_operation = ScheduledOperation.query(
|
|
session
|
|
).filter(
|
|
ScheduledOperation.id == scheduled_operation_id
|
|
).one()
|
|
except NoResultFound:
|
|
return None, 404
|
|
|
|
# SQLAlchemy objects ignore __dict__.update() with merge.
|
|
for k, v in kwargs.items():
|
|
setattr(scheduled_operation, k, v)
|
|
|
|
session.merge(scheduled_operation)
|
|
|
|
scheduled_operation.reschedule(session)
|
|
|
|
session.flush()
|
|
|
|
return scheduled_operation
|
|
|
|
|
|
api_api.add_resource(ScheduledOperationListResource, "/scheduled_operations")
|
|
api_api.add_resource(ScheduledOperationResource,
|
|
"/scheduled_operations/<int:scheduled_operation_id>")
|