Skip to content

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

Fix saving product description_plaintext (#12586)

SzymJ8b9720cb915d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e725bd1968..6b2f7b80f0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Saleor Apps
### Other changes
+- Fix saving `description_plaintext` for product - #12586 by @SzymJ
- Remove default `EMAIL_URL` value pointing to console output; from now on EMAIL_URL has to be set explicitly - #12580 by @maarcingebala
diff --git a/saleor/graphql/product/mutations/product/product_create.py b/saleor/graphql/product/mutations/product/product_create.py
index da48d22bf9..efbdc87688 100644
--- a/saleor/graphql/product/mutations/product/product_create.py
+++ b/saleor/graphql/product/mutations/product/product_create.py
@@ -153,10 +153,11 @@ class ProductCreate(ModelMutation):
def clean_input(cls, info: ResolveInfo, instance, data, **kwargs):
cleaned_input = super().clean_input(info, instance, data, **kwargs)
- description = cleaned_input.get("description")
- cleaned_input["description_plaintext"] = (
- clean_editor_js(description, to_string=True) if description else ""
- )
+ if "description" in cleaned_input:
+ description = cleaned_input["description"]
+ cleaned_input["description_plaintext"] = (
+ clean_editor_js(description, to_string=True) if description else ""
+ )
weight = cleaned_input.get("weight")
if weight and weight.value < 0:
diff --git a/saleor/graphql/product/tests/mutations/test_product_update.py b/saleor/graphql/product/tests/mutations/test_product_update.py
index 61d7eeec0e..e87c25715b 100644
--- a/saleor/graphql/product/tests/mutations/test_product_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_update.py
@@ -216,9 +216,41 @@ def test_update_and_search_product_by_description(
assert data["product"]["description"] == other_description_json
-def test_update_product_without_description_clear_description_plaintext(
+def test_update_product_clear_description_plaintext_when_description_is_none(
+ staff_api_client,
+ non_default_category,
+ product,
+ other_description_json,
+ permission_manage_products,
+ color_attribute,
+):
+ query = MUTATION_UPDATE_PRODUCT
+ description_plaintext = "some desc"
+ product.description_plaintext = description_plaintext
+ product.save()
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ product_name = "updated name"
+
+ variables = {
+ "productId": product_id,
+ "input": {"name": product_name, "description": None},
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert not data["errors"]
+ assert data["product"]["name"] == product_name
+ assert data["product"]["description"] is None
+
+ product.refresh_from_db()
+ assert product.description_plaintext == ""
+
+
+def test_update_product_doesnt_clear_description_plaintext_when_no_description(
staff_api_client,
- category,
non_default_category,
product,
other_description_json,
@@ -230,16 +262,12 @@ def test_update_product_without_description_clear_description_plaintext(
product.description_plaintext = description_plaintext
product.save()
product_id = graphene.Node.to_global_id("Product", product.pk)
- category_id = graphene.Node.to_global_id("Category", non_default_category.pk)
product_name = "updated name"
- product_slug = "updated-product"
variables = {
"productId": product_id,
"input": {
- "category": category_id,
"name": product_name,
- "slug": product_slug,
},
}
@@ -250,11 +278,9 @@ def test_update_product_without_description_clear_description_plaintext(
data = content["data"]["productUpdate"]
assert not data["errors"]
assert data["product"]["name"] == product_name
- assert data["product"]["slug"] == product_slug
- assert data["product"]["description"] is None
product.refresh_from_db()
- assert product.description_plaintext == ""
+ assert product.description_plaintext == description_plaintext
@patch("saleor.plugins.manager.PluginsManager.product_updated")
diff --git a/saleor/product/migrations/0178_fix_description_plaintext_field.py b/saleor/product/migrations/0178_fix_description_plaintext_field.py
new file mode 100644
index 0000000000..34a48fd5ae
--- /dev/null
+++ b/saleor/product/migrations/0178_fix_description_plaintext_field.py
@@ -0,0 +1,53 @@
+# Generated by Django 3.2.18 on 2023-04-17 08:00
+
+from django.db import migrations
+from django.db.models import Q
+from ...core.utils.editorjs import clean_editor_js
+
+BATCH_SIZE = 2000
+
+
+def queryset_in_batches(queryset):
+ start_pk = 0
+
+ while True:
+ qs = queryset.order_by("pk").filter(pk__gt=start_pk)[:BATCH_SIZE]
+ pks = list(qs.values_list("pk", flat=True))
+
+ if not pks:
+ break
+
+ yield pks
+
+ start_pk = pks[-1]
+
+
+def set_description_plaintext_if_description_exists(apps, schema_editor):
+ Product = apps.get_model("product", "Product")
+ queryset = Product.objects.filter(
+ Q(description_plaintext="") & ~Q(description=None)
+ )
+
+ for batch_pks in queryset_in_batches(queryset):
+ products = Product.objects.filter(pk__in=batch_pks)
+ for product in products:
+ product.description_plaintext = clean_editor_js(
+ product.description, to_string=True
+ )
+ product.search_index_dirty = True
+
+ Product.objects.bulk_update(
+ products, ["description_plaintext", "search_index_dirty"]
+ )
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0177_product_tax_class_producttype_tax_class"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ set_description_plaintext_if_description_exists, migrations.RunPython.noop
+ )
+ ]
diff --git a/saleor/product/migrations/0180_merge_20230417_1143.py b/saleor/product/migrations/0180_merge_20230417_1143.py
new file mode 100644
index 0000000000..f1f4367ab9
--- /dev/null
+++ b/saleor/product/migrations/0180_merge_20230417_1143.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.18 on 2023-04-17 11:43
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0178_fix_description_plaintext_field"),
+ ("product", "0179_productvariant_external_reference"),
+ ]
+
+ operations = []
diff --git a/saleor/product/migrations/0181_merge_20230417_1323.py b/saleor/product/migrations/0181_merge_20230417_1323.py
new file mode 100644
index 0000000000..4f581cb707
--- /dev/null
+++ b/saleor/product/migrations/0181_merge_20230417_1323.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.18 on 2023-04-17 13:23
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0180_merge_20230417_1143"),
+ ("product", "0180_productmedia_metadata"),
+ ]
+
+ operations = []