def form_valid(self, form): data = {} product = models.Product.objects \ .enabled() \ .available() \ .get(pk=form.cleaned_data['product_pk']) if product: cart = Cart(self.request.session, shop_settings.CARD_CART_SESSION_KEY) cart.add(product=product, price=product.pg_selling_price, quantity=form.cleaned_data['quantity']) data = cart.items_json return JsonResponse(data)
class OrderAgainView(LoginRequiredMixin, StoreContextMixin, HostRestrict, generic.FormView): logger = logging.getLogger(__name__) sub_domain = 'card' form_class = forms.DummyForm def form_valid(self, form): queryset = models.Order.objects \ .valid(self.request.user) \ .select_related('parent') \ .prefetch_related('products', 'products__codes') order = get_object_or_404(queryset, order_no=self.kwargs['uuid']) self.cart = Cart(self.request.session, shop_settings.CART_SESSION_KEY) self.cart.clear() order_product_dict = { op.code: op.quantity for op in order.products.all() } products = models.Product.objects \ .enabled() \ .available() \ .filter(code__in=order_product_dict.keys()) for product in products: self.cart.add(product=product, price=product.pg_selling_price, quantity=order_product_dict[product.code]) return super(OrderAgainView, self).form_valid(form) def form_invalid(self, form): return JsonResponse({ 'status': 'false', 'message': 'Bad Request' }, status=400) def get_success_url(self): return reverse('card:cart', args=(self.store.code, )) def get_template_names(self): return 'card/{}/error.html'.format(self.store.theme)