Przeglądaj źródła

Updated repository and added tests

master
David Winterbottom 13 lat temu
rodzic
commit
addaa0d9be

+ 2
- 3
oscar/apps/checkout/views.py Wyświetl plik

285
         # Shipping methods can depend on the user, the contents of the basket
285
         # Shipping methods can depend on the user, the contents of the basket
286
         # and the shipping address.  I haven't come across a scenario that doesn't
286
         # and the shipping address.  I haven't come across a scenario that doesn't
287
         # fit this system.
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
     def post(self, request, *args, **kwargs):
291
     def post(self, request, *args, **kwargs):
293
         # Need to check that this code is valid for this user
292
         # Need to check that this code is valid for this user

+ 2
- 1
oscar/apps/shipping/base.py Wyświetl plik

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

+ 1
- 7
oscar/apps/shipping/models.py Wyświetl plik

5
 from django.template.defaultfilters import slugify
5
 from django.template.defaultfilters import slugify
6
 from django.conf import settings
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
     Standard shipping method
11
     Standard shipping method
15
     
12
     
37
         if not self.code:
34
         if not self.code:
38
             self.code = slugify(self.name)
35
             self.code = slugify(self.name)
39
         super(AbstractOrderAndItemLevelChargeMethod, self).save(*args, **kwargs)
36
         super(AbstractOrderAndItemLevelChargeMethod, self).save(*args, **kwargs)
40
-    
41
-    class Meta:
42
-        abstract = True
43
         
37
         
44
     def __unicode__(self):
38
     def __unicode__(self):
45
         return self.name
39
         return self.name

+ 4
- 9
oscar/apps/shipping/repository.py Wyświetl plik

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
 class Repository(object):
4
 class Repository(object):
9
     objects for a given user, basket etc
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
         Return a list of all applicable shipping method objects
12
         Return a list of all applicable shipping method objects
15
         for a given basket.
13
         for a given basket.
18
         this behaviour can easily be overridden by subclassing this class
16
         this behaviour can easily be overridden by subclassing this class
19
         and overriding this method.
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
         for method in methods:
20
         for method in methods:
26
             method.set_basket(basket)
21
             method.set_basket(basket)
27
         return methods
22
         return methods
32
         """
27
         """
33
         if code == FreeShipping.code:
28
         if code == FreeShipping.code:
34
             return FreeShipping()
29
             return FreeShipping()
35
-        return shipping_models.OrderAndItemLevelChargeMethod._default_manager.get(code=code)          
30
+        return None

+ 15
- 0
oscar/apps/shipping/tests.py Wyświetl plik

2
 
2
 
3
 from django.utils import unittest
3
 from django.utils import unittest
4
 from django.test.client import Client
4
 from django.test.client import Client
5
+from django.contrib.auth.models import User
5
 
6
 
6
 from oscar.apps.shipping.methods import FreeShipping, FixedPriceShipping, WeightBasedChargesMethod
7
 from oscar.apps.shipping.methods import FreeShipping, FixedPriceShipping, WeightBasedChargesMethod
7
 from oscar.apps.shipping.models import OrderAndItemLevelChargeMethod, WeightBand
8
 from oscar.apps.shipping.models import OrderAndItemLevelChargeMethod, WeightBand
9
+from oscar.apps.shipping.repository import Repository
8
 from oscar.apps.shipping import Scales
10
 from oscar.apps.shipping import Scales
9
 from oscar.apps.basket.models import Basket
11
 from oscar.apps.basket.models import Basket
10
 from oscar.test.helpers import create_product
12
 from oscar.test.helpers import create_product
247
         WeightBand.objects.create(method_code='express', upper_limit=2, charge=D('8.00'))
249
         WeightBand.objects.create(method_code='express', upper_limit=2, charge=D('8.00'))
248
         WeightBand.objects.create(method_code='standard', upper_limit=3, charge=D('12.00'))
250
         WeightBand.objects.create(method_code='standard', upper_limit=3, charge=D('12.00'))
249
         self.assertEqual(D('12.00'), WeightBand.get_band_for_weight('standard', 2.5).charge)
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
+

Ładowanie…
Anuluj
Zapisz