def add(request, id=0, redirect_to="satchmo_cart"): """Add an item to the cart.""" log.debug("FORM: %s", request.POST) formdata = request.POST.copy() productslug = None if "productname" in formdata: productslug = formdata["productname"] try: product, details = product_from_post(productslug, formdata) if not (product and product.active): return _product_error( request, product, _("That product is not available at the moment.")) except (Product.DoesNotExist, MultiValueDictKeyError): log.debug("Could not find product: %s", productslug) return bad_or_missing( request, _("The product you have requested does not exist.")) try: quantity = int(formdata["quantity"]) except ValueError: return _product_error(request, product, _("Please enter a whole number.")) if quantity < 1: return _product_error(request, product, _("Please enter a positive number.")) cart = Cart.objects.from_request(request, create=True) # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( cart, product=product, quantity=quantity, details=details, request=request, form=formdata, ) try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited as cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send( cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata, ) satchmo_cart_changed.send(cart, cart=cart, request=request) url = reverse(redirect_to) return HttpResponseRedirect(url)
def add(request, id=0, redirect_to="satchmo_cart"): """Add an item to the cart.""" log.debug("FORM: %s", request.POST) formdata = request.POST.copy() productslug = None if "productname" in formdata: productslug = formdata["productname"] try: product, details = product_from_post(productslug, formdata) if not (product and product.active): return _product_error( request, product, _("That product is not available at the moment.") ) except (Product.DoesNotExist, MultiValueDictKeyError): log.debug("Could not find product: %s", productslug) return bad_or_missing( request, _("The product you have requested does not exist.") ) try: quantity = int(formdata["quantity"]) except ValueError: return _product_error(request, product, _("Please enter a whole number.")) if quantity < 1: return _product_error(request, product, _("Please enter a positive number.")) cart = Cart.objects.from_request(request, create=True) # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( cart, product=product, quantity=quantity, details=details, request=request, form=formdata, ) try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited as cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send( cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata, ) satchmo_cart_changed.send(cart, cart=cart, request=request) url = reverse(redirect_to) return HttpResponseRedirect(url)
def _set_quantity(request, force_delete=False): """Set the quantity for a specific cartitem. Checks to make sure the item is actually in the user's cart. """ cart = Cart.objects.from_request(request, create=False) if isinstance(cart, NullCart): return (False, None, None, _("No cart to update.")) if force_delete: qty = 0 else: try: qty = int(request.POST.get("quantity")) except (TypeError, ValueError): return (False, cart, None, _("Bad quantity.")) if qty < 0: qty = 0 try: itemid = int(request.POST.get("cartitem")) except (TypeError, ValueError): return (False, cart, None, _("Bad item number.")) try: cartitem = CartItem.objects.get(pk=itemid, cart=cart) except CartItem.DoesNotExist: return (False, cart, None, _("No such item in your cart.")) if qty == 0: cartitem.delete() cartitem = NullCartItem(itemid) else: config = Config.objects.get_current() if config.no_stock_checkout is False: stock = cartitem.product.items_in_stock log.debug("checking stock quantity. Have %i, need %i", stock, qty) if stock < qty: return ( False, cart, cartitem, _("Not enough items of '%s' in stock.") % cartitem.product.name, ) cartitem.quantity = qty cartitem.save() satchmo_cart_changed.send(cart, cart=cart, request=request) return (True, cart, cartitem, "")
def _set_quantity(request, force_delete=False): """Set the quantity for a specific cartitem. Checks to make sure the item is actually in the user's cart. """ cart = Cart.objects.from_request(request, create=False) if isinstance(cart, NullCart): return (False, None, None, _("No cart to update.")) if force_delete: qty = 0 else: try: qty = int(request.POST.get("quantity")) except (TypeError, ValueError): return (False, cart, None, _("Bad quantity.")) if qty < 0: qty = 0 try: itemid = int(request.POST.get("cartitem")) except (TypeError, ValueError): return (False, cart, None, _("Bad item number.")) try: cartitem = CartItem.objects.get(pk=itemid, cart=cart) except CartItem.DoesNotExist: return (False, cart, None, _("No such item in your cart.")) if qty == 0: cartitem.delete() cartitem = NullCartItem(itemid) else: config = Config.objects.get_current() if config.no_stock_checkout is False: stock = cartitem.product.items_in_stock log.debug("checking stock quantity. Have %i, need %i", stock, qty) if stock < qty: return ( False, cart, cartitem, _("Not enough items of '%s' in stock.") % cartitem.product.translated_name(), ) cartitem.quantity = qty cartitem.save() satchmo_cart_changed.send(cart, cart=cart, request=request) return (True, cart, cartitem, "")
def wishlist_move_to_cart(request): wish, msg = _wish_from_post(request) if wish: cart = Cart.objects.from_request(request, create=True) try: cart.add_item(wish.product, number_added=1, details=wish.details) except CartAddProhibited, cap: msg = _("Wishlist product '%(product)s' could't be added to the cart. %(details)s") % { 'product': wish.product.translated_name, 'detail': cap.message } return wishlist_view(request, message=msg) url = urlresolvers.reverse('satchmo_cart') satchmo_cart_changed.send(cart, cart=cart, request=request) return HttpResponseRedirect(url)
def wishlist_move_to_cart(request): wish, msg = _wish_from_post(request) if wish: cart = Cart.objects.from_request(request, create=True) try: cart.add_item(wish.product, number_added=1, details=wish.details) except CartAddProhibited, cap: msg = _( "Wishlist product '%(product)s' could't be added to the cart. %(details)s" ) % { 'product': wish.product.translated_name, 'detail': cap.message } return wishlist_view(request, message=msg) url = urlresolvers.reverse('satchmo_cart') satchmo_cart_changed.send(cart, cart=cart, request=request) return HttpResponseRedirect(url)
cart, product=product, quantity=quantity, details=details, request=request, form=formdata ) try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited, cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata) satchmo_cart_changed.send(cart, cart=cart, request=request) url = urlresolvers.reverse(redirect_to) return HttpResponseRedirect(url) def add_ajax(request, id=0, template="json.html"): data = {'errors': []} product = None formdata = request.POST.copy() if not formdata.has_key('productname'): data['errors'].append(('product', _('No product requested'))) else: productslug = formdata['productname'] log.debug('CART_AJAX: slug=%s', productslug) try: product, details = product_from_post(productslug, formdata)
def add_ajax(request, id=0, template="json.html"): data = {"errors": []} product = None formdata = request.POST.copy() if "productname" not in formdata: data["errors"].append(("product", _("No product requested"))) else: productslug = formdata["productname"] log.debug("CART_AJAX: slug=%s", productslug) try: product, details = product_from_post(productslug, formdata) except Product.DoesNotExist: log.warn("Could not find product: %s", productslug) product = None if not product: data["errors"].append( ("product", _("The product you have requested does not exist."))) else: if not product.active: data["errors"].append( ("product", _("That product is not available at the moment."))) else: data["id"] = product.id data["name"] = product.name if "quantity" not in formdata: quantity = -1 else: quantity = formdata["quantity"] try: quantity = int(quantity) if quantity < 0: data["errors"].append( ("quantity", _("Choose a quantity."))) except (TypeError, ValueError): data["errors"].append( ("quantity", _("Choose a whole number."))) tempCart = Cart.objects.from_request(request, create=True) if not data["errors"]: # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( tempCart, product=product, quantity=quantity, details=details, request=request, form=formdata, ) try: added_item = tempCart.add_item(product, number_added=quantity) request.session["cart"] = tempCart.id data["results"] = _("Success") if added_item: # send a signal so that listeners can also operate on this form and item. satchmo_cart_add_complete.send( tempCart, cartitem=added_item, product=product, request=request, form=formdata, ) except CartAddProhibited as cap: data["results"] = _("Error") data["errors"].append(("product", cap.message)) else: data["results"] = _("Error") data["cart_count"] = tempCart.numItems encoded = json.JSONEncoder().encode(data) encoded = mark_safe(encoded) log.debug("CART AJAX: %s", data) satchmo_cart_changed.send(tempCart, cart=tempCart, request=request) return render(template, {"json": encoded})
def add_ajax(request, id=0, template="json.html"): data = {"errors": []} product = None formdata = request.POST.copy() if "productname" not in formdata: data["errors"].append(("product", _("No product requested"))) else: productslug = formdata["productname"] log.debug("CART_AJAX: slug=%s", productslug) try: product, details = product_from_post(productslug, formdata) except Product.DoesNotExist: log.warn("Could not find product: %s", productslug) product = None if not product: data["errors"].append( ("product", _("The product you have requested does not exist.")) ) else: if not product.active: data["errors"].append( ("product", _("That product is not available at the moment.")) ) else: data["id"] = product.id data["name"] = product.translated_name() if "quantity" not in formdata: quantity = -1 else: quantity = formdata["quantity"] try: quantity = int(quantity) if quantity < 0: data["errors"].append(("quantity", _("Choose a quantity."))) except (TypeError, ValueError): data["errors"].append(("quantity", _("Choose a whole number."))) tempCart = Cart.objects.from_request(request, create=True) if not data["errors"]: # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( tempCart, product=product, quantity=quantity, details=details, request=request, form=formdata, ) try: added_item = tempCart.add_item(product, number_added=quantity) request.session["cart"] = tempCart.id data["results"] = _("Success") if added_item: # send a signal so that listeners can also operate on this form and item. satchmo_cart_add_complete.send( tempCart, cartitem=added_item, product=product, request=request, form=formdata, ) except CartAddProhibited as cap: data["results"] = _("Error") data["errors"].append(("product", cap.message)) else: data["results"] = _("Error") data["cart_count"] = tempCart.numItems encoded = json.JSONEncoder().encode(data) encoded = mark_safe(encoded) log.debug("CART AJAX: %s", data) satchmo_cart_changed.send(tempCart, cart=tempCart, request=request) return render(template, {"json": encoded})
try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited, cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata) satchmo_cart_changed.send(cart, cart=cart, request=request) url = urlresolvers.reverse(redirect_to) return HttpResponseRedirect(url) def add_ajax(request, id=0, template="json.html"): data = {'errors': []} product = None formdata = request.POST.copy() if 'productname' not in formdata: data['errors'].append(('product', _('No product requested'))) else: productslug = formdata['productname'] log.debug('CART_AJAX: slug=%s', productslug) try: