Skip to content

Commit

Permalink
Prevent name overwritting of Product Variants when Updating Product T…
Browse files Browse the repository at this point in the history
…ypes (saleor#15670)

* prevent overwrite of variant names if name exists already
  • Loading branch information
teddyondieki authored Apr 19, 2024
1 parent c5bc3ef commit d3fa53c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Don't raise InsufficientStock for track_inventory=False variants #15475 by @carlosa54
- DB performance improvements in attribute dataloaders - #15474 by @AjmalPonneth
- Calculate order promotions in draft orders - #15459 by @zedzior
- Prevent name overwriting of Product Variants when Updating Product Types - #15670 by @teddyondieki

# 3.19.0

Expand Down
5 changes: 3 additions & 2 deletions saleor/product/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ def _variants_in_batches(variants_qs):
def _update_variants_names(instance: ProductType, saved_attributes: Iterable):
"""Product variant names are created from names of assigned attributes.
After change in attribute value name, for all product variants using this
attributes we need to update the names.
After change in attribute value name, we update the names for all product variants
that lack names and use these attributes.
"""
initial_attributes = set(instance.variant_attributes.all())
attributes_changed = initial_attributes.intersection(saved_attributes)
if not attributes_changed:
return

variants = ProductVariant.objects.filter(
name="",
product__in=instance.products.all(),
product__product_type__variant_attributes__in=attributes_changed,
)
Expand Down
23 changes: 15 additions & 8 deletions saleor/product/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from django.utils import timezone
from faker import Faker

from ...discount import PromotionType, RewardValueType
from ...discount.models import Promotion, PromotionRule
Expand Down Expand Up @@ -230,17 +231,23 @@ def test_recalculate_discounted_price_for_products_task_re_trigger_task(
assert recalculate_discounted_price_for_products_task_mock.called


@patch("saleor.product.tasks._update_variants_names")
def test_update_variants_names(
update_variants_names_mock, product_type, size_attribute
):
def test_update_variants_names(product_variant_list, size_attribute):
# given
variant_without_name = product_variant_list[0]
variant_with_name = product_variant_list[1]
random_name = Faker().word()
variant_with_name.name = random_name
variant_with_name.save()
product = variant_without_name.product

# when
update_variants_names(product_type.id, [size_attribute.id])
update_variants_names(product.product_type_id, [size_attribute.id])

# then
args, _ = update_variants_names_mock.call_args
assert args[0] == product_type
assert {arg.pk for arg in args[1]} == {size_attribute.pk}
variant_without_name.refresh_from_db()
variant_with_name.refresh_from_db()
assert variant_without_name.name == variant_without_name.sku
assert variant_with_name.name == random_name


def test_update_variants_names_product_type_does_not_exist(caplog):
Expand Down

0 comments on commit d3fa53c

Please sign in to comment.