Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add more information to `AttributeValue` translations webhook. (#7783)

Rafał Pitoń116a81ceab3c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c5a33db01e..b9fbea41cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -130,6 +130,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add date & date time attributes - #7500 by @piotrgrundas
- Add `withChoices` flag for Attribute type - #7733 by @dexon44
- Drop assigning cheapest shipping method in checkout - #7767 by @maarcingebala
+- Add `product_id`, `product_variant_id`, `attribute_id` and `page_id` when it's possible for `AttributeValue` translations webhook. - #7783 by @fowczarek
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/attribute/models/base.py b/saleor/attribute/models/base.py
index 4b440a9ba5..861cd1abab 100644
--- a/saleor/attribute/models/base.py
+++ b/saleor/attribute/models/base.py
@@ -253,3 +253,31 @@ class AttributeValueTranslation(Translation):
def get_translated_keys(self):
return {"name": self.name, "rich_text": self.rich_text}
+
+ def get_translation_context(self):
+ context = {}
+ attribute_value = self.attribute_value
+ attribute = attribute_value.attribute
+ context["attribute_id"] = attribute.id
+ if attribute.input_type in AttributeInputType.TYPES_WITH_UNIQUE_VALUES:
+ if attribute.type == AttributeType.PRODUCT_TYPE:
+ if assigned_variant_attribute_value := (
+ attribute_value.variantvalueassignment.first()
+ ):
+ if variant := assigned_variant_attribute_value.assignment.variant:
+ context["product_variant_id"] = variant.id
+ context["product_id"] = variant.product_id
+ elif assigned_product_attribute_value := (
+ attribute_value.productvalueassignment.first()
+ ):
+ if product_id := (
+ assigned_product_attribute_value.assignment.product_id
+ ):
+ context["product_id"] = product_id
+ elif attribute.type == AttributeType.PAGE_TYPE:
+ if assigned_page_attribute_value := (
+ attribute_value.pagevalueassignment.first()
+ ):
+ if page_id := assigned_page_attribute_value.assignment.page_id:
+ context["page_id"] = page_id
+ return context
diff --git a/saleor/core/utils/translations.py b/saleor/core/utils/translations.py
index d573f1fbd0..a6bf5f1db5 100644
--- a/saleor/core/utils/translations.py
+++ b/saleor/core/utils/translations.py
@@ -48,3 +48,6 @@ class Translation(models.Model):
raise NotImplementedError(
"Models extending Translation should implement get_translated_keys"
)
+
+ def get_translation_context(self) -> Dict[str, Any]:
+ return {}
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 860a12aef5..e51a75aabe 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -3577,6 +3577,51 @@ def translated_attribute_value(pink_attribute_value):
)
+def translated_page_unique_attribute_value(page, rich_text_attribute_page_type):
+ page_type = page.page_type
+ page_type.page_attributes.add(rich_text_attribute_page_type)
+ attribute_value = rich_text_attribute_page_type.values.first()
+ associate_attribute_values_to_instance(
+ page, rich_text_attribute_page_type, attribute_value
+ )
+ return AttributeValueTranslation.objects.create(
+ language_code="fr",
+ attribute_value=attribute_value,
+ rich_text=dummy_editorjs("French description."),
+ )
+
+
+def translated_product_unique_attribute_value(product, rich_text_attribute):
+ product_type = product.product_type
+ product_type.product_attributes.add(rich_text_attribute)
+ attribute_value = rich_text_attribute.values.first()
+ associate_attribute_values_to_instance(
+ product, rich_text_attribute, attribute_value
+ )
+ return AttributeValueTranslation.objects.create(
+ language_code="fr",
+ attribute_value=attribute_value,
+ rich_text=dummy_editorjs("French description."),
+ )
+
+
+def translated_variant_unique_attribute_value(variant, rich_text_attribute):
+ product_type = variant.product.product_type
+ product_type.variant_attributes.add(rich_text_attribute)
+ attribute_value = rich_text_attribute.values.first()
+ associate_attribute_values_to_instance(
+ variant, rich_text_attribute, attribute_value
+ )
+ return AttributeValueTranslation.objects.create(
+ language_code="fr",
+ attribute_value=attribute_value,
+ rich_text=dummy_editorjs("French description."),
+ )
+
+
@pytest.fixture
def voucher_translation_fr(voucher):
return VoucherTranslation.objects.create(
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 5d81a7ac4e..1963d15ccb 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Iterable, Optional
import graphene
from django.db.models import F, QuerySet
+from ..attribute.models import AttributeValueTranslation
from ..checkout.models import Checkout
from ..core.utils import build_absolute_uri
from ..core.utils.anonymization import (
@@ -612,6 +613,22 @@ def generate_sample_payload(event_name: str) -> Optional[dict]:
return json.loads(payload) if payload else None
+def process_translation_context(context):
+ additional_id_fields = [
+ ("product_id", "Product"),
+ ("product_variant_id", "ProductVariant"),
+ ("attribute_id", "Attribute"),
+ ("page_id", "Page"),
+ ]
+ result = {}
+ for key, type_name in additional_id_fields:
+ if object_id := context.get(key, None):
+ result[key] = graphene.Node.to_global_id(type_name, object_id)
+ else:
+ result[key] = None
+ return result
+
+
def generate_translation_payload(translation: "Translation"):
object_type, object_id = translation.get_translated_object_id()
translated_keys = [
@@ -619,11 +636,18 @@ def generate_translation_payload(translation: "Translation"):
for key, value in translation.get_translated_keys().items()
]
+ context = None
+ if isinstance(translation, AttributeValueTranslation):
+ context = process_translation_context(translation.get_translation_context())
+
translation_data = {
"id": graphene.Node.to_global_id(object_type, object_id),
- "type": object_type,
"language_code": translation.language_code,
+ "type": object_type,
"keys": translated_keys,
}
+ if context:
+ translation_data.update(context)
+
return json.dumps(translation_data)
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index dc2304640f..957f75be5f 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -383,6 +383,10 @@ def test_generate_product_translation_payload(product_translation_fr):
"Product", product_translation_fr.product_id
)
assert data["language_code"] == product_translation_fr.language_code
+ assert "product_id" not in data.keys()
+ assert "product_variant_id" not in data.keys()
+ assert "attribute_id" not in data.keys()
+ assert "page_id" not in data.keys()
translation_keys = {i["key"]: i["value"] for i in data["keys"]}
assert translation_keys["name"] == product_translation_fr.name
@@ -396,11 +400,101 @@ def test_generate_product_variant_translation_payload(variant_translation_fr):
"ProductVariant", variant_translation_fr.product_variant_id
)
assert data["language_code"] == variant_translation_fr.language_code
+ assert "product_id" not in data.keys()
+ assert "product_variant_id" not in data.keys()
+ assert "attribute_id" not in data.keys()
+ assert "page_id" not in data.keys()
translation_keys = {i["key"]: i["value"] for i in data["keys"]}
assert translation_keys["name"] == variant_translation_fr.name
+def test_generate_choices_attribute_value_translation_payload(
+ translated_attribute_value, color_attribute
+):
+ payload = generate_translation_payload(translated_attribute_value)
+ data = json.loads(payload)
+ assert data["id"] == graphene.Node.to_global_id(
+ "AttributeValue", translated_attribute_value.attribute_value_id
+ )
+ assert data["language_code"] == translated_attribute_value.language_code
+ assert data["product_id"] is None
+ assert data["product_variant_id"] is None
+ assert data["attribute_id"] == graphene.Node.to_global_id(
+ "Attribute", color_attribute.id
+ )
+ assert data["page_id"] is None
+
+ translation_keys = {i["key"]: i["value"] for i in data["keys"]}
+ assert translation_keys["name"] == translated_attribute_value.name
+
+
+def test_generate_unique_product_attribute_value_translation_payload(
+ translated_product_unique_attribute_value, product, rich_text_attribute
+):
+ translated_attribute_value = translated_product_unique_attribute_value
+ payload = generate_translation_payload(translated_attribute_value)
+ data = json.loads(payload)
+ assert data["id"] == graphene.Node.to_global_id(
+ "AttributeValue", translated_attribute_value.attribute_value_id
+ )
+ assert data["language_code"] == translated_attribute_value.language_code
+ assert data["product_id"] == graphene.Node.to_global_id("Product", product.id)
+ assert data["product_variant_id"] is None
+ assert data["attribute_id"] == graphene.Node.to_global_id(
+ "Attribute", rich_text_attribute.id
+ )
+ assert data["page_id"] is None
+ translation_keys = {i["key"]: i["value"] for i in data["keys"]}
+ assert translation_keys["rich_text"] == translated_attribute_value.rich_text
+
+
+def test_generate_unique_variant_attribute_value_translation_payload(
+ translated_variant_unique_attribute_value, variant, rich_text_attribute
+):
+ translated_attribute_value = translated_variant_unique_attribute_value
+ payload = generate_translation_payload(translated_attribute_value)
+ data = json.loads(payload)
+ assert data["id"] == graphene.Node.to_global_id(
+ "AttributeValue", translated_attribute_value.attribute_value_id
+ )
+ assert data["language_code"] == translated_attribute_value.language_code
+ assert data["product_id"] == graphene.Node.to_global_id(
+ "Product", variant.product_id
+ )
+ assert data["product_variant_id"] == graphene.Node.to_global_id(
+ "ProductVariant", variant.id
+ )
+ assert data["attribute_id"] == graphene.Node.to_global_id(
+ "Attribute", rich_text_attribute.id
+ )
+ assert data["page_id"] is None
+ translation_keys = {i["key"]: i["value"] for i in data["keys"]}
+ assert translation_keys["rich_text"] == translated_attribute_value.rich_text
+
+
+def test_generate_unique_page_attribute_value_translation_payload(
+ translated_page_unique_attribute_value,
+ page,
+ rich_text_attribute_page_type,
+):
+ translated_attribute_value = translated_page_unique_attribute_value
+ payload = generate_translation_payload(translated_attribute_value)
+ data = json.loads(payload)
+ assert data["id"] == graphene.Node.to_global_id(
+ "AttributeValue", translated_attribute_value.attribute_value_id
+ )
+ assert data["language_code"] == translated_attribute_value.language_code
+ assert data["product_id"] is None
+ assert data["product_variant_id"] is None
+ assert data["attribute_id"] == graphene.Node.to_global_id(
+ "Attribute", rich_text_attribute_page_type.id
+ )
+ assert data["page_id"] == graphene.Node.to_global_id("Page", page.id)
+ translation_keys = {i["key"]: i["value"] for i in data["keys"]}
+ assert translation_keys["rich_text"] == translated_attribute_value.rich_text
+
+
def test_generate_customer_payload(customer_user, address_other_country, address):
customer = customer_user