Skip to content

Commit 7091bfc

Browse files
committed
Merge branch 'develop' of github.com:laterpay/laterpay-client-python into develop
2 parents d0d325c + 4176fb3 commit 7091bfc

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

laterpay/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class ItemDefinition(object):
6666
For Single item purchases: http://docs.laterpay.net/platform/dialogs/buy/
6767
"""
6868

69-
def __init__(self, item_id, pricing, url, title, expiry=None, sub_id=None, period=None, item_type=None):
69+
def __init__(self, item_id, pricing, url, title, expiry=None, sub_id=None,
70+
period=None, item_type=None, election_id=None):
7071
for price in pricing.split(','):
7172
if not _PRICING_RE.match(price):
7273
raise InvalidItemDefinition('Pricing is not valid: %s' % pricing)
@@ -82,9 +83,16 @@ def __init__(self, item_id, pricing, url, title, expiry=None, sub_id=None, perio
8283
'expiry': expiry,
8384
}
8485

85-
if item_type in {constants.ITEM_TYPE_CONTRIBUTION, constants.ITEM_TYPE_DONATION}:
86+
if item_type in {
87+
constants.ITEM_TYPE_CONTRIBUTION,
88+
constants.ITEM_TYPE_DONATION,
89+
constants.ITEM_TYPE_POLITICAL_CONTRIBUTION,
90+
}:
8691
self.data['campaign_id'] = item_id
8792
self.item_type = item_type
93+
94+
if item_type == constants.ITEM_TYPE_POLITICAL_CONTRIBUTION:
95+
self.data['election_id'] = election_id
8896
else:
8997
self.data['article_id'] = item_id
9098
self.item_type = None
@@ -320,6 +328,8 @@ def get_buy_url(self, item_definition, *args, **kwargs):
320328
page_type = 'contribute/pay_now'
321329
elif item_type == constants.ITEM_TYPE_DONATION:
322330
page_type = 'donate/pay_now'
331+
elif item_type == constants.ITEM_TYPE_POLITICAL_CONTRIBUTION:
332+
page_type = 'political_contribution/pay_now'
323333
else:
324334
page_type = 'buy'
325335

@@ -339,6 +349,8 @@ def get_add_url(self, item_definition, *args, **kwargs):
339349
page_type = 'contribute/pay_later'
340350
elif item_type == constants.ITEM_TYPE_DONATION:
341351
page_type = 'donate/pay_later'
352+
elif item_type == constants.ITEM_TYPE_POLITICAL_CONTRIBUTION:
353+
page_type = 'political_contribution/pay_later'
342354
else:
343355
page_type = 'add'
344356

laterpay/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414

1515
ITEM_TYPE_CONTRIBUTION = 'contribution'
1616
ITEM_TYPE_DONATION = 'donation'
17+
ITEM_TYPE_POLITICAL_CONTRIBUTION = 'political'

tests/test_client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ def test_sub_id(self):
7272
'url': 'http://example.com/t',
7373
})
7474

75+
def test_election_id_for_non_poltical_contribution(self):
76+
it = ItemDefinition(
77+
1, 'EUR20', 'http://example.com/t', 'title', item_type=constants.ITEM_TYPE_CONTRIBUTION,
78+
election_id='123-election',
79+
)
80+
self.assertEqual(it.data, {
81+
'campaign_id': 1,
82+
'expiry': None,
83+
'pricing': 'EUR20',
84+
'title': 'title',
85+
'url': 'http://example.com/t',
86+
})
87+
7588
def test_item_type_contribution(self):
7689
it = ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', item_type=constants.ITEM_TYPE_CONTRIBUTION)
7790
self.assertEqual(it.data, {
@@ -94,6 +107,21 @@ def test_item_type_donation(self):
94107
})
95108
self.assertEqual(it.item_type, 'donation')
96109

110+
def test_item_type_political_contribution(self):
111+
it = ItemDefinition(
112+
1, 'EUR20', 'http://example.com/t', 'title', item_type=constants.ITEM_TYPE_POLITICAL_CONTRIBUTION,
113+
election_id='123-election',
114+
)
115+
self.assertEqual(it.data, {
116+
'campaign_id': 1,
117+
'expiry': None,
118+
'pricing': 'EUR20',
119+
'title': 'title',
120+
'url': 'http://example.com/t',
121+
'election_id': '123-election',
122+
})
123+
self.assertEqual(it.item_type, 'political')
124+
97125
def test_item_type_unknown(self):
98126
it = ItemDefinition(1, 'EUR20', 'http://example.com/t', 'title', item_type='whatever')
99127
self.assertEqual(it.data, {
@@ -285,6 +313,19 @@ def test_get_add_url_donation(self):
285313
self.assertQueryString(url, 'url', 'http://example.net/t')
286314
self.assertQueryString(url, 'title', 'Save the World!')
287315

316+
def test_get_add_url_political_contribution(self):
317+
item = ItemDefinition(
318+
'2', 'EUR20', 'http://example.net/t', 'Vote for Sue!',
319+
item_type=constants.ITEM_TYPE_POLITICAL_CONTRIBUTION, election_id='123-election',
320+
)
321+
url = self.lp.get_add_url(item, item_type='political')
322+
self.assertTrue(url.startswith('https://web.laterpay.net/dialog/political_contribution/pay_later?'))
323+
self.assertQueryString(url, 'campaign_id', '2')
324+
self.assertQueryString(url, 'pricing', 'EUR20')
325+
self.assertQueryString(url, 'url', 'http://example.net/t')
326+
self.assertQueryString(url, 'title', 'Vote for Sue!')
327+
self.assertQueryString(url, 'election_id', '123-election')
328+
288329
def test_get_buy_url(self):
289330
item = ItemDefinition(1, 'EUR20', 'http://example.net/t', 'title')
290331
url = self.lp.get_buy_url(
@@ -336,6 +377,19 @@ def test_get_buy_url_donation(self):
336377
self.assertQueryString(url, 'url', 'http://example.net/t')
337378
self.assertQueryString(url, 'title', 'Save the World!')
338379

380+
def test_get_buy_url_political_contribution(self):
381+
item = ItemDefinition(
382+
'vote-for-sue', 'EUR20', 'http://example.net/t', 'Vote for Sue!',
383+
item_type=constants.ITEM_TYPE_POLITICAL_CONTRIBUTION, election_id='123-election',
384+
)
385+
url = self.lp.get_buy_url(item)
386+
self.assertTrue(url.startswith('https://web.laterpay.net/dialog/political_contribution/pay_now?'))
387+
self.assertQueryString(url, 'campaign_id', 'vote-for-sue')
388+
self.assertQueryString(url, 'pricing', 'EUR20')
389+
self.assertQueryString(url, 'url', 'http://example.net/t')
390+
self.assertQueryString(url, 'title', 'Vote for Sue!')
391+
self.assertQueryString(url, 'election_id', '123-election')
392+
339393
def test_get_subscribe_url(self):
340394
item = ItemDefinition(1, 'EUR20', 'http://example.net/t', 'title', sub_id='a0_-9Z', period=12345)
341395
url = self.lp.get_subscribe_url(

0 commit comments

Comments
 (0)