From 53acd4d30ecd5276555aa468124ec6ac6c0ac9a0 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 20 Dec 2016 12:41:54 +0100 Subject: [PATCH 1/3] Add tests that reveal end-of-period date problem See #322 --- .../lib/python/cartodb_services/test/test_user_service.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/lib/python/cartodb_services/test/test_user_service.py b/server/lib/python/cartodb_services/test/test_user_service.py index 2905f8b..cd4b880 100644 --- a/server/lib/python/cartodb_services/test/test_user_service.py +++ b/server/lib/python/cartodb_services/test/test_user_service.py @@ -23,6 +23,10 @@ class TestUserService(TestCase): amount=400) assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 400 + def test_user_quota_for_a_month_shorter_than_end_day(self): + us = self.__build_user_service('test_user', end_date=date(2016,1,31)) + assert us.used_quota(self.NOKIA_GEOCODER, date(2016,2,10)) == 0 + def test_org_used_quota_for_a_day(self): us = self.__build_user_service('test_user', orgname='test_org') increment_service_uses(self.redis_conn, 'test_user', @@ -30,6 +34,10 @@ class TestUserService(TestCase): amount=400) assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 400 + def test_org_quota_quota_for_a_month_shorter_than_end_day(self): + us = self.__build_user_service('test_user', orgname='test_org', end_date=date(2016,1,31)) + assert us.used_quota(self.NOKIA_GEOCODER, date(2016,2,10)) == 0 + def test_user_not_amount_in_used_quota_for_month_should_be_0(self): us = self.__build_user_service('test_user') assert us.used_quota(self.NOKIA_GEOCODER, date.today()) == 0 From b5514aea6033225f73fc15015326f184aa7c29a1 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 20 Dec 2016 12:48:36 +0100 Subject: [PATCH 2/3] Avoid invalid end of period dates Fixes #322 --- .../cartodb_services/metrics/user.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/metrics/user.py b/server/lib/python/cartodb_services/cartodb_services/metrics/user.py index 4a65014..0411ee4 100644 --- a/server/lib/python/cartodb_services/cartodb_services/metrics/user.py +++ b/server/lib/python/cartodb_services/cartodb_services/metrics/user.py @@ -1,6 +1,15 @@ from datetime import date, timedelta from dateutil.relativedelta import relativedelta +from calendar import monthrange +def last_day_of_month(year, month): + """last valid day of a month""" + return monthrange(year, month)[1] + +def latest_valid_date(year, month, day): + """latest date not later than the day specified""" + valid_day = min(day, last_day_of_month(year, month)) + return date(year, month, valid_day) class UserMetricsService: """ Class to manage all the user info """ @@ -143,9 +152,9 @@ class UserMetricsService: today = date.today() if end_period_day > today.day: temp_date = today + relativedelta(months=-1) - date_from = date(temp_date.year, temp_date.month, end_period_day) + date_from = latest_valid_date(temp_date.year, temp_date.month, end_period_day) else: - date_from = date(today.year, today.month, end_period_day) + date_from = latest_valid_date(today.year, today.month, end_period_day) return date_from, today From 32010669e88091df65a871d2d50c0e7b5024505f Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 20 Dec 2016 16:39:19 +0100 Subject: [PATCH 3/3] New version of the python lib To fix bug with period end date; see #322 --- server/lib/python/cartodb_services/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/lib/python/cartodb_services/setup.py b/server/lib/python/cartodb_services/setup.py index 38f6f6a..2e9aa98 100644 --- a/server/lib/python/cartodb_services/setup.py +++ b/server/lib/python/cartodb_services/setup.py @@ -10,7 +10,7 @@ from setuptools import setup, find_packages setup( name='cartodb_services', - version='0.12.3', + version='0.12.4', description='CartoDB Services API Python Library',