Sfoglia il codice sorgente

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

master
Martijn Jacobs 4 anni fa
parent
commit
0995c5d2c7
Nessun account collegato all'indirizzo email del committer

+ 7
- 0
src/oscar/apps/catalogue/product_attributes.py Vedi File

@@ -9,6 +9,10 @@ class ProductAttributesContainer:
9 9
     To set attributes on a product, use the `attr` attribute:
10 10
 
11 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 18
     def __setstate__(self, state):
@@ -16,6 +20,9 @@ class ProductAttributesContainer:
16 20
 
17 21
     def __init__(self, product):
18 22
         self.product = product
23
+        self.refresh()
24
+
25
+    def refresh(self):
19 26
         values = self.get_values().select_related('attribute')
20 27
         for v in values:
21 28
             setattr(self, v.attribute.code, v.value)

+ 15
- 1
tests/integration/catalogue/test_attributes.py Vedi File

@@ -4,7 +4,7 @@ from django.core.exceptions import ValidationError
4 4
 from django.core.files.uploadedfile import SimpleUploadedFile
5 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 8
 from oscar.test import factories
9 9
 
10 10
 
@@ -30,6 +30,20 @@ class TestContainer(TestCase):
30 30
         assert product.attr.a1 == "v2"
31 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 48
 class TestBooleanAttributes(TestCase):
35 49
 

Loading…
Annulla
Salva