def delete_shipping_method(request, shipping_method_id): """Deletes shipping method with passed shipping id. All customers, which have selected this shipping method are getting the default shipping method. """ try: shipping_method = ShippingMethod.objects.get(pk=shipping_method_id) except ObjectDoesNotExist: pass else: for customer in Customer.objects.filter(selected_shipping_method=shipping_method_id): customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) customer.save() shipping_method.delete() return muecke.core.utils.set_message_cookie( url=reverse("muecke_manage_shipping"), msg=_(u"Shipping method has been deleted."), )
def delete_shipping_method(request, shipping_method_id): """Deletes shipping method with passed shipping id. All customers, which have selected this shipping method are getting the default shipping method. """ try: shipping_method = ShippingMethod.objects.get(pk=shipping_method_id) except ObjectDoesNotExist: pass else: for customer in Customer.objects.filter( selected_shipping_method=shipping_method_id): customer.selected_shipping_method = shipping_utils.get_default_shipping_method( request) customer.save() shipping_method.delete() return muecke.core.utils.set_message_cookie( url=reverse("muecke_manage_shipping"), msg=_(u"Shipping method has been deleted."), )
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = "" for item in cart.get_items(): try: value = request.POST.get("amount-cart-item_%s" % item.id, "0.0") if isinstance(value, unicode): # atof() on unicode string fails in some environments, like Czech value = value.encode("utf-8") amount = locale.atof(value) except (TypeError, ValueError): amount = 1.0 if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount if amount < 0: amount = 0 if amount == 0: message = _(u"Sorry, but '%(product)s' is not available anymore." % {"product": item.product.name}) elif amount == 1: message = _(u"Sorry, but '%(product)s' is only one time available." % {"product": item.product.name}) else: message = _(u"Sorry, but '%(product)s' is only %(amount)s times available.") % {"product": item.product.name, "amount": amount} if item.product.get_active_packing_unit(): item.amount = muecke.catalog.utils.calculate_real_amount(item.product, float(amount)) else: item.amount = amount if amount == 0: item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method customer.selected_shipping_method_id = request.POST.get("shipping_method") valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method(request) # Update payment method customer.selected_payment_method_id = request.POST.get("payment_method") # Last but not least we save the customer ... customer.save() result = simplejson.dumps({ "html": cart_inline(request), "message": message, }, cls=LazyEncoder) return HttpResponse(result)
def refresh_cart(request): """ Refreshes the cart after some changes has been taken place, e.g.: the amount of a product or shipping/payment method. """ cart = cart_utils.get_cart(request) customer = customer_utils.get_or_create_customer(request) # Update country country_iso = request.POST.get("country") if country_iso: selected_country = Country.objects.get(code=country_iso.lower()) customer.selected_country_id = selected_country.id if customer.selected_shipping_address: customer.selected_shipping_address.country = selected_country customer.selected_shipping_address.save() customer.selected_shipping_address.save() if customer.selected_invoice_address: customer.selected_invoice_address.country = selected_country customer.selected_invoice_address.save() customer.selected_invoice_address.save() # NOTE: The customer has to be saved already here in order to calculate # a possible new valid shippig method below, which coulb be triggered by # the changing of the shipping country. customer.save() # Update Amounts message = "" for item in cart.get_items(): try: value = request.POST.get("amount-cart-item_%s" % item.id, "0.0") if isinstance(value, unicode): # atof() on unicode string fails in some environments, like Czech value = value.encode("utf-8") amount = locale.atof(value) except (TypeError, ValueError): amount = 1.0 if item.product.manage_stock_amount and amount > item.product.stock_amount and not item.product.order_time: amount = item.product.stock_amount if amount < 0: amount = 0 if amount == 0: message = _( u"Sorry, but '%(product)s' is not available anymore." % {"product": item.product.name}) elif amount == 1: message = _( u"Sorry, but '%(product)s' is only one time available." % {"product": item.product.name}) else: message = _( u"Sorry, but '%(product)s' is only %(amount)s times available." ) % { "product": item.product.name, "amount": amount } if item.product.get_active_packing_unit(): item.amount = muecke.catalog.utils.calculate_real_amount( item.product, float(amount)) else: item.amount = amount if amount == 0: item.delete() else: item.save() # IMPORTANT: We have to send the signal already here, because the valid # shipping methods might be dependent on the price. cart_changed.send(cart, request=request) # Update shipping method customer.selected_shipping_method_id = request.POST.get("shipping_method") valid_shipping_methods = shipping_utils.get_valid_shipping_methods(request) if customer.selected_shipping_method not in valid_shipping_methods: customer.selected_shipping_method = shipping_utils.get_default_shipping_method( request) # Update payment method customer.selected_payment_method_id = request.POST.get("payment_method") # Last but not least we save the customer ... customer.save() result = simplejson.dumps( { "html": cart_inline(request), "message": message, }, cls=LazyEncoder) return HttpResponse(result)