From 49742e22655b479a934e8a1c95fc66564e344b52 Mon Sep 17 00:00:00 2001 From: Lars van de Kerkhof Date: Thu, 28 Sep 2023 16:18:32 +0200 Subject: [PATCH] I moet peopen --- oscarapi/serializers/admin/product.py | 4 ++- oscarapi/serializers/product.py | 38 ++++++++++++++++++--------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/oscarapi/serializers/admin/product.py b/oscarapi/serializers/admin/product.py index ee2869c0..1fa5842a 100644 --- a/oscarapi/serializers/admin/product.py +++ b/oscarapi/serializers/admin/product.py @@ -145,12 +145,14 @@ def update(self, instance, validated_data): if ( self.partial ): # we need to clean up all the attributes with wrong product class + attribute_codes = product_class.attributes.values_list("code", flat=True) for attribute_value in instance.attribute_values.exclude( attribute__product_class=product_class ): code = attribute_value.attribute.code + print("moet je peopen", code, "harrie", pclass_option_codes) if ( - code in pclass_option_codes + code in attribute_codes ): # if the attribute exist also on the new product class, update the attribute attribute_value.attribute = product_class.attributes.get( code=code diff --git a/oscarapi/serializers/product.py b/oscarapi/serializers/product.py index 8f0636c3..02334d21 100644 --- a/oscarapi/serializers/product.py +++ b/oscarapi/serializers/product.py @@ -197,26 +197,30 @@ class Meta: class ProductAttributeValueListSerializer(UpdateListSerializer): def to_internal_value(self, data): productclasses = set() - attributes = set() + # attributes = set() for item in data: product_class, code = getitems(item, "product_class", "code") if product_class: productclasses.add(product_class) - if code: - attributes.add(code) + # if code: + # attributes.add(code) + print(productclasses) # if all attributes belong to the same productclass, everything is just # as expected and we can do an optimization by only resolving the productclass to the model instance and nothing else. - if len(productclasses) == 1: - (product_class,) = productclasses - pc = ProductClass.objects.get(name=product_class) - return [ - {"value": item["value"], "attribute": item["code"], "product_class": pc} - for item in data - ] - else: - return super().to_internal_value(self) + try: + if len(productclasses) == 1: + (product_class,) = productclasses + pc = ProductClass.objects.get(slug=product_class) + return [ + {"value": item["value"], "attribute": item["code"], "product_class": pc} + for item in data + ] + except ProductClass.DoesNotExist: + raise Exception("productclasses", "bestaat niet", productclasses) + + return super().to_internal_value(data) def get_value(self, dictionary): values = super(ProductAttributeValueListSerializer, self).get_value(dictionary) @@ -245,22 +249,30 @@ def update(self, instance, validated_data): _, product = self.get_name_and_rel_instance(instance) attr_codes = [] + product.attr.initialize() for validated_datum in validated_data: + print(validated_datum) # leave all the attribute saving to the ProductAttributesContainer instead # of the child serializers attribute, value = getitems(validated_datum, "attribute", "value") if hasattr( attribute, "code" ): # if the attribute is a model instance use the code + print(" i set dem pussyes") product.attr.set(attribute.code, value) attr_codes.append(attribute.code) else: + print(" set dem reten") product.attr.set(attribute, value) attr_codes.append(attribute) + # if we don't clear the dirty attributes all parent attributes + # are marked as explicitly set, so they will be copied to the + # child product. + product.attr._dirty.clear() product.attr.save() - return product.attr.get_values() + return list(product.attr.get_values().filter(attribute__code__in=attr_codes)) class ProductAttributeValueSerializer(OscarModelSerializer):