Commits

Kai Diefenbach committed b52e7f2

Bugfix: restrict adding to cart if the product is not deliverable.

Comments (0)

Files changed (4)

lfs/cart/views.py

 from lfs.cart import utils as cart_utils
 from lfs.cart.models import CartItem
 from lfs.core.utils import l10n_float
+from lfs.core.utils import LazyEncoder
 from lfs.shipping import utils as shipping_utils
 from lfs.payment import utils as payment_utils
 from lfs.customer import utils as customer_utils
     if (product.is_active() and product.is_deliverable()) == False:
         raise Http404()
 
+    try:
+        quantity = float(request.POST.get("quantity", 1))
+    except TypeError:
+        quantity = 1
+
     # Validate properties (They are added below)
     properties_dict = {}
     if product.is_configurable_product():
                     value = l10n_float(value)
 
                 properties_dict[property_id] = unicode(value)
-                                
+
                 # validate property's value
                 if property.is_number_field:
 
         variant_id = request.POST.get("variant_id")
         product = lfs_get_object_or_404(Product, pk=variant_id)
 
-    try:
-        quantity = float(request.POST.get("quantity", 1))
-    except TypeError:
-        quantity = 1
-
     if product.active_packing_unit:
         quantity = lfs.catalog.utils.calculate_real_amount(product, quantity)
 
 
     cart_items = [cart_item]
 
+    # Check stock amount
+    message = ""
+    if product.manage_stock_amount and cart_item.amount > product.stock_amount and not product.order_time:
+        if product.stock_amount == 0:
+            message = _(u"Sorry, but '%(product)s' is not available anymore." % {"product": product.name})
+        elif product.stock_amount == 1:
+            message = _(u"Sorry, but '%(product)s' is only one time available." % {"product": product.name})
+        else:
+            message = _(u"Sorry, but '%(product)s' is only %(amount)s times available.") % {"product": product.name, "amount" : amount}
+        cart_item.amount = product.stock_amount
+        cart_item.save()
+
     # Add selected accessories to cart
     for key, value in request.POST.items():
         if key.startswith("accessory"):
     except AttributeError:
         url_name = "lfs.cart.views.added_to_cart"
 
-    return HttpResponseRedirect(reverse(url_name))
+    if message:
+        return lfs.core.utils.set_message_cookie(reverse(url_name), message)
+    else:
+        return HttpResponseRedirect(reverse(url_name))
 
 def delete_cart_item(request, cart_item_id):
     """Deletes the cart item with the given id.
         amount = request.POST.get("amount-cart-item_%s" % item.id, 0)
         try:
             amount = float(amount)
-            if item.product.manage_stock_amount and amount > item.product.stock_amount:
+            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
-                message = _(u"Sorry, but there are only %(amount)s article(s) in stock.") % {"amount" : amount}
+
+                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}
+
         except ValueError:
             amount = 1
 
     result = simplejson.dumps({
         "html" : cart_inline(request),
         "message" : message,
-    })
+    }, cls = LazyEncoder)
 
     return HttpResponse(result)
 

lfs/catalog/models.py

     delivery_time = models.ForeignKey("DeliveryTime", verbose_name=_(u"Delivery time"), blank=True, null=True, related_name="products_delivery_time")
     order_time = models.ForeignKey("DeliveryTime", verbose_name=_(u"Order time"), blank=True, null=True, related_name="products_order_time")
     ordered_at = models.DateField(_(u"Ordered at"), blank=True, null=True)
-    manage_stock_amount = models.BooleanField(_(u"Manage stock amount"), default=True)
+    manage_stock_amount = models.BooleanField(_(u"Manage stock amount"), default=False)
     stock_amount = models.FloatField(_(u"Stock amount"), default=0)
 
     active_packing_unit = models.BooleanField(_(u"Active packing unit"), default=False)
     def is_deliverable(self):
         """Returns the deliverable state of the product.
         """
-        if self.is_variant():
-            return self.parent.deliverable
-        else:
+        if self.manage_stock_amount and self.stock_amount <= 0 and not self.order_time:
+            return False
+        else:    
             return self.deliverable
 
     # 3rd party contracts

lfs/core/templatetags/lfs_tags.py

 
 def _get_shipping(context, product):
     request = context.get("request")
-    if product.deliverable == False:
+    if product.is_deliverable() == False:
         return {
             "deliverable" : False,
             "delivery_time" : shipping_utils.get_product_delivery_time(request, product.slug)

lfs/shipping/utils.py

             except AttributeError:
                 delivery_time = None
 
+    # TODO: Define default delivery time for the shop
+    if delivery_time is None:
+        delivery_time = DeliveryTime(min=1, max=2, unit=DELIVERY_TIME_UNIT_DAYS)
+
     # Calculate the total delivery time if the product is not on stock.
     if (product.stock_amount <= 0) and (product.order_time):
 
         delivery_time += order_time_left
         delivery_time = delivery_time.as_reasonable_unit()
 
-    # TODO: Define default delivery time for the shop
-    if delivery_time is None:
-        delivery_time = DeliveryTime(min=1, max=2, unit=DELIVERY_TIME_UNIT_DAYS)
-
     delivery_time = delivery_time.round()
     shippings[product_key] = delivery_time
     cache.set(cache_key, shippings)