From 8062cb4c59a2361daa66572bd9f7488f0a36f549 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 26 Jan 2013 12:47:04 +0100 Subject: [PATCH] Now select the current month if exists. --- src/api/controller/accounts.py | 6 +++--- src/html/js/entries.js | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/api/controller/accounts.py b/src/api/controller/accounts.py index 411483d..a8aa82f 100644 --- a/src/api/controller/accounts.py +++ b/src/api/controller/accounts.py @@ -39,13 +39,13 @@ def get_months(account_id): session = db.session query = session.query( - distinct(extract("year", Entry.operation_date)).label("year"), - extract("month", Entry.operation_date).label("month") + distinct(cast(extract("year", Entry.operation_date), db.String)).label("year"), + cast(extract("month", Entry.operation_date), db.String).label("month") ).filter(Entry.account_id == account_id).order_by("year", "month") return json.dumps([{ "year": i.year, - "month": i.month + "month": i.month.rjust(2, '0') } for i in query.all()]) @app.route("/api/accounts", methods=["PUT"]) diff --git a/src/html/js/entries.js b/src/html/js/entries.js index 6fd7ad2..05044b7 100644 --- a/src/html/js/entries.js +++ b/src/html/js/entries.js @@ -285,15 +285,23 @@ var ListViewModel = function() { var monthToSelect = null; - // Reset selected month to the new instance corresponding to the old one - if(self.month()) { - // Find the new instance of the previously selected month. - $.each(self.months(), function(index, month) { + var today = new Date(); + var currentYear = today.getFullYear().toString(); + var currentMonth = (today.getMonth() + 1 < 10 ? "0" : "") + (today.getMonth() + 1); + + // Find the new instance of the previously selected month. + $.each(self.months(), function(index, month) { + // Reset selected month to the new instance corresponding to the old one + if(self.month()) { if(month.year() === self.month().year() && month.month() === self.month().month()) { monthToSelect = month; } - }); - } + } else { + if(month.year() === currentYear && month.month() === currentMonth) { + monthToSelect = month; + } + } + }); // Set selected month to the last one if not yet selected. if(!monthToSelect && self.months().length > 0) {