diff --git a/combo/apps/lingo/views.py b/combo/apps/lingo/views.py index 52a73d53..67282f69 100644 --- a/combo/apps/lingo/views.py +++ b/combo/apps/lingo/views.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -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'):