Browse Source

Weight-based shipping bands now support different method codes

master
David Winterbottom 14 years ago
parent
commit
8a9c548ec5
2 changed files with 43 additions and 15 deletions
  1. 7
    3
      oscar/apps/shipping/models.py
  2. 36
    12
      oscar/apps/shipping/tests.py

+ 7
- 3
oscar/apps/shipping/models.py View File

1
+from decimal import Decimal as D
1
 from django.db import models
2
 from django.db import models
2
 from django.utils.translation import ugettext_lazy as _
3
 from django.utils.translation import ugettext_lazy as _
3
 
4
 
9
 
10
 
10
 
11
 
11
 class WeightBand(models.Model):
12
 class WeightBand(models.Model):
13
+    method_code = models.CharField(max_length=64, db_index=True)
12
     upper_limit = models.DecimalField(decimal_places=2, max_digits=12,
14
     upper_limit = models.DecimalField(decimal_places=2, max_digits=12,
13
                                       help_text=_("""Enter upper limit of this weight band in Kg"""))
15
                                       help_text=_("""Enter upper limit of this weight band in Kg"""))
14
     charge = models.DecimalField(decimal_places=2, max_digits=12)
16
     charge = models.DecimalField(decimal_places=2, max_digits=12)
15
     
17
     
16
     @property
18
     @property
17
     def weight_from(self):
19
     def weight_from(self):
18
-        lower_bands = WeightBand.objects.filter(upper_limit__lt=self.upper_limit).order_by('-upper_limit')
20
+        lower_bands = WeightBand.objects.filter(method_code=self.method_code,
21
+                upper_limit__lt=self.upper_limit).order_by('-upper_limit')
19
         if not lower_bands:
22
         if not lower_bands:
20
             return D('0.00')
23
             return D('0.00')
21
         return lower_bands[0].upper_limit
24
         return lower_bands[0].upper_limit
31
         return u'Charge for weights up to %s' % (self.upper_limit,)
34
         return u'Charge for weights up to %s' % (self.upper_limit,)
32
         
35
         
33
     @classmethod
36
     @classmethod
34
-    def get_band_for_weight(cls, weight):
37
+    def get_band_for_weight(cls, method_code, weight):
35
         """
38
         """
36
         Return the weight band for a given weight
39
         Return the weight band for a given weight
37
         """
40
         """
38
-        bands = WeightBand.objects.filter(upper_limit__gte=weight).order_by('upper_limit')
41
+        bands = WeightBand.objects.filter(method_code=method_code, 
42
+                upper_limit__gte=weight).order_by('upper_limit')
39
         if not bands.count():
43
         if not bands.count():
40
             # No band for this weight
44
             # No band for this weight
41
             return None
45
             return None

+ 36
- 12
oscar/apps/shipping/tests.py View File

136
         WeightBand.objects.all().delete()
136
         WeightBand.objects.all().delete()
137
 
137
 
138
     def test_get_band_for_lower_weight(self):
138
     def test_get_band_for_lower_weight(self):
139
-        band = WeightBand.objects.create(upper_limit=1, charge=D('4.00'))
140
-        fetched_band = WeightBand.get_band_for_weight(0.5)
139
+        band = WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
140
+        fetched_band = WeightBand.get_band_for_weight('standard', 0.5)
141
         self.assertEqual(band.id, fetched_band.id)
141
         self.assertEqual(band.id, fetched_band.id)
142
 
142
 
143
     def test_get_band_for_higher_weight(self):
143
     def test_get_band_for_higher_weight(self):
144
-        band = WeightBand.objects.create(upper_limit=1, charge=D('4.00'))
145
-        fetched_band = WeightBand.get_band_for_weight(1.5)
144
+        band = WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
145
+        fetched_band = WeightBand.get_band_for_weight('standard', 1.5)
146
         self.assertIsNone(fetched_band)
146
         self.assertIsNone(fetched_band)
147
 
147
 
148
     def test_get_band_for_matching_weight(self):
148
     def test_get_band_for_matching_weight(self):
149
-        band = WeightBand.objects.create(upper_limit=1, charge=D('4.00'))
150
-        fetched_band = WeightBand.get_band_for_weight(1)
149
+        band = WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
150
+        fetched_band = WeightBand.get_band_for_weight('standard', 1)
151
         self.assertEqual(band.id, fetched_band.id)
151
         self.assertEqual(band.id, fetched_band.id)
152
 
152
 
153
-    def test_get_band_for_series_of_bands(self):
153
+    def test_weight_to_is_upper_bound(self):
154
+        band = WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
155
+        self.assertEqual(1, band.weight_to)
156
+
157
+    def test_weight_from_for_single_band(self):
158
+        band = WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
159
+        self.assertEqual(0, band.weight_from)
160
+
161
+    def test_weight_from_for_multiple_bands(self):
154
         WeightBand.objects.create(upper_limit=1, charge=D('4.00'))
162
         WeightBand.objects.create(upper_limit=1, charge=D('4.00'))
155
-        WeightBand.objects.create(upper_limit=2, charge=D('8.00'))
156
-        WeightBand.objects.create(upper_limit=3, charge=D('12.00'))
157
-        self.assertEqual(D('4.00'), WeightBand.get_band_for_weight(0.5).charge)
158
-        self.assertEqual(D('8.00'), WeightBand.get_band_for_weight(1.5).charge)
159
-        self.assertEqual(D('12.00'), WeightBand.get_band_for_weight(2.5).charge)
163
+        band = WeightBand.objects.create(method_code='standard', upper_limit=2, charge=D('8.00'))
164
+        self.assertEqual(1, band.weight_from)
165
+
166
+    def test_weight_from_for_multiple_bands(self):
167
+        WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
168
+        band = WeightBand.objects.create(method_code='express', upper_limit=2, charge=D('8.00'))
169
+        self.assertEqual(0, band.weight_from)
170
+
171
+    def test_get_band_for_series_of_bands(self):
172
+        WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
173
+        WeightBand.objects.create(method_code='standard', upper_limit=2, charge=D('8.00'))
174
+        WeightBand.objects.create(method_code='standard', upper_limit=3, charge=D('12.00'))
175
+        self.assertEqual(D('4.00'), WeightBand.get_band_for_weight('standard', 0.5).charge)
176
+        self.assertEqual(D('8.00'), WeightBand.get_band_for_weight('standard', 1.5).charge)
177
+        self.assertEqual(D('12.00'), WeightBand.get_band_for_weight('standard', 2.5).charge)
178
+
179
+    def test_get_band_for_series_of_bands_from_different_methods(self):
180
+        WeightBand.objects.create(method_code='standard', upper_limit=1, charge=D('4.00'))
181
+        WeightBand.objects.create(method_code='express', upper_limit=2, charge=D('8.00'))
182
+        WeightBand.objects.create(method_code='standard', upper_limit=3, charge=D('12.00'))
183
+        self.assertEqual(D('12.00'), WeightBand.get_band_for_weight('standard', 2.5).charge)

Loading…
Cancel
Save