Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Allow translating rich-text attributes values (#7700)
Dawid Matuszczyk9be3f964b960
diff --git a/saleor/attribute/__init__.py b/saleor/attribute/__init__.pyindex 35f99d668f..89d7be65bb 100644--- a/saleor/attribute/__init__.py+++ b/saleor/attribute/__init__.py@@ -38,6 +38,11 @@ class AttributeInputType:NUMERIC,]+ # list of the translatable attributes, excluding attributes with choices.+ TRANSLATABLE_ATTRIBUTES = [+ RICH_TEXT,+ ]+# list of input types that are allowed for given attribute propertyATTRIBUTE_PROPERTIES_CONFIGURATION = {diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex bd5bb75234..12348cf7a2 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -666,6 +666,7 @@ input AttributeValueInput {type AttributeValueTranslatableContent implements Node {id: ID!name: String!+ richText: JSONStringtranslation(languageCode: LanguageCodeEnum!): AttributeValueTranslationattributeValue: AttributeValue @deprecated(reason: "Will be removed in Saleor 4.0. Get model fields from the root level.")}@@ -3677,6 +3678,7 @@ type PageTranslatableContent implements Node {contentJson: JSONString @deprecated(reason: "Will be removed in Saleor 4.0. Use the `content` field instead.")translation(languageCode: LanguageCodeEnum!): PageTranslationpage: Page @deprecated(reason: "Will be removed in Saleor 4.0. Get model fields from the root level.")+ attributeValues: [AttributeValueTranslatableContent!]!}type PageTranslate {@@ -4423,6 +4425,7 @@ type ProductTranslatableContent implements Node {descriptionJson: JSONString @deprecated(reason: "Will be removed in Saleor 4.0. Use the `description` field instead.")translation(languageCode: LanguageCodeEnum!): ProductTranslationproduct: Product @deprecated(reason: "Will be removed in Saleor 4.0. Get model fields from the root level.")+ attributeValues: [AttributeValueTranslatableContent!]!}type ProductTranslate {@@ -4697,6 +4700,7 @@ type ProductVariantTranslatableContent implements Node {name: String!translation(languageCode: LanguageCodeEnum!): ProductVariantTranslationproductVariant: ProductVariant @deprecated(reason: "Will be removed in Saleor 4.0. Get model fields from the root level.")+ attributeValues: [AttributeValueTranslatableContent!]!}type ProductVariantTranslate {diff --git a/saleor/graphql/translations/tests/test_translations.py b/saleor/graphql/translations/tests/test_translations.pyindex b0fc66a795..8bc8ea18bf 100644--- a/saleor/graphql/translations/tests/test_translations.py+++ b/saleor/graphql/translations/tests/test_translations.py@@ -2308,3 +2308,160 @@ def test_product_and_attribute_translation(user_api_client, product, channel_USD]assert attribute_translation_data["name"] == "Kolor"assert attribute_translation_data["language"]["code"] == "PL"+++def test_product_attribute_value_rich_text_translation(+ staff_api_client,+ product_with_rich_text_attribute,+ permission_manage_translations,+):+ rich_text = dummy_editorjs("Test_dummy_data")+ assigned_attribute = product_with_rich_text_attribute[0].attributes.first()+ attribute_value = assigned_attribute.attribute.values.first()+ attribute_value.translations.create(language_code="pl", rich_text=rich_text)++ product_id = graphene.Node.to_global_id(+ "Product", product_with_rich_text_attribute[0].id+ )++ query = """+ query translation(+ $kind: TranslatableKinds!+ $id: ID!+ $languageCode: LanguageCodeEnum!+ ) {+ translation(kind: $kind, id: $id) {+ ... on ProductTranslatableContent {+ name+ attributeValues {+ name+ richText+ translation(languageCode: $languageCode) {+ name+ richText+ }+ }+ }+ }+ }+ """+ variables = {+ "id": product_id,+ "kind": TranslatableKinds.PRODUCT.name,+ "languageCode": LanguageCodeEnum.PL.name,+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_translations]+ )+ data = get_graphql_content(response)["data"]++ attribute_value_response = data["translation"]["attributeValues"][0]+ assert attribute_value_response["name"] == attribute_value.name+ assert attribute_value_response["richText"] == json.dumps(attribute_value.rich_text)+ assert attribute_value_response["translation"]["richText"] == json.dumps(rich_text)+++def test_product_variant_attribute_value_rich_text_translation(+ staff_api_client,+ product_with_rich_text_attribute,+ permission_manage_translations,+ product_type_with_rich_text_attribute,+):+ rich_text = dummy_editorjs("Test_dummy_data")+ variant_attr = product_type_with_rich_text_attribute.variant_attributes.first()+ attribute_value = variant_attr.values.first()+ attribute_value.translations.create(language_code="pl", rich_text=rich_text)++ variant_id = graphene.Node.to_global_id(+ "ProductVariant", product_with_rich_text_attribute[1].id+ )++ query = """+ query translation(+ $kind: TranslatableKinds!+ $id: ID!+ $languageCode: LanguageCodeEnum!+ ) {+ translation(kind: $kind, id: $id) {+ ... on ProductVariantTranslatableContent {+ name+ attributeValues {+ name+ richText+ translation(languageCode: $languageCode) {+ name+ richText+ }+ }+ }+ }+ }+ """+ variables = {+ "id": variant_id,+ "kind": TranslatableKinds.VARIANT.name,+ "languageCode": LanguageCodeEnum.PL.name,+ }++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_translations]+ )+ data = get_graphql_content(response)["data"]++ translations_response = data["translation"]["attributeValues"][0]+ assert translations_response["name"] == attribute_value.name+ assert translations_response["richText"] == json.dumps(attribute_value.rich_text)+ assert translations_response["translation"]["richText"] == json.dumps(rich_text)+++def test_page_attribute_value_rich_text_translation(+ staff_api_client,+ page_with_rich_text_attribute,+ permission_manage_translations,+ page_type_with_rich_text_attribute,+ permission_manage_pages,+):+ rich_text = dummy_editorjs("Test_dummy_data")+ variant_attr = page_type_with_rich_text_attribute.page_attributes.first()+ attribute_value = variant_attr.values.first()+ attribute_value.translations.create(language_code="pl", rich_text=rich_text)++ page_id = graphene.Node.to_global_id("Page", page_with_rich_text_attribute.id)++ query = """+ query translation(+ $kind: TranslatableKinds!+ $id: ID!+ $languageCode: LanguageCodeEnum!+ ) {+ translation(kind: $kind, id: $id) {+ ... on PageTranslatableContent {+ attributeValues {+ name+ richText+ translation(languageCode: $languageCode) {+ name+ richText+ }+ }+ }+ }+ }++ """++ variables = {+ "id": page_id,+ "kind": TranslatableKinds.PAGE.name,+ "languageCode": LanguageCodeEnum.PL.name,+ }+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_translations]+ )+ data = get_graphql_content(response)["data"]++ attribute_value_response = data["translation"]["attributeValues"][0]+ assert attribute_value_response["name"] == attribute_value.name+ assert attribute_value_response["richText"] == json.dumps(attribute_value.rich_text)+ assert attribute_value_response["translation"]["richText"] == json.dumps(rich_text)diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.pyindex a7b092d198..41eb91162b 100644--- a/saleor/graphql/translations/types.py+++ b/saleor/graphql/translations/types.py@@ -1,6 +1,7 @@import graphenefrom django.conf import settings+from ...attribute import AttributeInputTypefrom ...attribute import models as attribute_modelsfrom ...core.permissions import DiscountPermissions, ShippingPermissionsfrom ...core.tracing import traced_resolver@@ -74,7 +75,7 @@ class AttributeValueTranslatableContent(CountableDjangoObjectType):class Meta:model = attribute_models.AttributeValueinterfaces = [graphene.relay.Node]- only_fields = BASIC_TRANSLATABLE_FIELDS+ only_fields = BASIC_TRANSLATABLE_FIELDS + ["rich_text"]@staticmethoddef resolve_attribute_value(root: attribute_models.AttributeValue, _info):@@ -128,6 +129,11 @@ class ProductVariantTranslatableContent(CountableDjangoObjectType):"Will be removed in Saleor 4.0. " "Get model fields from the root level."),)+ attribute_values = graphene.List(+ graphene.NonNull(AttributeValueTranslatableContent),+ required=True,+ description="List of product variant attribute values that can be translated.",+ )class Meta:model = product_models.ProductVariant@@ -138,6 +144,16 @@ class ProductVariantTranslatableContent(CountableDjangoObjectType):def resolve_product_variant(root: product_models.ProductVariant, info):return ChannelContext(node=root, channel_slug=None)+ @staticmethod+ def resolve_attribute_values(root: product_models.ProductVariant, _info):+ translatable_values = []+ for assignment in root.attributes.all():+ attr = assignment.attribute+ if attr.input_type in AttributeInputType.TRANSLATABLE_ATTRIBUTES:+ value = assignment.values.first()+ translatable_values.append(value)+ return translatable_values+class ProductTranslation(BaseTranslationType):description_json = graphene.JSONString(@@ -173,6 +189,11 @@ class ProductTranslatableContent(CountableDjangoObjectType):"Will be removed in Saleor 4.0. " "Get model fields from the root level."),)+ attribute_values = graphene.List(+ graphene.NonNull(AttributeValueTranslatableContent),+ required=True,+ description="List of product attribute values that can be translated.",+ )class Meta:model = product_models.Product@@ -188,6 +209,16 @@ class ProductTranslatableContent(CountableDjangoObjectType):description = root.descriptionreturn description if description is not None else {}+ @staticmethod+ def resolve_attribute_values(root: product_models.Product, _info):+ translatable_values = []+ for assignment in root.attributes.all():+ attr = assignment.attribute+ if attr.input_type in AttributeInputType.TRANSLATABLE_ATTRIBUTES:+ value = assignment.values.first()+ translatable_values.append(value)+ return translatable_values+class CollectionTranslation(BaseTranslationType):description_json = graphene.JSONString(@@ -335,6 +366,11 @@ class PageTranslatableContent(CountableDjangoObjectType):"Will be removed in Saleor 4.0. " "Get model fields from the root level."),)+ attribute_values = graphene.List(+ graphene.NonNull(AttributeValueTranslatableContent),+ required=True,+ description="List of page content attribute values that can be translated.",+ )class Meta:model = page_models.Page@@ -360,6 +396,16 @@ class PageTranslatableContent(CountableDjangoObjectType):content = root.contentreturn content if content is not None else {}+ @staticmethod+ def resolve_attribute_values(root: page_models.Page, _info):+ translatable_values = []+ for assignment in root.attributes.all():+ attr = assignment.attribute+ if attr.input_type in AttributeInputType.TRANSLATABLE_ATTRIBUTES:+ value = assignment.values.first()+ translatable_values.append(value)+ return translatable_values+class VoucherTranslation(BaseTranslationType):class Meta:diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 83efacfb00..3890818719 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -913,6 +913,27 @@ def rich_text_attribute(db):return attribute+def rich_text_attribute_page_type(db):+ attribute = Attribute.objects.create(+ slug="text",+ name="Text",+ type=AttributeType.PAGE_TYPE,+ input_type=AttributeInputType.RICH_TEXT,+ filterable_in_storefront=False,+ filterable_in_dashboard=False,+ available_in_grid=False,+ )+ text = "Rich text attribute content."+ AttributeValue.objects.create(+ attribute=attribute,+ name=truncatechars(clean_editor_js(dummy_editorjs(text), to_string=True), 50),+ slug=f"instance_{attribute.id}",+ rich_text=dummy_editorjs(text),+ )+ return attribute++@pytest.fixturedef rich_text_attribute_with_many_values(rich_text_attribute):attribute = rich_text_attribute@@ -1328,6 +1349,21 @@ def product_type(color_attribute, size_attribute):return product_type+def product_type_with_rich_text_attribute(+ rich_text_attribute, color_attribute, size_attribute+):+ product_type = ProductType.objects.create(+ name="Default Type",+ slug="default-type",+ has_variants=True,+ is_shipping_required=True,+ )+ product_type.product_attributes.add(rich_text_attribute)+ product_type.variant_attributes.add(rich_text_attribute)+ return product_type++@pytest.fixturedef product_type_without_variant():product_type = ProductType.objects.create(@@ -1376,6 +1412,48 @@ def product(product_type, category, warehouse, channel_USD):return product+def product_with_rich_text_attribute(+ product_type_with_rich_text_attribute, category, warehouse, channel_USD+):+ product_attr = product_type_with_rich_text_attribute.product_attributes.first()+ product_attr_value = product_attr.values.first()++ product = Product.objects.create(+ name="Test product",+ slug="test-product-11",+ product_type=product_type_with_rich_text_attribute,+ category=category,+ )+ ProductChannelListing.objects.create(+ product=product,+ channel=channel_USD,+ is_published=True,+ discounted_price_amount="10.00",+ currency=channel_USD.currency_code,+ visible_in_listings=True,+ available_for_purchase=datetime.date(1999, 1, 1),+ )++ associate_attribute_values_to_instance(product, product_attr, product_attr_value)++ variant_attr = product_type_with_rich_text_attribute.variant_attributes.first()+ variant_attr_value = variant_attr.values.first()++ variant = ProductVariant.objects.create(product=product, sku="123")+ ProductVariantChannelListing.objects.create(+ variant=variant,+ channel=channel_USD,+ price_amount=Decimal(10),+ cost_price_amount=Decimal(1),+ currency=channel_USD.currency_code,+ )+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=10)++ associate_attribute_values_to_instance(variant, variant_attr, variant_attr_value)+ return [product, variant]++@pytest.fixturedef product_with_collections(product, published_collection, unpublished_collection, collection@@ -3278,6 +3356,26 @@ def page(db, page_type):return page+def page_with_rich_text_attribute(db, page_type_with_rich_text_attribute):+ data = {+ "slug": "test-url",+ "title": "Test page",+ "content": dummy_editorjs("Test content."),+ "is_published": True,+ "page_type": page_type_with_rich_text_attribute,+ }+ page = Page.objects.create(**data)++ # associate attribute value+ page_attr = page_type_with_rich_text_attribute.page_attributes.first()+ page_attr_value = page_attr.values.first()++ associate_attribute_values_to_instance(page, page_attr, page_attr_value)++ return page++@pytest.fixturedef page_list(db, page_type):data_1 = {@@ -3325,6 +3423,13 @@ def page_type(db, size_page_attribute, tag_page_attribute):return page_type+def page_type_with_rich_text_attribute(db, rich_text_attribute_page_type):+ page_type = PageType.objects.create(name="Test page type", slug="test-page-type")+ page_type.page_attributes.add(rich_text_attribute_page_type)+ return page_type++@pytest.fixturedef page_type_list(db, tag_page_attribute):page_types = list(