Pārlūkot izejas kodu

Reworked the availability logic of the DefaultWrapper

master
David Winterbottom 13 gadus atpakaļ
vecāks
revīzija
92221c4f68

+ 1
- 0
oscar/apps/basket/forms.py Parādīt failu

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

+ 7
- 1
oscar/apps/partner/abstract_models.py Parādīt failu

142
         """
142
         """
143
         Return the effective number in stock.  This is correct property to show
143
         Return the effective number in stock.  This is correct property to show
144
         the customer, not the num_in_stock field as that doesn't account for
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
         if self.num_in_stock is None:
147
         if self.num_in_stock is None:
148
             return 0
148
             return 0
197
         Return an item's availability as a string
197
         Return an item's availability as a string
198
         """
198
         """
199
         return get_partner_wrapper(self.partner.name).availability(self)
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
     @property
207
     @property
202
     def dispatch_date(self):
208
     def dispatch_date(self):

+ 4
- 4
oscar/apps/partner/tests/models.py Parādīt failu

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

+ 30
- 0
oscar/apps/partner/tests/wrappers.py Parādīt failu

37
         record = StockRecord(num_in_stock=4, product=self.product)
37
         record = StockRecord(num_in_stock=4, product=self.product)
38
         result, reason = self.wrapper.is_purchase_permitted(record, quantity=5)
38
         result, reason = self.wrapper.is_purchase_permitted(record, quantity=5)
39
         self.assertFalse(result)
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 Parādīt failu

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

+ 1
- 1
oscar/templates/catalogue/partials/product.html Parādīt failu

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

Notiek ielāde…
Atcelt
Saglabāt