Exemple #1
0
 def post(self, request, cart_id):
     data = request.data
     line_items = data.get('line_items', None)
     token = data.get('token', None)
     jwt_decode = jwt.decode(token, "SECRET_KEY", algorithms='HS256')
     signature = token.split('.')[2]
     user_id = jwt_decode['user_id']
     try:
         user = Users.objects.get(id=user_id)
     except Users.DoesNotExist:
         return Response(RESPONSE.FAILURE_NOT_EXIST)
     if signature == user.token and user and line_items:
         cart = Carts.objects.get(id=cart_id)
         for item in line_items:
             bc_cart = {
                 'line_items': [item],
                 'gift_certificates': data.get('gift_certificates')
             }
             resp = carts.Carts.add_cart_items(carts.Carts(), cart.bc_cart_id, bc_cart)
             product = Products.objects.get(bc_product_id=item.get('product_id'))
             param = {
                 'cart_id': cart,
                 'product_id': product,
                 'bc_item_id': resp.get('data').get('line_items').get('physical_items')[0].get('id'),
                 'quantity': item.get('quantity')
             }
             cart_product = CartProduct.objects.create(**param)
         if resp and cart_product:
             return Response({
                 'code': 200,
                 'message': 'success',
                 'bc_cart_id': cart.bc_cart_id
             })
Exemple #2
0
 def delete(self, request, cart_id, item_id):
     data = request.data
     token = data.get('token', None)
     jwt_decode = jwt.decode(token, "SECRET_KEY", algorithms='HS256')
     signature = token.split('.')[2]
     user_id = jwt_decode['user_id']
     try:
         user = Users.objects.get(id=user_id)
     except Users.DoesNotExist:
         return Response(RESPONSE.FAILURE_NOT_EXIST)
     if signature == user.token and user:
         cart_product = CartProduct.objects.get(cart_id=cart_id,
                                                bc_item_id=item_id)
         product = Products.objects.get(
             id=cart_product.product_id.__str__().get('id'))
         product.quantity += cart_product.quantity
         product.save()
         cart_product.delete()
         cart = Carts.objects.get(id=cart_id)
         resp = carts.Carts.delete_cart_items(carts.Carts(),
                                              cart.bc_cart_id, item_id)
         if resp is not False and cart_product:
             return Response({
                 'code': 200,
                 'message': 'success',
                 'data': {}
             })
     return Response(RESPONSE.FAILURE_BAD)
Exemple #3
0
 def put(self, request, cart_id, item_id):
     data = request.data
     line_item = data.get('line_item', None)
     token = data.get('token', None)
     jwt_decode = jwt.decode(token, "SECRET_KEY", algorithms='HS256')
     signature = token.split('.')[2]
     user_id = jwt_decode['user_id']
     try:
         user = Users.objects.get(id=user_id)
     except Users.DoesNotExist:
         return Response(RESPONSE.FAILURE_NOT_EXIST)
     try:
         if signature == user.token and user and line_item:
             cart = Carts.objects.get(pk=cart_id)
             product = Products.objects.get(
                 bc_product_id=line_item.get('product_id', None))
             try:
                 cart_product = CartProduct.objects.get(cart_id=cart_id,
                                                        bc_item_id=item_id)
             except CartProduct.DoesNotExist:
                 cart_product = None
             if cart_product:
                 product.quantity += cart_product.quantity
                 cart_product.quantity = line_item.get('quantity')
                 product.quantity -= line_item.get('quantity')
                 cart_product.save()
                 product.save()
             else:
                 return Response(RESPONSE.FAILURE_BAD)
             bc_param = {
                 "line_item": {
                     "quantity": line_item.get('quantity'),
                     "product_id": line_item.get('product_id'),
                 }
             }
             carts.Carts.put_cart_items(carts.Carts(),
                                        bc_cart_id=cart.bc_cart_id,
                                        item_id=item_id,
                                        data=bc_param)
             success = update_dict(dict(cart_id=cart.id))
             return Response(success)
     except Exception:
         return Response(RESPONSE.FAILURE_BAD)
Exemple #4
0
 def post(self, request):
     data = request.data
     token = data.get('token', None)
     jwt_decode = jwt.decode(token, 'SECRET_KEY', algorithms='HS256')
     signature = token.split('.')[2]
     user_id = jwt_decode['user_id']
     product_list = data.get('list', None)
     try:
         user = Users.objects.get(id=user_id)
     except Users.DoesNotExist:
         return Response(RESPONSE.FAILURE_NOT_EXIST)
     if signature == user.token and user and product_list:
         bc_param = {"line_items": product_list}
         resp = carts.Carts.post_cart(carts.Carts(), bc_param)
         param = {
             'user_id': user,
             'status': '1',
         }
         cart = Carts.objects.create(**param)
         for item in product_list:
             product = Products.objects.get(
                 bc_product_id=item.get('product_id', None))
             item_list = resp.get('data').get('line_items').get(
                 'physical_items')
             for physical_item in item_list:
                 _cart_product = {
                     'cart_id': cart,
                     'product_id': product,
                     'bc_item_id': physical_item.get('id'),
                     'quantity': item.get('quantity', None),
                 }
                 product.quantity -= item.get('quantity')
                 product.save()
                 CartProduct.objects.create(**_cart_product)
             cart.bc_cart_id = resp.get('data').get('id')
             cart.save()
         success = update_dict(
             dict(cart_id=cart.id, bc_cart_id=resp.get('data').get('id')))
         return Response(success)
     return Response(status.HTTP_400_BAD_REQUEST)