Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Made requested edits
NyanKiyoshi1256fb095050
diff --git a/saleor/core/utils/text.py b/saleor/core/utils/text.pyindex 211c16b9ba..aff77da9df 100644--- a/saleor/core/utils/text.py+++ b/saleor/core/utils/text.py@@ -1,34 +1,6 @@import bleach-from html5lib.serializer import HTMLSerializer---def trim_text(text: str, max_length: int, suffix: str=''):- """Trims a given text in accordance to parameters.- If text length is greater than allowed,- it trims it and add a given suffix to the text.-- Note that the text + suffix is equal to the max length.-- Example:- max length = 16- prefix = '[...]' (5 chars)- text = 'Hello World, everyone!' (22 chars)-- => text = 'Hello World[...]'-- :param text:- :type text: str- :param max_length:- :type max_length: int- :param suffix:- :type suffix: str- :rtype: str- """- if len(text) > max_length:- new_length = max_length - len(suffix)- text = '%s%s' % (text[:new_length], suffix)- return text+from html5lib.serializer import HTMLSerializerdef get_cleaner(**serializer_kwargs: bool):@@ -76,3 +48,11 @@ def strip_html(text: str, **serializer_kwargs: bool):cleaner = get_cleaner(**serializer_kwargs)text = cleaner.clean(text)return text+++def generate_seo_description(html_text):+ """Strips HTML tags and whitespaces from text,+ then trim the description."""+ text = strip_html(html_text, strip_whitespace=True)+ text = text[:300]+ return textdiff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex 76f4a9ca74..17911d60d3 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -9,7 +9,6 @@ from django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom . import ProductBulkAction-from ...core.utils.text import strip_html, trim_textfrom ...product.models import (AttributeChoiceValue, Collection, Product, ProductAttribute, ProductImage,ProductType, ProductVariant, Stock, StockLocation, VariantImage)@@ -170,16 +169,6 @@ class ProductForm(forms.ModelForm):self.fields["collections"].initial = Collection.objects.filter(products__name=self.instance)- @classmethod- def generate_seo_description(cls, html_text):- """Strips HTML tags and whitespaces from text,- then trim the description."""- text = strip_html(html_text, strip_whitespace=True)- text = trim_text(text,- settings.GENERATED_SEO_MAX_LENGTH,- settings.GENERATED_SEO_SUFFIX)- return text-def clean_seo_description(self):seo_description = self.cleaned_data['seo_description']diff --git a/saleor/product/migrations/0053_product_seo_description.py b/saleor/product/migrations/0053_product_seo_description.pyindex ddadd27d60..db718ee920 100644--- a/saleor/product/migrations/0053_product_seo_description.py+++ b/saleor/product/migrations/0053_product_seo_description.py@@ -1,9 +1,9 @@# Generated by Django 2.0.2 on 2018-03-11 18:54import html+from django.core.validators import MaxLengthValidatorfrom django.db import migrations, models--from saleor.dashboard.product.forms import ProductForm+from saleor.core.utils.text import generate_seo_descriptiondef to_seo_friendly(text):@@ -12,7 +12,7 @@ def to_seo_friendly(text):text = html.unescape(text)# cleanup the description and make it seo friendly- return ProductForm.generate_seo_description(text)+ return generate_seo_description(text)def assign_seo_descriptions(apps, schema_editor):@@ -33,7 +33,9 @@ class Migration(migrations.Migration):migrations.AddField(model_name='product',name='seo_description',- field=models.CharField(blank=True, null=True, max_length=255),+ field=models.CharField(+ blank=True, null=True, max_length=300,+ validators=[MaxLengthValidator(300)]),preserve_default=False,),migrations.RunPython(assign_seo_descriptions)diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 249a606780..8613b4864c 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -3,7 +3,8 @@ from decimal import Decimalfrom django.conf import settingsfrom django.contrib.postgres.fields import HStoreField-from django.core.validators import MinValueValidator, RegexValidator+from django.core.validators import MinValueValidator, RegexValidator, \+ MaxLengthValidatorfrom django.db import modelsfrom django.db.models import F, Max, Qfrom django.urls import reverse@@ -92,7 +93,9 @@ class Product(models.Model):ProductType, related_name='products', on_delete=models.CASCADE)name = models.CharField(max_length=128)description = models.TextField()- seo_description = models.CharField(max_length=255, blank=True, null=True)+ seo_description = models.CharField(+ max_length=300, blank=True, null=True,+ validators=[MaxLengthValidator(300)])category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)price = MoneyField(diff --git a/tests/test_core.py b/tests/test_core.pyindex a4a3724cf0..5352030ab5 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -8,7 +8,7 @@ from saleor.account.models import Address, Userfrom saleor.core.utils import (Country, create_superuser, format_money, get_country_by_ip,get_currency_for_country, random_data)-from saleor.core.utils.text import trim_text, strip_html, get_cleaner+from saleor.core.utils.text import strip_html, get_cleanerfrom saleor.discount.models import Sale, Voucherfrom saleor.order.models import Orderfrom saleor.product.models import Product@@ -163,11 +163,6 @@ def test_manifest(client, site_settings):assert content['description'] == site_settings.description-def test_utils_trim_text():- text = trim_text('Hello World, everyone!', 16, '[...]')- assert text == 'Hello World[...]'--def test_utils_get_cleaner_invalid_parameters():with pytest.raises(ValueError):get_cleaner(bad=True)