def order_create(request): cart = SessionCart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() return render(request, 'orders/created.html', {'order': order}) else: form = OrderCreateForm() return render(request, 'orders/create.html', {'form': form})
def __init__(self, request): self.cart = SessionCart(request) print(self.cart) print(self.cart.cart) self.request = request self.user = self.get_user() self.address = self.check_delivery_address() self.order = self.create_order() self.order_item = self.add_items_to_order()
def post(self, request, *args, **kwargs): qty = request.POST.get('qty') product = self.get_object() print(qty) cart = SessionCart(request) cart.add(product_id=product.id, qty=qty) print(request.session.items()) messages.add_message(request, messages.INFO, "Товар успешно добавлен") return HttpResponseRedirect('/cart/')
def post(self, request, *args, **kwargs): # cart_product = CartProduct.objects.get( # user=self.cart.owner, cart=self.cart, content_type=content_type, object_id=product.id # ) # qty = int(request.POST.get('qty')) # cart_product.qty = qty # cart_product.save() # self.cart.save() product = self.get_object() cart = SessionCart(request) cart.update(product_id=product.id, qty=request.POST.get('qty')) messages.add_message(request, messages.INFO, "Кол-во успешно изменено") return HttpResponseRedirect('/cart/')
def get(self, request, slug, *args, **kwargs): # ct_model, product_slug = kwargs.get('ct_model'), kwargs.get('slug') # content_type = ContentType.objects.get(model=ct_model) # product = content_type.model_class().objects.get(slug=product_slug) # cart_product = CartProduct.objects.get( # user=self.cart.owner, cart=self.cart, content_type=content_type, object_id=product.id # ) # self.cart.products.remove(cart_product) # cart_product.delete() # self.cart.save() product = self.get_object() cart = SessionCart(request) cart.delete(product_id=product.id) messages.add_message(request, messages.INFO, "Товар успешно удален") return HttpResponseRedirect('/cart/')
def order_create(request): cart_session = SessionCart(request) order_create_form = OrderCreateForm(request.POST) if order_create_form.is_valid(): # The 'Order' part order_table_inst = order_create_form.save(commit=False) if cart_session.coupon: order_table_inst.coupon = cart_session.coupon # fk VS class attr order_table_inst.discount = cart_session.coupon.discount order_table_inst.save() # The 'OrderItem' part for cart_inst in cart_session: OrderItem.objects.create( order=order_table_inst, product=cart_inst["product"], price=cart_inst["price"], quantity=cart_inst["quantity"], ) cart_session.clear() task_order_created.delay(order_table_inst.id) return render( request=request, template_name="orders/order/created.html", context={"order_table_inst": order_table_inst}, ) else: order_create_form = OrderCreateForm() return render( request=request, template_name="orders/order/create.html", context={ "cart_session": cart_session, "order_create_form": order_create_form, }, )
def get_wishlist(request): session_wishlist = SessionCart(request, WISHLIST_SESSION_KEY) if request.user.is_authenticated(): try: wishlist = Wishlist.objects.get(user=request.user) except Wishlist.DoesNotExist: wishlist = Wishlist(user=request.user) # merge session wishlist, if it exists if session_wishlist.count(): if not wishlist.pk: wishlist.save() session_wishlist.save_to(wishlist) session_wishlist.clear() return wishlist else: return session_wishlist
def cart(request): return {'cart': SessionCart(request)}
def get(self, request, *args, **kwargs): cart = SessionCart(request) context = {'cart': cart} return render(request, 'cart.html', context)