From b9838350237caa66355461c3e11cc8968ee7f341 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 13 Jun 2015 01:27:32 +0200 Subject: [PATCH] Replace method to use get with range in paramters. --- accountant/api/views/accounts.py | 49 +++++++++-------------- accountant/frontend/static/js/entries.js | 11 ++++- accountant/frontend/templates/layout.html | 2 + 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/accountant/api/views/accounts.py b/accountant/api/views/accounts.py index 71e012e..5f30413 100644 --- a/accountant/api/views/accounts.py +++ b/accountant/api/views/accounts.py @@ -14,7 +14,9 @@ You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . """ -from flask import json, request +import dateutil.parser + +from flask import json from flask.ext.restful import Resource, fields, reqparse, marshal_with_field from sqlalchemy.orm.exc import NoResultFound @@ -26,34 +28,10 @@ from .. import api, api_api from ..models.accounts import Account from ..models.entries import Entry -from ..models.operations import Operation from ..fields import Object -@api.route("/accounts////") -@auth.login_required -def get_account_status(account_id, year, month): - with session_scope() as session: - query = Operation.get_account_status(session, account_id, year, month) - - if query.count() == 1: - result = query.one() - revenues = result.revenues - expenses = result.expenses - balance = result.balance - else: - revenues = 0.0 - expenses = 0.0 - balance = 0.0 - - return json.dumps({ - "expenses": str(expenses), - "revenues": str(revenues), - "balance": str(balance) - }) - - @api.route("/accounts//months") @auth.login_required def get_months(account_id): @@ -67,12 +45,15 @@ def get_months(account_id): resource_fields = { - 'id': fields.Integer, + 'id': fields.Integer(default=None), 'name': fields.String, 'authorized_overdraft': fields.Fixed(decimals=2), - 'current': fields.Float, - 'pointed': fields.Float, - 'future': fields.Float, + 'current': fields.Fixed(decimals=2), + 'pointed': fields.Fixed(decimals=2), + 'future': fields.Fixed(decimals=2), + 'expenses': fields.Fixed(decimals=2), + 'revenues': fields.Fixed(decimals=2), + 'balance': fields.Fixed(decimals=2), } @@ -80,6 +61,12 @@ parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('authorized_overdraft', type=float, required=True) +date_parser = reqparse.RequestParser() +date_parser.add_argument('begin', + type=lambda a: dateutil.parser.parse(a) if a else None) +date_parser.add_argument('end', + type=lambda a: dateutil.parser.parse(a) if a else None) + class AccountListResource(Resource): @session_aware @@ -124,8 +111,10 @@ class AccountResource(Resource): """ Get account. """ + kwargs = date_parser.parse_args() + try: - return Account.get(session, account_id) + return Account.get(session, account_id, **kwargs) except NoResultFound: return None, 404 diff --git a/accountant/frontend/static/js/entries.js b/accountant/frontend/static/js/entries.js index 7536f6b..7cc7223 100644 --- a/accountant/frontend/static/js/entries.js +++ b/accountant/frontend/static/js/entries.js @@ -86,8 +86,15 @@ var EntryController = function($scope, $http, $rootScope, $filter) { $scope.getAccountStatus = function(account, month) { if(account != null && month != null) { - $http.get("/api/accounts/" + account.id + "/" + month.year + "/" + month.month). - success($scope.getAccountStatus_success); + // Note: Month is 0 indexed. + var begin = moment({year: month.year, month: month.month - 1, day: 1}); + var end = begin.clone().endOf('month'); + + $http.get("/api/accounts/" + account.id, + {params: { + begin: begin.format('YYYY-MM-DD'), + end: end.format('YYYY-MM-DD') + }}).success($scope.getAccountStatus_success); } }; diff --git a/accountant/frontend/templates/layout.html b/accountant/frontend/templates/layout.html index 36e96a8..17f0b28 100644 --- a/accountant/frontend/templates/layout.html +++ b/accountant/frontend/templates/layout.html @@ -83,6 +83,8 @@ {% block footer %}{% endblock %} + +