Skip to content

Commit

Permalink
I moet peopen
Browse files Browse the repository at this point in the history
  • Loading branch information
specialunderwear committed Sep 28, 2023
1 parent 8aa79ff commit 49742e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion oscarapi/serializers/admin/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 25 additions & 13 deletions oscarapi/serializers/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 49742e2

Please sign in to comment.