2015-06-15 13:05:52 +02:00

135 lines
3.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 .. import api_api
from ..models.entries import Entry
from ..models.operations import Operation
from ..fields import Object
resource_fields = {
'id': fields.Integer(default=None),
'operation_date': fields.DateTime(dt_format='iso8601'),
'label': fields.String,
'value': fields.Float,
'pointed': fields.Boolean,
'category': fields.String,
'account_id': fields.Integer,
'scheduled_operation_id': fields.Integer(default=None),
'sold': fields.Float,
'canceled': fields.Boolean,
}
parser = reqparse.RequestParser()
# Must use lambda because the parser passes other parameters badly interpreted
# by dateutil.parser.parse
parser.add_argument('operation_date', type=lambda a: dateutil.parser.parse(a))
parser.add_argument('label', type=str)
parser.add_argument('value', type=float)
parser.add_argument('pointed', type=bool)
parser.add_argument('category', type=str)
parser.add_argument('account_id', type=int)
parser.add_argument('scheduled_operation_id', type=int)
range_parser = reqparse.RequestParser()
range_parser.add_argument('account', type=int)
range_parser.add_argument('begin', type=lambda a: dateutil.parser.parse(a))
range_parser.add_argument('end', type=lambda a: dateutil.parser.parse(a))
class EntryListResource(Resource):
@session_aware
@marshal_with_field(fields.List(Object(resource_fields)))
def get(self, session):
kwargs = range_parser.parse_args()
return Operation.get_for_account_and_range(session, **kwargs).all()
def put(self, *args):
return self.post()
@session_aware
@marshal_with_field(Object(resource_fields))
def post(self, session):
kwargs = parser.parse_args()
entry = Entry(**kwargs)
session.add(entry)
return entry
class EntryResource(Resource):
@session_aware
@marshal_with_field(Object(resource_fields))
def get(self, entry_id, session):
"""
Get entry.
"""
try:
return Entry.get(session, entry_id)
except NoResultFound:
return None, 404
@session_aware
@marshal_with_field(Object(resource_fields))
def put(self, entry_id, session):
kwargs = parser.parse_args()
assert (id not in kwargs or kwargs.id is None
or kwargs.id == entry_id)
try:
entry = Entry.get(session, entry_id)
except NoResultFound:
return None, 404
# SQLAlchemy objects ignore __dict__.update() with merge.
for k, v in kwargs.items():
setattr(entry, k, v)
session.merge(entry)
return entry
@session_aware
@marshal_with_field(Object(resource_fields))
def delete(self, entry_id, session):
try:
entry = Entry.get(session, entry_id)
except NoResultFound:
return None, 404
session.delete(entry)
return entry
api_api.add_resource(EntryListResource, "/entries")
api_api.add_resource(EntryResource, "/entries/<int:entry_id>")