Commits

twidi committed b60b502

Workaround to resolve the bug when we want to pay for a old order with nothing paid. Also add __iter__ and save to the OrderCart to be used during the process. See #1309

Comments (0)

Files changed (2)

satchmo/apps/payment/utils.py

 from decimal import Decimal
 from livesettings import config_get_group
-from satchmo_store.shop.models import Order, OrderItem, OrderItemDetail
+from satchmo_store.shop.models import Order, OrderItem, OrderItemDetail, OrderCart
 from satchmo_store.shop.signals import satchmo_post_copy_item_to_order
 from shipping.utils import update_shipping
 import logging
     """Update the order with all cart items, first removing all items if this
     is an update.
     """
-    if update:
-        new_order.remove_all_items()
-    else:
-        # have to save first, or else we can't add orderitems
-        new_order.site = cart.site
-        new_order.save()
+    if not isinstance(cart, OrderCart):
+        if update:
+            new_order.remove_all_items()
+        else:
+            # have to save first, or else we can't add orderitems
+            new_order.site = cart.site
+            new_order.save()
 
-    # Add all the items in the cart to the order
-    for item in cart.cartitem_set.all():
-        new_order_item = OrderItem(order=new_order,
-            product=item.product,
-            quantity=item.quantity,
-            unit_price=item.unit_price,
-            line_item_price=item.line_total)
+        # Add all the items in the cart to the order
+        for item in cart.cartitem_set.all():
+            new_order_item = OrderItem(order=new_order,
+                product=item.product,
+                quantity=item.quantity,
+                unit_price=item.unit_price,
+                line_item_price=item.line_total)
 
-        update_orderitem_for_subscription(new_order_item, item)
-        update_orderitem_details(new_order_item, item)
+            update_orderitem_for_subscription(new_order_item, item)
+            update_orderitem_details(new_order_item, item)
 
-        # Send a signal after copying items
-        # External applications can copy their related objects using this
-        satchmo_post_copy_item_to_order.send(
-                cart,
-                cartitem=item,
-                order=new_order, orderitem=new_order_item
-                )
+            # Send a signal after copying items
+            # External applications can copy their related objects using this
+            satchmo_post_copy_item_to_order.send(
+                    cart,
+                    cartitem=item,
+                    order=new_order, orderitem=new_order_item
+                    )
 
     new_order.recalculate_total()

satchmo/apps/satchmo_store/shop/models.py

     def __len__(self):
         return 0
 
+    def save(self):
+        pass
+
 class OrderCart(NullCart):
     """Allows us to fake a cart if we are reloading an order."""
 
     def __len__(self):
         return self.numItems
 
+    def __iter__(self):
+        return iter(self.cartitem_set.all())
+
 class CartManager(models.Manager):
 
     def from_request(self, request, create=False, return_nullcart=True):
         """Given the addressee name, try to return a first name"""
         return ' '.join(self.ship_addressee.split()[0:-1]) or ''
     ship_first_name = property(_ship_first_name)
-        
+
     def _ship_last_name(self):
         """Given the addressee name, try to return a last name"""
         return ' '.join(self.ship_addressee.split()[-1:]) or ''