Pārlūkot izejas kodu

Entity attribute update (#4205)

* Product attribute entities can be created in bulk, but not updated in bulk

* Added test for delete also
master
Voxin Muyli 1 gadu atpakaļ
vecāks
revīzija
fa78d2c235
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam

+ 4
- 0
src/oscar/apps/catalogue/abstract_models.py Parādīt failu

@@ -968,6 +968,10 @@ class AbstractProductAttribute(models.Model):
968 968
     def is_file(self):
969 969
         return self.type in [self.FILE, self.IMAGE]
970 970
 
971
+    @property
972
+    def is_entity(self):
973
+        return self.type == self.ENTITY
974
+
971 975
     def __str__(self):
972 976
         return self.name
973 977
 

+ 5
- 0
src/oscar/apps/catalogue/product_attributes.py Parādīt failu

@@ -254,6 +254,11 @@ class ProductAttributesContainer:
254 254
                         if bound_value_obj is None:
255 255
                             to_be_deleted.append(value_obj.pk)
256 256
                         else:
257
+                            if bound_value_obj.attribute.is_entity:
258
+                                # entities can be bulk_created, but not bulk_saved
259
+                                bound_value_obj.save()
260
+                                continue
261
+
257 262
                             if bound_value_obj.attribute.is_file:
258 263
                                 # with bulk_create the file is save just fine, but
259 264
                                 # with buld_update, it's not, so we have to performa

+ 43
- 0
tests/unit/catalogue/test_product_attributes.py Parādīt failu

@@ -6,6 +6,7 @@ from django.test import TestCase
6 6
 
7 7
 from oscar.core.loading import get_model
8 8
 from oscar.test.factories import (
9
+    PartnerFactory,
9 10
     ProductAttributeFactory,
10 11
     ProductClassFactory,
11 12
     ProductFactory,
@@ -289,6 +290,48 @@ class ProductAttributeTest(TestCase):
289 290
         self.assertEqual(html, "<h1>Hi</h1>")
290 291
         self.assertTrue(hasattr(html, "__html__"))
291 292
 
293
+    def test_entity_attributes(self):
294
+        unrelated_object = PartnerFactory()
295
+        _ = ProductAttributeFactory(
296
+            type="entity",
297
+            product_class=self.product_class,
298
+            name="entity",
299
+            code="entity",
300
+        )
301
+        self.product.attr.entity = unrelated_object
302
+        self.product.attr.weight = 3
303
+        self.product.save()
304
+
305
+        self.product.refresh_from_db()
306
+        self.assertEqual(self.product.attr.entity, unrelated_object)
307
+
308
+        another_product = ProductFactory(
309
+            title="Aother",
310
+            stockrecords=None,
311
+            product_class=self.product_class,
312
+            structure="standalone",
313
+            upc="henk1239",
314
+        )
315
+
316
+        self.product.attr.entity = another_product
317
+        self.product.attr.weight = 5
318
+        self.product.save()
319
+
320
+        self.product.refresh_from_db()
321
+        self.assertEqual(self.product.attr.entity, another_product)
322
+
323
+        self.product.attr.entity = None
324
+        self.product.save()
325
+
326
+        self.product.refresh_from_db()
327
+
328
+        self.assertEqual(self.product.attr.entity, None)
329
+
330
+        product = Product.objects.get(pk=self.product.pk)
331
+        with self.assertRaises(AttributeError):
332
+            # pylint: disable=pointless-statement
333
+            product.attr.entity
334
+
292 335
 
293 336
 class MultiOptionTest(TestCase):
294 337
     fixtures = ["productattributes"]

Notiek ielāde…
Atcelt
Saglabāt