diff --git a/accountant/api/views/accounts.py b/accountant/api/views/accounts.py index f6e39b7..f02b370 100644 --- a/accountant/api/views/accounts.py +++ b/accountant/api/views/accounts.py @@ -25,27 +25,10 @@ from ..models.operations import Operation from ..fields import Object -from .users import requires_auth +from .models import (account_model, solds_model, balance_model, + category_model, ohlc_model) from .parsers import account_parser, range_parser - - -account_model = { - 'id': fields.Integer(default=None), - 'name': fields.String, - 'authorized_overdraft': fields.Float, -} - -solds_model = { - 'current': fields.Float, - 'pointed': fields.Float, - 'future': fields.Float, -} - -balance_model = { - 'expenses': fields.Float, - 'revenues': fields.Float, - 'balance': fields.Float, -} +from .users import requires_auth class AccountListResource(Resource): @@ -167,13 +150,6 @@ class BalanceResource(Resource): return account.balance(**data), 200 -category_model = { - 'category': fields.String, - 'expenses': fields.Float, - 'revenues': fields.Float -} - - class CategoryResource(Resource): @requires_auth @marshal_with_field(fields.List(Object(category_model))) @@ -183,15 +159,6 @@ class CategoryResource(Resource): return Operation.get_categories_for_range(id, **data).all() -ohlc_model = { - 'operation_date': fields.DateTime(dt_format='iso8601'), - 'open': fields.Float, - 'high': fields.Float, - 'low': fields.Float, - 'close': fields.Float -} - - class OHLCResource(Resource): @requires_auth @marshal_with_field(fields.List(Object(ohlc_model))) diff --git a/accountant/api/views/models.py b/accountant/api/views/models.py new file mode 100644 index 0000000..ad6f8d1 --- /dev/null +++ b/accountant/api/views/models.py @@ -0,0 +1,96 @@ +""" + 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 . +""" +from flask.ext.restful import fields + +# Account model. +account_model = { + 'id': fields.Integer(default=None), + 'name': fields.String, + 'authorized_overdraft': fields.Float, +} + +# Account status model. +solds_model = { + 'current': fields.Float, + 'pointed': fields.Float, + 'future': fields.Float, +} + +# Account balance model. +balance_model = { + 'expenses': fields.Float, + 'revenues': fields.Float, + 'balance': fields.Float, +} + +# Category with expenses and revenues. +category_model = { + 'category': fields.String, + 'expenses': fields.Float, + 'revenues': fields.Float +} + +# OHLC model. +ohlc_model = { + 'operation_date': fields.DateTime(dt_format='iso8601'), + 'open': fields.Float, + 'high': fields.Float, + 'low': fields.Float, + 'close': fields.Float +} + +# Operation with sold model. +operation_with_sold_model = { + '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, + 'confirmed': fields.Boolean, + 'canceled': fields.Boolean, +} + +# Scheduled operation model. +scheduled_operation_model = { + '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, +} + +# Token with expiration time and type. +token_model = { + 'token': fields.String, + 'expiration': fields.DateTime(dt_format='iso8601'), + 'token_type': fields.String +} + +# User model. +user_model = { + 'id': fields.Integer(default=None), + 'email': fields.String, + 'active': fields.Boolean +} diff --git a/accountant/api/views/operations.py b/accountant/api/views/operations.py index f0ad12f..1438c8f 100644 --- a/accountant/api/views/operations.py +++ b/accountant/api/views/operations.py @@ -22,29 +22,13 @@ from .. import api from ..models.operations import Operation -from .users import requires_auth +from .models import operation_model, operation_with_sold_model from .parsers import operation_parser, account_range_parser +from .users import requires_auth from ..fields import Object -operation_with_sold_model = { - '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, - 'confirmed': fields.Boolean, - 'canceled': fields.Boolean, -} - -operation_model = operation_with_sold_model - - class OperationListResource(Resource): @requires_auth @marshal_with_field(fields.List(Object(operation_with_sold_model))) diff --git a/accountant/api/views/scheduled_operations.py b/accountant/api/views/scheduled_operations.py index 9aeb0a6..0ddf71b 100644 --- a/accountant/api/views/scheduled_operations.py +++ b/accountant/api/views/scheduled_operations.py @@ -25,25 +25,13 @@ from ..models.operations import Operation from .. import api -from .users import requires_auth +from .models import scheduled_operation_model from .parsers import account_id_parser, scheduled_operation_parser +from .users import requires_auth from ..fields import Object -scheduled_operation_model = { - '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, -} - - class ScheduledOperationListResource(Resource): @requires_auth @marshal_with_field(fields.List(Object(scheduled_operation_model))) diff --git a/accountant/api/views/users.py b/accountant/api/views/users.py index 5d35bd3..77078f6 100644 --- a/accountant/api/views/users.py +++ b/accountant/api/views/users.py @@ -20,7 +20,7 @@ import arrow from functools import wraps from flask import request, g -from flask.ext.restful import Resource, fields, marshal_with, marshal_with_field +from flask.ext.restful import Resource, marshal_with, marshal_with_field from accountant import app @@ -30,6 +30,7 @@ from ..fields import Object from ..models.users import User +from .models import token_model, user_model from .parsers import login_parser @@ -63,19 +64,6 @@ def requires_auth(f): return wrapped -token_model = { - 'token': fields.String, - 'expiration': fields.DateTime(dt_format='iso8601'), - 'token_type': fields.String -} - -user_model = { - 'id': fields.Integer(default=None), - 'email': fields.String, - 'active': fields.Boolean -} - - class LoginResource(Resource): @marshal_with(token_model) def post(self):