Skip to content

Commit 0ffd99f

Browse files
committed
Replaced magic time constants with understandable constant names
Previous calculation of "seconds per year" assumed 365 days á 24 * 60 * 60 seconds, while we actually want 12 months á 31 days á 24 * 60 * 60 seconds
1 parent 8306163 commit 0ffd99f

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 5.2.0 (under development)
44

5-
* ...
5+
* Added constants outlining expiration and duration time bases for purchases
66

77
## 5.1.0
88

laterpay/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import six
2020
from six.moves.urllib.parse import quote_plus
2121

22-
from . import signing, utils
22+
from . import constants, signing, utils
2323

2424

2525
_logger = logging.getLogger(__name__)
@@ -89,12 +89,17 @@ def __init__(self, item_id, pricing, url, title, expiry=None, sub_id=None, perio
8989
"uppercase ASCII characters, digits, underscore and hyphen, the length of "
9090
"which is between 1 and 128 characters." % sub_id
9191
)
92-
if isinstance(period, int) and 3600 <= period <= 31536000:
92+
if (
93+
isinstance(period, int) and
94+
constants.EXPIRY_SECONDS_FOR_HOUR <= period <= constants.EXPIRY_SECONDS_FOR_YEAR
95+
):
9396
self.data['period'] = period
9497
else:
9598
raise InvalidItemDefinition(
9699
"Period not set or invalid value '%s' for period. The subscription period "
97-
"must be an int in the range [3600, 31536000] (including)." % period
100+
"must be an int in the range [%d, %d] (including)." % (
101+
period, constants.EXPIRY_SECONDS_FOR_HOUR, constants.EXPIRY_SECONDS_FOR_YEAR,
102+
)
98103
)
99104

100105

laterpay/constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
EXPIRY_SECONDS_FOR_HOUR = 60 * 60
2+
EXPIRY_SECONDS_FOR_DAY = 60 * 60 * 24
3+
EXPIRY_SECONDS_FOR_WEEK = 60 * 60 * 24 * 7
4+
EXPIRY_SECONDS_FOR_MONTH = 60 * 60 * 24 * 31
5+
EXPIRY_SECONDS_FOR_YEAR = 60 * 60 * 24 * 31 * 12
6+
7+
EXPIRY_SECONDS_FOR_TIME_UNITS = {
8+
'h': EXPIRY_SECONDS_FOR_HOUR,
9+
'd': EXPIRY_SECONDS_FOR_DAY,
10+
'w': EXPIRY_SECONDS_FOR_WEEK,
11+
'm': EXPIRY_SECONDS_FOR_MONTH,
12+
'y': EXPIRY_SECONDS_FOR_YEAR,
13+
}

tests/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def test_invalid_sub_id(self):
3737

3838
def test_invalid_period(self):
3939
with self.assertRaisesRegexp(InvalidItemDefinition, r'Period not set or invalid value'):
40-
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=3599)
40+
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=60 * 60 - 1)
4141
with self.assertRaisesRegexp(InvalidItemDefinition, r'Period not set or invalid value'):
42-
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=31536001)
42+
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=60 * 60 * 24 * 31 * 12 + 1)
4343
with self.assertRaisesRegexp(InvalidItemDefinition, r'Period not set or invalid value'):
4444
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period='12345')
4545

@@ -55,8 +55,8 @@ def test_item_definition(self):
5555

5656
def test_sub_id(self):
5757
# Test sub_id_bounds
58-
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=3600)
59-
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a' * 128, period=31536000)
58+
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a', period=60 * 60)
59+
ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='a' * 128, period=60 * 60 * 24 * 31 * 12)
6060

6161
it = ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', sub_id='abc', period=12345)
6262
self.assertEqual(it.data, {

0 commit comments

Comments
 (0)