1. Ian George
  2. Quiet apps

Commits

Ian George  committed 07fcc84

Added order.from_basket method to standardise things a bit.

  • Participants
  • Parent commits 3a52e7b
  • Branches default

Comments (0)

Files changed (2)

File quiet/shop/models/order.py

View file
             super(Order, self).save(*args, **kwargs)
         self.generate_reference()
         super(Order, self).save(*args, **kwargs) 
+
+    def from_basket(self, basket):
+        self.shipping = basket.shipping_price()
+        self.subtotal = basket.net_price(False)
+        self.total = basket.total_price()
+        self.balance = basket.total_price()
+    
+        for item in basket:
+            order_item, created = OrderItem.objects.get_or_create(
+                order=self, 
+                product_content_type=item.item_content_type,
+                product_object_id = item.item_object_id,
+                defaults={'qty':1, 'price':0, 'total':0}
+                )
+            order_item.qty = item.quantity
+            order_item.price = item.price
+            order_item.tax = (item.tax or 0)
+            order_item.total = (item.price * item.quantity) + (item.tax or 0)
+            order_item.save()
+            debug("payment", "Added %s to order %s" % (order_item, self.id))
+        
+        for item in basket.basket.shipping_items.all():
+            order_item, created = OrderItem.objects.get_or_create(
+                order=self,
+                product_content_type = item.item_content_type,
+                product_object_id = item.item_object_id,
+                qty = 1,
+                defaults={'qty':1, 'price':0, 'total':0}
+                )
+            order_item.price = item.price
+            order_item.tax = (item.tax or 0)
+            order_item.total = item.price + (item.tax or 0)
+            order_item.save()
+            debug("payment", "Added shipping %s to order %s" % (order_item, self.id))
+            
+            self.save()        
+
     
     class Meta:
         app_label='shop'

File quiet/shop/session.py

View file
         except:
             self.basket_id = int(request)
         basket, created = Basket.objects.get_or_create(id=self.basket_id)
-        if created and hasattr(request, 'session'):
+        if created and request and hasattr(request, 'session'):
             request.session[BASKET_SESSION_KEY] = basket.id
         self.basket = basket
 
         for item in self.basket.shipping_items.all():
             item.delete()
 
-
     def kill(self, request):
         signal_basket_kill_pre.send(sender=self.__class__, basket_id=self.basket_id)
         self.clear()