def redirect_to_form(request): service = SessionObjectService('order') order_data = service.get_or_create(request) if 'current_form' in order_data: return redirect(reverse_lazy('order:' + order_data['current_form'])) else: return redirect(reverse_lazy('order:personal_info'))
def add_item(request): if request.method == 'POST': post_data = json.loads(request.body) service = SessionObjectService('cart') cart = service.get_or_create(request) serializer = CartItemSerializer(data=post_data) if serializer.is_valid(): item = serializer.save() if item in cart: response = { 'text': 'Данный товар уже в корзине.', 'items_number': cart.get_items_number(), 'total_price': cart.get_total_price(), } return JsonResponse(response) cart.add_item(item) service.save(request, session_object=cart) response = { 'text': 'Товар добавлен в корзину.', 'items_number': cart.get_items_number(), 'total_price': cart.get_total_price(), } return JsonResponse(response) else: raise Http404 else: raise Http404()
def cart_items_number(request): service = SessionObjectService('cart') cart = service.get_or_create(request) return { 'cart_items_number': cart.get_items_number(), 'cart_total_price': cart.get_total_price() }
def favorites_items_number(request): service = SessionObjectService('favorites') favorites = service.get_or_create(request) return { 'favorites_items_number': favorites.get_items_number(), 'favorites': [item.product_id for item in favorites.items] }
def post(self, request, *args, **kwargs): form = QuickBuyForm(request.POST) if form.is_valid(): order = form.save() service = SessionObjectService('cart') session_cart = service.get_or_create(request) order.parse_session_cart(session_cart) return redirect(reverse_lazy('order:complete', args=(order.id, ))) else: return redirect('cart')
def get(self, request, *args, **kwargs): seo, _ = SitePageSeo.objects.get_or_create(page_name='Корзина') service = SessionObjectService('cart') cart = service.get_or_create(request) context = { 'seo': seo, 'cart': service.serialize(cart), 'quick_buy_form': QuickBuyForm() } return render(request, 'cart.html', context)
def post(self, request, *args, **kwargs): form = forms.OrderDeliveryForm(data=request.POST) if form.is_valid(): service = SessionObjectService('order') order = service.get_or_create(request) order.update(form.cleaned_data) service.save(request, order) order['current_form'] = 'payment' return redirect(reverse_lazy('order:payment')) else: context = self.get_context_data(request) context['form'] = form return render(request, 'checkout_delivery.html', context)
def remove_item(request): if request.method == 'POST': post_data = json.loads(request.body) service = SessionObjectService('cart') cart = service.get_or_create(request) cart.remove_item_by_product_id_and_size( post_data.get('id'), post_data.get('size'), ) service.save(request, cart) return JsonResponse({ 'text': 'Товар удален из корзины.', 'items_number': cart.get_items_number(), 'total_price': cart.get_total_price(), }) else: raise Http404()
def change_item_quantity(request): if request.method == 'POST': post_data = json.loads(request.body) service = SessionObjectService('cart') cart = service.get_or_create(request) changed_item = cart.change_item_quantity( post_data.get('id'), post_data.get('size'), int(post_data.get('quantity')), ) service.save(request, cart) context = { 'text': 'Количество изменено.', 'item_total': changed_item.get_total_price(), 'cart_total': cart.get_total_price(), } return JsonResponse(context) else: raise Http404()
def get(self, request, *args, **kwargs): service = SessionObjectService('favorites') favorites = service.get_or_create(request) seo, _ = SitePageSeo.objects.get_or_create(page_name=self.page_name) context = { 'seo': seo, 'filter': ProductFilter( request.GET, queryset=Product.displayed.filter( id__in=[item.product_id for item in favorites.items])), 'is_favorites': True, 'min_price': Product.objects.order_by('price').first().price, 'max_price': Product.objects.order_by('-price').first().price } return render(request, 'product_list.html', context)
def post(self, request, *args, **kwargs): form = forms.OrderForm(data=request.POST) if form.is_valid(): created_order = form.save() cart_service = SessionObjectService('cart') order_service = SessionObjectService('order') created_order.parse_session_cart(cart_service.get_or_create(request)) created_order.parse_session_order(order_service.get_or_create(request)) cart_service.new(request) if created_order.payment.payment_method_id == 1: return redirect(get_payment_url(created_order.id, created_order.get_total_price())) return redirect(reverse_lazy('order:complete', args=(created_order.id, ))) else: context = self.get_context_data(request) context['form'] = form return render(request, 'checkout.html', context)
def order_incomplete(request): if request.method == 'POST': cart_service = SessionObjectService('cart') order_service = SessionObjectService('order') cart = cart_service.get_or_create(request) order = order_service.get_or_create(request) message = OrderIncompleteMessage(cart, order) notify_admin( 'Незаполненная форма заказа.', message.render() ) return HttpResponse({}) else: raise Http404('Order incomplete')
def get_context_data(self, request): order_service = SessionObjectService('order') cart_service = SessionObjectService('cart') cart = cart_service.get_or_create(request) order = order_service.get_or_create(request) seo, _ = SitePageSeo.objects.get_or_create(page_name=self.page_name) delivery_method = DeliveryMethod.objects.get(id=order.get('delivery_method')) form = forms.OrderForm() context = { 'seo': seo, 'form': form, 'order': order, 'payment_method': PaymentMethod.objects.get(id=order.get('payment_method')), 'delivery_method': delivery_method, 'order_total_price': cart.get_total_price() + delivery_method.price, 'cart': cart_service.serialize(cart) } return context
def toggle_item(request): if request.method == 'POST': post_data = json.loads(request.body) service = SessionObjectService('favorites') favorites = service.get_or_create(request) product_id = int(post_data.get('id')) if product_id in favorites: item = favorites._get_item_by_product_id(product_id) favorites.remove_item(item) service.save(request, favorites) response = {'text': 'Товар удален из избранного', 'status': 201} else: item = FavoritesItem(product_id=post_data.get('id')) favorites.add_item(item) service.save(request, favorites) response = {'text': 'Товар добавлен в избранное', 'status': 200} return JsonResponse(response) else: raise Http404('Only post allowed')
def get_context_data(self, request): service = SessionObjectService('order') cart_service = SessionObjectService('cart') cart = cart_service.get_or_create(request) order_data = service.get_or_create(request) initial = { 'first_name': order_data.get('first_name', None), 'second_name': order_data.get('second_name', None), 'email': order_data.get('email', None), 'phone_number': order_data.get('phone_number', None), } seo, _ = SitePageSeo.objects.get_or_create(page_name=self.page_name) form = forms.OrderPersonalInformationForm(initial=initial, ) context = { 'seo': seo, 'form': form, 'order': order_data, 'cart': cart_service.serialize(cart) } return context
def get_context_data(self, request): service = SessionObjectService('order') order_data = service.get_or_create(request) cart_service = SessionObjectService('cart') cart = cart_service.get_or_create(request) initial = { 'delivery_method': order_data.get('delivery_method', None), 'index': order_data.get('index', None), 'city': order_data.get('city', None), 'street': order_data.get('street', None), 'building_number': order_data.get('building_number', None), 'apartment': order_data.get('apartment', None), 'order': order_data, } seo, _ = SitePageSeo.objects.get_or_create(page_name=self.page_name) form = forms.OrderDeliveryForm(initial=initial) context = { 'seo': seo, 'form': form, 'cart': cart_service.serialize(cart) } return context