lingo: compute item amount from payload and its optional extra attribute (#9174)

This commit is contained in:
Serghei Mihai 2015-12-02 11:07:49 +01:00 committed by Frédéric Péters
parent 5bbd0193cf
commit a33f6d4200
1 changed files with 16 additions and 3 deletions

View File

@ -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'):