def add_favours_to_cart(request): favours = FavourItem.objects.filter(cart=get_cart(request)) for favour in favours: cart_item, _ = CartItem.objects.get_or_create( cart=get_cart(request), item__id=favour.item.id, ordered=False, ) if _: cart_item.quantity = 1 if not _: cart_item.quantity += 1 cart_item.save() favour.delete() return Response(status=200)
def cart_item(request, id): cart = get_cart(request) if request.method == 'GET': cart_item = CartItem.objects.get(id=id) return Response(data=CartItemSerializer(cart_item).data, status=200) elif request.method == 'PATCH': cart_item = cart.change_cart_item_amount(id, request.data['quantity']) response = { "cart_item_total_price":cart_item.total_price, } response.update(get_cart_info(request)) return Response(data=response, status=202) elif request.method == 'DELETE': get_cart(request).remove_cart_item(id) return Response(get_cart_info(request), status=204)
def order_request(request): # query = request.POST or request.GET query = request.data name = query.get('name', '---') email = query.get('email', '---') phone = query.get('phone', '---') address = query.get('address', '---') comments = query.get('comments', '---') attributes = json.loads(query.get('attributes')) item_id = query['product_id'] payment = _('Покупка в 1 клік') delivery = _('Покупка в 1 клік') print(query) cart = get_cart(request) cart.add_item(item_id=item_id, quantity=1, attributes=attributes) order = Order.objects.create( name=name, email=email, phone=phone, address=address, delivery_opt=delivery, payment_opt=payment, cart=cart, ) order.make_order(request) return JsonResponse({ 'status': 'OK', 'url': reverse('thank_you'), })
def get_items_in_cart(request, items): items_in_cart = [] for item in items: cart = get_cart(request) if cart.items.filter(item=item).exists(): items_in_cart.append(item.id) return items_in_cart
def get_order_liqpay_context(request, params={}): cart = get_cart(request) order = Order.objects.get( cart=cart, ordered=False, ) amount = 0 for cart_item in CartItem.objects.filter(cart=cart): amount += cart_item.total_price def_params = { 'amount': amount, 'description': str(order.comments), 'order_id': str(order.id), 'action': 'pay', 'currency': 'UAH', 'version': '3', 'sandbox': int(LiqpayConfig.get_solo().sandbox_mode), 'server_url': f'/sw_order/liqpay_callback/', # 'server_url': f'{Site.objects.get_current()}pay_callback/', } def_params.update(params) signature, data = get_liqpay_context(def_params) context = { 'signature': signature, 'data': data, 'callback_url': '/sw_order/liqpay_callback/', } return context
def order_items(request): query = request.POST or request.GET # import pdb; pdb.set_trace() name = query.get('name', "---") email = query.get('email', "---") phone = query.get('phone', "---") address = query.get('address', "---") comments = query.get('comments', "---") payment_opt = query.get('payment_opt', "---") delivery_opt = query.get('delivery_opt', "---") order = Order.objects.create( name=name, email=email, phone=phone, address=address, comments=comments, payment_opt=payment_opt, delivery_opt=delivery_opt, ) cart = get_cart(request) cart.order = order cart.save() if payment_opt == 'liqpay': order.payment_opt = _("З передоплатою") order.save() url = reverse('payment') return JsonResponse({"url": url}) else: order.payment_opt = _("Без предоплати") order.make_order(request) url = reverse('thank_you') return JsonResponse({"url": url})
def handle_user(self, request): cart = get_cart(request) if request.user.is_authenticated: self.user = request.user self.save() cart.user = request.user cart.save()
def get_items_in_favours(request, items): items_in_favours = [] for item in items: cart = get_cart(request) # if cart.favour_items.all().exists(): if cart.favour_items.filter(item=item).exists(): items_in_favours.append(item.id) return items_in_favours
def favour_item(request, id): cart = get_cart(request) if request.method == 'GET': favour_item = FavourItem.objects.get(id=id) response = FavourItemSerializer(favour_item).data return Response(response, status=200) elif request.method == 'DELETE': FavourItem.objects.get(id=id).delete() return Response(status=204)
def add_favour_to_cart(request, id): favour_item = FavourItem.objects.get(id=id) cart_item, _ = CartItem.objects.get_or_create( cart=get_cart(request), item__id=favour_item.item.id, ordered=False, ) if _: cart_item.quantity = 1 if not _: cart_item.quantity += 1 cart_item.save() favour_item.delete() return Response(status=202)
def cart_items(request): cart = get_cart(request) if request.method == 'GET': return Response(data=get_cart_info(request),status=200) if request.method == 'POST': query = request.data quantity = query.get('quantity', 1) item_id = query['item_id'] attributes = query.get('attributes', []) attributes = json.loads(attributes) cart.add_item(item_id, quantity, attributes) return Response(data=get_cart_info(request), status=203) if request.method == 'DELETE': cart.clear() return Response(data=get_cart_info(request), status=204)
def favour_items(request): cart = get_cart(request) if request.method == 'GET': favours = FavourItem.objects.filter(cart=cart) response = FavourItemSerializer(favours, many=True).data return Response(response, status=200) if request.method == 'POST': item_id = request.data['item_id'] favour, _ = FavourItem.objects.get_or_create( cart=cart, item=Item.objects.get(id=item_id) ) print(favour.item) return Response(status=202) if request.method == 'DELETE': FavourItem.objects.filter(cart=cart).delete() return Response(status=204)
def make_order(self, request): cart = get_cart(request) self.handle_user(request) self.handle_amount(request) self.total_price = self.cart.total_price self.ordered = True self.save() cart.order = self cart.ordered = True cart.save() cart_items = CartItem.objects.filter(cart=cart) context = {'cart_items': cart_items} box_send_mail( subject=f'Отримано замовлення товарів # {self.id}', template='sw_order/mail.html', email_config=OrderRecipientEmail, model=self, fail_silently=False, context=context, )
def context(request): if not request.session.get('current_currency_code'): request.session['current_currency_code'] = 'UAH' current_currency_code = request.session['current_currency_code'] try: current_currency = Currency.objects.get(code=current_currency_code) main_currency = Currency.objects.get(is_main=True) velo = ItemCategory.objects.get(code='velo') ramy = ItemCategory.objects.get(code='ramy') comp = ItemCategory.objects.get(code='comp') amor = ItemCategory.objects.get(code='amor') vilk = ItemCategory.objects.get(code='vilk') galm = ItemCategory.objects.get(code='galm') moto = ItemCategory.objects.get(code='moto') kole = ItemCategory.objects.get(code='kole') cart = get_cart(request) cart_items = CartItem.objects.filter(cart=cart) currencies = Currency.objects.all() except Exception as e: # raise e pass return locals()
def handle_amount(self, request): order = self cart = get_cart(request) # unavailable_stocks = ItemStock.objects.filter(availability=False) unavailable_stock = ItemStock.objects.filter( availability=False).first() for cart_item in CartItem.objects.filter(cart=cart): cart_item.ordered = True cart_item.order = order item = cart_item.item if item.amount != None: if item.amount < cart_item.quantity: cart_item.quantity = item.amount item.amount = 0 # if unavailable_stocks.exists(): # item.in_stock = unavailable_stocks.first() # else: # item.in_stock = None item.in_stock = unavailable_stock else: item.amount -= cart_item.quantity item.save() cart_item.save()
def is_in_cart(self, request): from box.apps.sw_shop.sw_cart.utils import get_cart return self.id in get_cart(request).items.all().values_list('item__id', flat=True)
def remove_favour_by_like(request, id): FavourItem.objects.get(cart=get_cart(request), item__id=id).delete() return Response(status=204)
def cart_item_count(request): cart = get_cart(request) return cart.items.all().count()
def change_item_amount(request, id): get_cart(request).change_item_amount(id, request.data['quantity']) return Response(get_cart_info(request), status=203)
def get_favours_amount(request): favours = FavourItem.objects.filter(cart=get_cart(request)) return HttpResponse(favours.count())