Sfoglia il codice sorgente

Updated repository and added tests

master
David Winterbottom 13 anni fa
parent
commit
addaa0d9be

+ 2
- 3
oscar/apps/checkout/views.py Vedi File

@@ -285,9 +285,8 @@ class ShippingMethodView(CheckoutSessionMixin, TemplateView):
285 285
         # Shipping methods can depend on the user, the contents of the basket
286 286
         # and the shipping address.  I haven't come across a scenario that doesn't
287 287
         # fit this system.
288
-        repo = Repository()
289
-        return repo.get_shipping_methods(self.request.user, self.request.basket, 
290
-                                         self.get_shipping_address())
288
+        return Repository().get_shipping_methods(self.request.user, self.request.basket, 
289
+                                                 self.get_shipping_address())
291 290
     
292 291
     def post(self, request, *args, **kwargs):
293 292
         # Need to check that this code is valid for this user

+ 2
- 1
oscar/apps/shipping/base.py Vedi File

@@ -6,8 +6,9 @@ class ShippingMethod(object):
6 6
     name = 'Default shipping'
7 7
     description = ''
8 8
     
9
-    def __init__(self):
9
+    def __init__(self, *args, **kwargs):
10 10
         self.exempt_from_tax = False
11
+        super(ShippingMethod, self).__init__(*args, **kwargs)
11 12
     
12 13
     def set_basket(self, basket):
13 14
         self.basket = basket

+ 1
- 7
oscar/apps/shipping/models.py Vedi File

@@ -5,11 +5,8 @@ from django.utils.translation import ugettext_lazy as _
5 5
 from django.template.defaultfilters import slugify
6 6
 from django.conf import settings
7 7
 
8
-from oscar.apps.shipping.abstract_models import AbstractOrderAndItemLevelChargeMethod
9
-from oscar.apps.shipping.base import ShippingMethod
10 8
 
11
-
12
-class OrderAndItemLevelChargeMethod(models.Model, ShippingMethod):
9
+class OrderAndItemLevelChargeMethod(models.Model):
13 10
     """
14 11
     Standard shipping method
15 12
     
@@ -37,9 +34,6 @@ class OrderAndItemLevelChargeMethod(models.Model, ShippingMethod):
37 34
         if not self.code:
38 35
             self.code = slugify(self.name)
39 36
         super(AbstractOrderAndItemLevelChargeMethod, self).save(*args, **kwargs)
40
-    
41
-    class Meta:
42
-        abstract = True
43 37
         
44 38
     def __unicode__(self):
45 39
         return self.name

+ 4
- 9
oscar/apps/shipping/repository.py Vedi File

@@ -1,6 +1,4 @@
1
-from oscar.apps.shipping.methods import FreeShipping
2
-from oscar.core.loading import import_module
3
-shipping_models = import_module('shipping.models', ['OrderAndItemLevelChargeMethod'])
1
+from oscar.apps.shipping.methods import FreeShipping, OrderAndItemLevelChargeMethod
4 2
 
5 3
 
6 4
 class Repository(object):
@@ -9,7 +7,7 @@ class Repository(object):
9 7
     objects for a given user, basket etc
10 8
     """
11 9
     
12
-    def get_shipping_methods(self, user, basket, shipping_addr, **kwargs):
10
+    def get_shipping_methods(self, user, basket, shipping_addr=None, **kwargs):
13 11
         """
14 12
         Return a list of all applicable shipping method objects
15 13
         for a given basket.
@@ -18,10 +16,7 @@ class Repository(object):
18 16
         this behaviour can easily be overridden by subclassing this class
19 17
         and overriding this method.
20 18
         """ 
21
-        methods = shipping_models.OrderAndItemLevelChargeMethod._default_manager.all()
22
-        if not methods.count():
23
-            return [FreeShipping()]
24
-        
19
+        methods = [FreeShipping()]
25 20
         for method in methods:
26 21
             method.set_basket(basket)
27 22
         return methods
@@ -32,4 +27,4 @@ class Repository(object):
32 27
         """
33 28
         if code == FreeShipping.code:
34 29
             return FreeShipping()
35
-        return shipping_models.OrderAndItemLevelChargeMethod._default_manager.get(code=code)          
30
+        return None

+ 15
- 0
oscar/apps/shipping/tests.py Vedi File

@@ -2,9 +2,11 @@ from decimal import Decimal as D
2 2
 
3 3
 from django.utils import unittest
4 4
 from django.test.client import Client
5
+from django.contrib.auth.models import User
5 6
 
6 7
 from oscar.apps.shipping.methods import FreeShipping, FixedPriceShipping, WeightBasedChargesMethod
7 8
 from oscar.apps.shipping.models import OrderAndItemLevelChargeMethod, WeightBand
9
+from oscar.apps.shipping.repository import Repository
8 10
 from oscar.apps.shipping import Scales
9 11
 from oscar.apps.basket.models import Basket
10 12
 from oscar.test.helpers import create_product
@@ -247,3 +249,16 @@ class WeightBandTests(unittest.TestCase):
247 249
         WeightBand.objects.create(method_code='express', upper_limit=2, charge=D('8.00'))
248 250
         WeightBand.objects.create(method_code='standard', upper_limit=3, charge=D('12.00'))
249 251
         self.assertEqual(D('12.00'), WeightBand.get_band_for_weight('standard', 2.5).charge)
252
+
253
+
254
+class RepositoryTests(unittest.TestCase):
255
+
256
+    def setUp(self):
257
+        self.repo = Repository()
258
+
259
+    def test_default_method_is_free(self):
260
+        user, basket = User(), Basket()
261
+        methods = self.repo.get_shipping_methods(user, basket)
262
+        self.assertEqual(1, len(methods))
263
+        self.assertTrue(isinstance(methods[0], FreeShipping))
264
+

Loading…
Annulla
Salva