Kaynağa Gözat

Reworked the availability logic of the DefaultWrapper

master
David Winterbottom 13 yıl önce
ebeveyn
işleme
92221c4f68

+ 1
- 0
oscar/apps/basket/forms.py Dosyayı Görüntüle

@@ -145,6 +145,7 @@ class AddToBasketForm(forms.Form):
145 145
         """
146 146
         self.fields[option.code] = forms.CharField()
147 147
 
148
+
148 149
 class SimpleAddToBasketForm(AddToBasketForm):
149 150
     quantity = forms.IntegerField(initial=1, min_value=1, widget=forms.HiddenInput)
150 151
 

+ 7
- 1
oscar/apps/partner/abstract_models.py Dosyayı Görüntüle

@@ -142,7 +142,7 @@ class AbstractStockRecord(models.Model):
142 142
         """
143 143
         Return the effective number in stock.  This is correct property to show
144 144
         the customer, not the num_in_stock field as that doesn't account for
145
-        allocations.
145
+        allocations.  This can be negative in some unusual circumstances
146 146
         """
147 147
         if self.num_in_stock is None:
148 148
             return 0
@@ -197,6 +197,12 @@ class AbstractStockRecord(models.Model):
197 197
         Return an item's availability as a string
198 198
         """
199 199
         return get_partner_wrapper(self.partner.name).availability(self)
200
+
201
+    def max_purchase_quantity(self, user):
202
+        """
203
+        Return an item's availability as a string
204
+        """
205
+        return get_partner_wrapper(self.partner.name).availability(self)
200 206
     
201 207
     @property
202 208
     def dispatch_date(self):

+ 4
- 4
oscar/apps/partner/tests/models.py Dosyayı Görüntüle

@@ -1,7 +1,7 @@
1 1
 from decimal import Decimal as D
2 2
 import datetime
3 3
 
4
-from django.utils import unittest
4
+from django.test import TestCase
5 5
 
6 6
 from oscar.test.helpers import create_product
7 7
 from oscar.apps.partner.abstract_models import partner_wrappers
@@ -16,7 +16,7 @@ class DummyWrapper(object):
16 16
         return "Another dummy response"
17 17
 
18 18
 
19
-class StockRecordTests(unittest.TestCase):
19
+class StockRecordTests(TestCase):
20 20
 
21 21
     def setUp(self):
22 22
         self.product = create_product(price=D('10.00'), num_in_stock=10)
@@ -53,7 +53,7 @@ class StockRecordTests(unittest.TestCase):
53 53
         self.assertEqual(10, self.stockrecord.num_in_stock)
54 54
 
55 55
 
56
-class DefaultWrapperTests(unittest.TestCase):
56
+class DefaultWrapperTests(TestCase):
57 57
 
58 58
     def test_default_wrapper_for_in_stock(self):
59 59
         product = create_product(price=D('10.00'), partner="Acme", num_in_stock=10)
@@ -77,7 +77,7 @@ class DefaultWrapperTests(unittest.TestCase):
77 77
         self.assertEquals(date, product.stockrecord.dispatch_date)
78 78
 
79 79
 
80
-class CustomWrapperTests(unittest.TestCase):
80
+class CustomWrapperTests(TestCase):
81 81
 
82 82
     def setUp(self):
83 83
         partner_wrappers['Acme'] = DummyWrapper()

+ 30
- 0
oscar/apps/partner/tests/wrappers.py Dosyayı Görüntüle

@@ -37,3 +37,33 @@ class DefaultWrapperTests(TestCase):
37 37
         record = StockRecord(num_in_stock=4, product=self.product)
38 38
         result, reason = self.wrapper.is_purchase_permitted(record, quantity=5)
39 39
         self.assertFalse(result)
40
+
41
+    def test_max_purchase_quantity(self):
42
+        record  = StockRecord(num_in_stock=4, product=self.product)
43
+        self.assertEqual(record.num_in_stock, self.wrapper.max_purchase_quantity(record))
44
+
45
+    def test_availability_code_for_in_stock(self):
46
+        record  = StockRecord(num_in_stock=4, product=self.product)
47
+        self.assertEqual('instock', self.wrapper.availability_code(record))
48
+
49
+    def test_availability_code_for_zero_stock(self):
50
+        record  = StockRecord(num_in_stock=0, product=self.product)
51
+        self.assertEqual('outofstock', self.wrapper.availability_code(record))
52
+
53
+    def test_availability_code_for_null_stock_but_available(self):
54
+        record  = StockRecord(num_in_stock=None, product=self.product)
55
+        self.assertEqual('available', self.wrapper.availability_code(record))
56
+
57
+    def test_availability_message_for_in_stock(self):
58
+        record  = StockRecord(num_in_stock=4, product=self.product)
59
+        self.assertEqual(u'In stock (4 available)', unicode(self.wrapper.availability(record)))
60
+
61
+    def test_availability_message_for_available(self):
62
+        record  = StockRecord(num_in_stock=None, product=self.product)
63
+        self.assertEqual(u'Available', unicode(self.wrapper.availability(record)))
64
+
65
+    def test_availability_message_for_out_of_stock(self):
66
+        record  = StockRecord(num_in_stock=0, product=self.product)
67
+        self.assertEqual(u'Not available', unicode(self.wrapper.availability(record)))
68
+
69
+

+ 14
- 2
oscar/apps/partner/wrappers.py Dosyayı Görüntüle

@@ -24,24 +24,36 @@ class DefaultWrapper(object):
24 24
         Test whether a particular purchase is possible (is a user buying a given
25 25
         quantity of the product)
26 26
         """
27
-        if stockrecord.net_stock_level <= 0:
27
+        if not self.is_available_to_buy(stockrecord):
28 28
             return False, _("'%s' is not available to purchase" % stockrecord.product.title)
29 29
         if stockrecord.net_stock_level < quantity:
30 30
             return False, _("'%s' - A maximum of %d can be bought" % (
31 31
                 stockrecord.product.title, stockrecord.net_stock_level))
32 32
         return True, None
33 33
 
34
+    def max_purchase_quantity(self, stockrecord, user=None):
35
+        """
36
+        Return the maximum available purchase quantity for a given user
37
+        """
38
+        return stockrecord.net_stock_level
39
+
34 40
     def availability_code(self, stockrecord):
35 41
         """
36 42
         Return a code for the availability of this product.
37 43
 
38 44
         This is normally used within CSS to add icons to stock messages
39 45
         """
40
-        return 'instock' if stockrecord.net_stock_level > 0 else 'outofstock'
46
+        if stockrecord.net_stock_level > 0:
47
+            return 'instock'
48
+        if self.is_available_to_buy(stockrecord):
49
+            return 'available'
50
+        return 'outofstock'
41 51
     
42 52
     def availability(self, stockrecord):
43 53
         if stockrecord.net_stock_level > 0:
44 54
             return _("In stock (%d available)" % stockrecord.net_stock_level)
55
+        if self.is_available_to_buy(stockrecord):
56
+            return _('Available')
45 57
         return _("Not available")
46 58
     
47 59
     def dispatch_date(self, stockrecord):

+ 1
- 1
oscar/templates/catalogue/partials/product.html Dosyayı Görüntüle

@@ -33,9 +33,9 @@
33 33
                 <p class="app-ico {{ product.stockrecord.availability_code }} avaliability ">{{ product.stockrecord.availability|truncatewords:2 }}</p>
34 34
             {% else %}
35 35
                 <p class="app-ico avaliability outofstock">Not available</p>
36
-                <button class="btn">Notify me</button> 
37 36
             {% endif %}
38 37
     {% endif %}
38
+
39 39
     {% basket_form basket product as basket_form single %}
40 40
     <form action="{% url basket:add %}" method="post">
41 41
         {% csrf_token %}

Loading…
İptal
Kaydet