diff --git a/accountant/api/views/entries.py b/accountant/api/views/entries.py index a8162be..b188e25 100644 --- a/accountant/api/views/entries.py +++ b/accountant/api/views/entries.py @@ -16,14 +16,13 @@ """ import dateutil.parser -from flask import json from flask.ext.restful import Resource, fields, reqparse, marshal_with_field from sqlalchemy.orm.exc import NoResultFound -from accountant import session_scope, session_aware +from accountant import session_aware -from .. import api, api_api +from .. import api_api from ..models.entries import Entry from ..models.operations import Operation @@ -31,38 +30,17 @@ from ..models.operations import Operation from ..fields import Object -@api.route("/entries///") -def get_entries(account_id, year, month): - """ - Return entries for an account, year, and month. - """ - with session_scope() as session: - query = Operation.get_for_account_and_month(session, account_id, year, - month) - - return json.dumps([{ - "id": i.id, - "pointed": i.pointed, - "operation_date": i.operation_date.strftime("%Y-%m-%d"), - "label": i.label, - "value": str(i.value), - "category": i.category, - "sold": str(i.sold) if not i.canceled else None, - "account_id": i.account_id, - "canceled": i.canceled, - "scheduled_operation_id": i.scheduled_operation_id - } for i in query.all()]) - - resource_fields = { - # 'id': fields.Integer, + 'id': fields.Integer(default=None), 'operation_date': fields.DateTime(dt_format='iso8601'), 'label': fields.String, 'value': fields.Fixed(decimals=2), 'pointed': fields.Boolean, 'category': fields.String, 'account_id': fields.Integer, - 'scheduled_operation_id': fields.Integer, + 'scheduled_operation_id': fields.Integer(default=None), + 'sold': fields.Fixed(decimals=2), + 'canceled': fields.Boolean, } parser = reqparse.RequestParser() @@ -77,7 +55,20 @@ 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=None): + kwargs = range_parser.parse_args() + + return Operation.get_for_account_and_range(session, **kwargs).all() + def put(self, *args): return self.post() diff --git a/accountant/frontend/static/js/entries.js b/accountant/frontend/static/js/entries.js index 7cc7223..13161ce 100644 --- a/accountant/frontend/static/js/entries.js +++ b/accountant/frontend/static/js/entries.js @@ -113,7 +113,16 @@ var EntryController = function($scope, $http, $rootScope, $filter) { $scope.savedItem = null; if(account && month) { - $http.get("/api/entries/" + account.id + "/" + month.year + "/" + month.month).success($scope.loadEntries_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/entries", + {params: { + account: account.id, + begin: begin.format('YYYY-MM-DD'), + end: end.format('YYYY-MM-DD') + }}).success($scope.loadEntries_success); } else { $scope.loadEntries_success(null); } @@ -367,14 +376,14 @@ var EntryController = function($scope, $http, $rootScope, $filter) { today.setHours(0); today.setMinutes(0); - // Find first and last days to set limits of the x axis. - var day = 24 * 60 * 60 * 1000; + // FIXME Alexis Lahouze 2015-06-12 Date format. - var firstDate = $filter('date')(new Date(Date.parse(entries[0][0]).valueOf() - day), 'yyyy-MM-dd'); - var lastDate = $filter('date')(new Date(Date.parse(entries[entries.length -1][0]).valueOf() + day), 'yyyy-MM-dd'); + // Find first and last days to set limits of the x axis. + var firstDate = moment(entries[0][0]).subtract(1, 'day').format(); + var lastDate = moment(entries[entries.length - 1][0]).add(1, 'day').format(); var chart = nv.models.lineChart().options({ - x: function(d) { return d3.time.format("%Y-%m-%d").parse(d[0]); }, + x: function(d) { return d3.time.format.utc(d[0]); }, y: function(d) { return new Number(d[1]); }, transitionDuration: 250, showXAxis: true, @@ -388,7 +397,7 @@ var EntryController = function($scope, $http, $rootScope, $filter) { chart.xAxis .axisLabel("Date") .tickFormat(function(d) { - return d3.time.format("%Y-%m-%d")(new Date(d)); + return d3.time.format.iso(new Date(d)); }); //chart.xAxis.scale().range([firstDate, lastDate]);