Skip to content

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

Refactor core.utils.generate_seo_description to take a const int instead of a field

wuwu86fb1eeb5119
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 2f420c888e..dc3c47532f 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -270,13 +270,11 @@ def get_or_create_collection(name, **kwargs):
def create_product(**kwargs):
description = fake.paragraphs(5)
- seo_description_field = Product._meta.get_field('seo_description')
defaults = {
'name': fake.company(),
'price': fake.money(),
'description': '\n\n'.join(description),
- 'seo_description': generate_seo_description(
- description[0], seo_description_field)}
+ 'seo_description': generate_seo_description(description[0], 300)}
defaults.update(kwargs)
return Product.objects.create(**defaults)
diff --git a/saleor/core/utils/text.py b/saleor/core/utils/text.py
index 7869a65886..b146881fee 100644
--- a/saleor/core/utils/text.py
+++ b/saleor/core/utils/text.py
@@ -53,9 +53,9 @@ def strip_html(text: str, **serializer_kwargs: bool):
return text
-def generate_seo_description(html_text: str, target_field: fields.CharField):
+def generate_seo_description(html_text: str, max_length: int):
"""Strips HTML tags and whitespaces from text,
then trim the description."""
text = strip_html(html_text, strip_whitespace=True)
- text = truncatechars(text, target_field.max_length)
+ text = truncatechars(text, max_length)
return text
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 2918cad6ab..65df6755c5 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -182,8 +182,7 @@ class ProductForm(forms.ModelForm):
product_description = self.data['description']
# generate a SEO friendly from HTML description
- seo_description = generate_seo_description(
- product_description, self.fields['seo_description'])
+ seo_description = generate_seo_description(product_description, 300)
return seo_description
diff --git a/saleor/product/migrations/0053_product_seo_description.py b/saleor/product/migrations/0053_product_seo_description.py
index a937e604f4..15c29c832a 100644
--- a/saleor/product/migrations/0053_product_seo_description.py
+++ b/saleor/product/migrations/0053_product_seo_description.py
@@ -6,23 +6,20 @@ from django.db import migrations, models
from saleor.core.utils.text import generate_seo_description
-def to_seo_friendly(cls, text):
+def to_seo_friendly(text):
# saleor descriptions are stored as escaped HTML,
# we need to decode them before processing them
text = html.unescape(text)
# cleanup the description and make it seo friendly
- return generate_seo_description(text, cls._meta.get_field(
- 'seo_description'))
+ return generate_seo_description(text, 300)
def assign_seo_descriptions(apps, schema_editor):
Product = apps.get_model('product', 'Product')
for product in Product.objects.all():
if product.seo_description is None:
- product.seo_description = to_seo_friendly(
- Product,
- product.description)
+ product.seo_description = to_seo_friendly(product.description)
product.save()