From 7d9b94bc80d71fb853addd1a351a013248d3d54c Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Tue, 12 Jan 2016 22:36:22 +0100 Subject: [PATCH] Move some resources from operations to account. --- accountant/api/views/accounts.py | 45 ++++++++++++++++++++++++++++++ accountant/api/views/operations.py | 36 ------------------------ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/accountant/api/views/accounts.py b/accountant/api/views/accounts.py index d3b0508..84b803f 100644 --- a/accountant/api/views/accounts.py +++ b/accountant/api/views/accounts.py @@ -25,6 +25,7 @@ from accountant import db from .. import api_api from ..models.accounts import Account +from ..models.operations import Operation from ..fields import Object @@ -150,3 +151,47 @@ class AccountResource(Resource): api_api.add_resource(AccountListResource, '/accounts') api_api.add_resource(AccountResource, '/accounts/') + + +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)) + + +category_resource_fields = { + 'category': fields.String, + 'expenses': fields.Float, + 'revenues': fields.Float +} + + +class CategoriesResource(Resource): + @requires_auth + @marshal_with_field(fields.List(Object(category_resource_fields))) + def get(self): + kwargs = range_parser.parse_args() + + return Operation.get_categories_for_range(**kwargs).all() + + +ohlc_resource_fields = { + 'operation_date': fields.DateTime(dt_format='iso8601'), + 'open': fields.Float, + 'high': fields.Float, + 'low': fields.Float, + 'close': fields.Float +} + + +class SoldsResource(Resource): + @requires_auth + @marshal_with_field(fields.List(Object(ohlc_resource_fields))) + def get(self): + kwargs = range_parser.parse_args() + + return Operation.get_ohlc_per_day_for_range(**kwargs).all() + + +api_api.add_resource(CategoriesResource, "/categories") +api_api.add_resource(SoldsResource, "/solds") diff --git a/accountant/api/views/operations.py b/accountant/api/views/operations.py index 8f023c8..591af10 100644 --- a/accountant/api/views/operations.py +++ b/accountant/api/views/operations.py @@ -136,41 +136,5 @@ class OperationResource(Resource): return operation -category_resource_fields = { - 'category': fields.String, - 'expenses': fields.Float, - 'revenues': fields.Float -} - - -class CategoriesResource(Resource): - @requires_auth - @marshal_with_field(fields.List(Object(category_resource_fields))) - def get(self): - kwargs = range_parser.parse_args() - - return Operation.get_categories_for_range(**kwargs).all() - - -ohlc_resource_fields = { - 'operation_date': fields.DateTime(dt_format='iso8601'), - 'open': fields.Float, - 'high': fields.Float, - 'low': fields.Float, - 'close': fields.Float -} - - -class SoldsResource(Resource): - @requires_auth - @marshal_with_field(fields.List(Object(ohlc_resource_fields))) - def get(self): - kwargs = range_parser.parse_args() - - return Operation.get_ohlc_per_day_for_range(**kwargs).all() - - api_api.add_resource(OperationListResource, "/operations") api_api.add_resource(OperationResource, "/operations/") -api_api.add_resource(CategoriesResource, "/categories") -api_api.add_resource(SoldsResource, "/solds")