compute item amount from payload and its optional extra attribute (#9174)
This commit is contained in:
parent
dd47d33317
commit
550d0eae32
21
README
21
README
|
@ -19,3 +19,24 @@ details.
|
|||
|
||||
You should have received a copy of the GNU Affero General Public License along
|
||||
with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
Items amount can be added to basket through API by posting to
|
||||
*/api/lingo/add-basket-item* endpoint a json payload containing *amount* and/or
|
||||
*"extra": {"amount": ...}* attribute.
|
||||
|
||||
For example:
|
||||
|
||||
{"display_name": "<item display name>",
|
||||
"url": "http://<item url>",
|
||||
"amount": "42.42",
|
||||
"extra": {"amount": "10.42", ...},
|
||||
...
|
||||
}
|
||||
|
||||
The "amount" attribute should be float or decimal or a list of
|
||||
floats/decimals. For example:
|
||||
|
||||
{"amount": [14.12], "extra": {"amount": ["10.42", "5", "10"], ... }, ...}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
import json
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
@ -55,13 +55,26 @@ class AddBasketItemApiView(View):
|
|||
def dispatch(self, *args, **kwargs):
|
||||
return super(AddBasketItemApiView, self).dispatch(*args, **kwargs)
|
||||
|
||||
def get_amount(self, amount):
|
||||
if isinstance(amount, list):
|
||||
d = sum([Decimal(a) for a in amount])
|
||||
else:
|
||||
d = Decimal(amount)
|
||||
return d.quantize(Decimal('0.01'), ROUND_HALF_UP)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# XXX: check request signature
|
||||
|
||||
request_body = json.loads(self.request.body)
|
||||
extra = request_body.get('extra', {})
|
||||
|
||||
item = BasketItem()
|
||||
item.amount = sum([Decimal(x) for x in request.GET.getlist('amount')])
|
||||
item = BasketItem(amount=0)
|
||||
|
||||
if request_body.get('amount'):
|
||||
item.amount += self.get_amount(request_body['amount'])
|
||||
|
||||
if extra.get('amount'):
|
||||
item.amount += self.get_amount(extra['amount'])
|
||||
|
||||
try:
|
||||
if request.GET.get('NameId'):
|
||||
|
|
|
@ -2,6 +2,7 @@ import pytest
|
|||
from datetime import datetime, timedelta
|
||||
import urlparse
|
||||
from decimal import Decimal
|
||||
import json
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.urlresolvers import reverse
|
||||
|
@ -80,3 +81,28 @@ def test_successfull_items_payment(regie, user):
|
|||
# simulate backend callback call
|
||||
resp = client.get(qs['return_url'][0], args)
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_add_amount_to_basket(regie, user):
|
||||
user_email = 'foo@example.com'
|
||||
User.objects.get_or_create(email=user_email)
|
||||
amount = 42
|
||||
data = {'amount': amount, 'display_name': 'test amount',
|
||||
'url': 'http://example.com'}
|
||||
url = '%s?email=%s' % (reverse('api-add-basket-item'), user_email)
|
||||
resp = client.post(url, json.dumps(data), content_type='application/json')
|
||||
assert resp.status_code == 200
|
||||
assert json.loads(resp.content) == {'result': 'success'}
|
||||
assert BasketItem.objects.filter(amount=amount).exists()
|
||||
|
||||
data['extra'] = {'amount': '22.22'}
|
||||
resp = client.post(url, json.dumps(data), content_type='application/json')
|
||||
assert resp.status_code == 200
|
||||
assert json.loads(resp.content) == {'result': 'success'}
|
||||
assert BasketItem.objects.filter(amount=Decimal('64.22')).exists()
|
||||
|
||||
data['amount'] = [amount]
|
||||
data['extra'] = {'amount': ['22.22', '12']}
|
||||
resp = client.post(url, json.dumps(data), content_type='application/json')
|
||||
assert resp.status_code == 200
|
||||
assert json.loads(resp.content) == {'result': 'success'}
|
||||
assert BasketItem.objects.filter(amount=Decimal('76.22')).exists()
|
||||
|
|
Reference in New Issue