Replace method to use get with range in paramters.

This commit is contained in:
Alexis Lahouze 2015-06-13 01:27:32 +02:00
parent 7b49cec047
commit b983835023
3 changed files with 30 additions and 32 deletions

View File

@ -14,7 +14,9 @@
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
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/<account_id>/<year>/<month>/")
@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/<account_id>/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

View File

@ -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);
}
};

View File

@ -83,6 +83,8 @@
</div>
{% block footer %}{% endblock %}
<script type="text/javascript" src="https://raw.githubusercontent.com/moment/moment/develop/min/moment.min.js"></script>
<!-- JQuery Javascript library -->
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>