Bläddra i källkod

Add method to allow refresh of product attribute values from the database (#3644)

master
Martijn Jacobs 4 år sedan
förälder
incheckning
0995c5d2c7
Inget konto är kopplat till bidragsgivarens mejladress

+ 7
- 0
src/oscar/apps/catalogue/product_attributes.py Visa fil

9
     To set attributes on a product, use the `attr` attribute:
9
     To set attributes on a product, use the `attr` attribute:
10
 
10
 
11
         product.attr.weight = 125
11
         product.attr.weight = 125
12
+
13
+    To refetch the attribute values from the database:
14
+
15
+        product.attr.refresh()
12
     """
16
     """
13
 
17
 
14
     def __setstate__(self, state):
18
     def __setstate__(self, state):
16
 
20
 
17
     def __init__(self, product):
21
     def __init__(self, product):
18
         self.product = product
22
         self.product = product
23
+        self.refresh()
24
+
25
+    def refresh(self):
19
         values = self.get_values().select_related('attribute')
26
         values = self.get_values().select_related('attribute')
20
         for v in values:
27
         for v in values:
21
             setattr(self, v.attribute.code, v.value)
28
             setattr(self, v.attribute.code, v.value)

+ 15
- 1
tests/integration/catalogue/test_attributes.py Visa fil

4
 from django.core.files.uploadedfile import SimpleUploadedFile
4
 from django.core.files.uploadedfile import SimpleUploadedFile
5
 from django.test import TestCase
5
 from django.test import TestCase
6
 
6
 
7
-from oscar.apps.catalogue.models import Product
7
+from oscar.apps.catalogue.models import Product, ProductAttribute
8
 from oscar.test import factories
8
 from oscar.test import factories
9
 
9
 
10
 
10
 
30
         assert product.attr.a1 == "v2"
30
         assert product.attr.a1 == "v2"
31
         assert product.attr.a3 == "v6"
31
         assert product.attr.a3 == "v6"
32
 
32
 
33
+    def test_attributes_refresh(self):
34
+        product_class = factories.ProductClassFactory()
35
+        product_class.attributes.create(name='a1', code='a1', required=True)
36
+        product = factories.ProductFactory(product_class=product_class)
37
+        product.attr.a1 = "v1"
38
+        product.attr.save()
39
+
40
+        product_attr = ProductAttribute.objects.get(code="a1", product=product)
41
+        product_attr.save_value(product, "v2")
42
+        assert product.attr.a1 == "v1"
43
+
44
+        product.attr.refresh()
45
+        assert product.attr.a1 == "v2"
46
+
33
 
47
 
34
 class TestBooleanAttributes(TestCase):
48
 class TestBooleanAttributes(TestCase):
35
 
49
 

Laddar…
Avbryt
Spara