def add_product_to_cart(request): cart, created = CartModel.objects.get_or_create(user=request.user) context = {'success': False} logger.debug("ajax-add-to-cart") utils.show_request(request) template_name = 'catalog/product_detail.html' page_title = _('Product Detail') if request.method == 'POST': postdata = request.POST.copy() logger.info("send as POST") form = AddCartForm(postdata) if form.is_valid(): product = form.cleaned_data['product'] result, cart = cart_service.add_to_cart(cart, product) if result: context['success'] = True context['status'] = True #return redirect(product.get_absolute_url()) else: context['error'] = 'Form is invalid' context['status'] = False messages.error(request, message="Invalid Form") #if request.is_ajax(): # return JsonResponse(context,status=HTTPStatus.BAD_REQUEST) else: context['error'] = 'Bad Request' context['status'] = False return
def add_to_cart_old(request): cart, created = CartModel.objects.get_or_create(user=request.user) context = {'success': False} logger.debug("ajax-add-to-cart") if request.method == 'POST': postdata = request.POST.copy() form = AddToCartForm(postdata) if form.is_valid(): product = form.cleaned_data['product'] result, cart = cart_service.add_to_cart(cart, product) if result: context['success'] = True context['status'] = True #return redirect(product.get_absolute_url()) else: context['error'] = 'Form is invalid' context['status'] = False messages.error(request, message="Invalid Form") #if request.is_ajax(): # return JsonResponse(context,status=HTTPStatus.BAD_REQUEST) else: context['error'] = 'Bad Request' context['status'] = False return redirect('catalog:home')
def ajax_add_to_cart_old(request): cart, created = CartModel.objects.get_or_create(user=request.user) context = {'success': False} if request.method == 'POST': postdata = request.POST.copy() form = AddCartForm(postdata) if form.is_valid(): logger.debug("Summitted data are valid") variant_uuid = form.cleaned_data['variant_uuid'] attr = form.cleaned_data['attr'] variant = get_object_or_404(ProductVariant, product_uuid=variant_uuid) result, cart = cart_service.add_to_cart(cart, variant) prefix = variant.product.display_name if result: cart.refresh_from_db() context['success'] = True context['status'] = True context['quantity'] = cart.quantity context['message'] = _(CORE_UI_STRINGS.PRODUCT_ADDED) return JsonResponse(context) else: context['success'] = False context['status'] = True context['quantity'] = cart.quantity context['message'] = _( CORE_UI_STRINGS.PRODUCT_QTY_NOT_AVAILABLE) return JsonResponse(context) else: logger.error(f"Form is invalid. {form.errors}") context['error'] = _(CORE_UI_STRINGS.INVALID_FORM) context['status'] = False return JsonResponse(context, status=HTTPStatus.BAD_REQUEST) else: logger.warn("Ajax send as GET Request") context['error'] = 'Bad Request' context['status'] = False return JsonResponse(context, status=HTTPStatus.BAD_REQUEST)
def product_detail_slug(request, category_slug=None, product_slug=None, product_uuid=None): template_name = 'catalog/product_detail.html' product = get_object_or_404(Product, category__slug=category_slug, slug=product_slug, product_uuid=product_uuid) page_title = product.display_name if request.method == "POST": if request.user.is_authenticated: form = AddCartForm(utils.get_postdata(request)) if form.is_valid(): variant = get_object_or_404( ProductVariant, product_uuid=form.cleaned_data['variant_uuid']) item, cart = cart_service.add_to_cart( cart_service.get_cart(request.user), variant) if item: messages.success(request, message=_("Product added")) logger.info("Product added") else: messages.success(request, message=_("Product not added")) logger.info("Product not added") else: messages.warning(request, message=_("Invalid form")) logger.info( f"Product not added. Form is not valid : {form.errors} ") else: messages.warning( request, message=_( "You must first login before you can add item to your cart" )) logger.info(f"product_details : Product not added. anonyme user") Product.objects.filter(product_uuid=product.product_uuid).update( view_count=F('view_count') + 1) images = ProductImage.objects.filter(product=product) common_attrs, selective_attrs = catalog_service.get_product_attributes( product.id) product_attrs = catalog_service.product_attributes(product.id) if request.user.is_authenticated: wishlist_list = wishlist_service.get_wishlists( {'customer': request.user}) else: wishlist_list = None context = { 'page_title': page_title, 'product': product, 'image_list': images, 'common_attrs': common_attrs, 'selective_attrs': selective_attrs, 'product_attrs': product_attrs, 'OG_TITLE': page_title, 'OG_DESCRIPTION': product.short_description, 'OG_IMAGE': request.build_absolute_uri(product.images.first().get_image_url()), 'OG_URL': request.build_absolute_uri(), 'wishlist_list': wishlist_list, 'recommendations': inventory_service.get_recommandations(product), 'recommendation_label': CORE_UI_STRING.RECOMMANDATION_LABEL } return render(request, template_name, context)