Skip to content

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

Made requested changes: dropped custom trimming for django's

wuwu236e958e7546
diff --git a/requirements.in b/requirements.in
index 29580323ac..91c19a57ef 100644
--- a/requirements.in
+++ b/requirements.in
@@ -44,7 +44,6 @@ pytest-django
pytest-vcr
pytest-mock
vcrpy
-regex
requests>=1.2.0
social-auth-app-django>=2.0.0
text-unidecode
diff --git a/requirements.txt b/requirements.txt
index 781a7ade73..1f70a97dd4 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -89,7 +89,6 @@ python3-openid==3.1.0 # via social-auth-core
pytz==2018.3 # via babel, celery, django
pyyaml==3.12 # via vcrpy
redis==2.10.6 # via celery, django-redis
-regex==2018.2.21
requests-oauthlib==0.8.0 # via social-auth-core
requests==2.18.4
rx==1.6.0 # via graphql-core
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 57274cfae0..bd5a6081a1 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -15,6 +15,7 @@ from prices import Money, TaxedMoney
from ...account.models import Address, User
from ...account.utils import store_user_address
+from ...core.utils.text import generate_seo_description
from ...discount import DiscountValueType, VoucherType
from ...discount.models import Sale, Voucher
from ...order.models import Fulfillment, Order, Payment
@@ -269,11 +270,12 @@ 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': description[0][:300]}
+ 'seo_description': generate_seo_description(description[0], seo_description_field)}
defaults.update(kwargs)
return Product.objects.create(**defaults)
diff --git a/saleor/core/utils/text.py b/saleor/core/utils/text.py
index 1ced87d36f..7869a65886 100644
--- a/saleor/core/utils/text.py
+++ b/saleor/core/utils/text.py
@@ -1,55 +1,10 @@
# coding: utf-8
import bleach
-import regex as re
from django.forms import fields
+from django.template.defaultfilters import truncatechars
from html5lib.serializer import HTMLSerializer
-# This will match from a cut word to a punctuation or whitespace
-LAST_WORD_SENTENCE_RE = re.compile(r'[\p{Po}\s]\w*$', re.U)
-
-
-def _get_last_word_separator_pos(text: str):
- """Get the position of the first punctuation or whitespace next to the
- first full word from the end of the sentence.
-
- :param text:
- :type text: str
-
- :rtype: Union[int, None]
- """
- match = LAST_WORD_SENTENCE_RE.search(text)
- if match:
- # position of the last stop char
- return match.start()
- return None
-
-
-def trim_fullword(text: str, max_length: int):
- """Trim text to a length lesser than `max_length` without cutting words
- in their middle.
-
- If it failed to trim the text (first word too long), the word will be cut.
-
- :param text: The text to trim.
- :type text: str
-
- :param max_length: The maximal length of the new string to return.
- :type max_length: int
-
- :rtype: str
- """
- new_text = text[:max_length]
-
- if len(text) > max_length:
- # if the last char wasn't a punctuation or whitespace,
- # try to cut in another place or if unable,
- # just return the sentence with the word cut
- if not _get_last_word_separator_pos(text[max_length]):
- end_pos = _get_last_word_separator_pos(new_text)
- new_text = new_text[:end_pos]
- return new_text
-
def get_cleaner(**serializer_kwargs: bool):
"""
@@ -102,5 +57,5 @@ def generate_seo_description(html_text: str, target_field: fields.CharField):
"""Strips HTML tags and whitespaces from text,
then trim the description."""
text = strip_html(html_text, strip_whitespace=True)
- text = trim_fullword(text, target_field.max_length)
+ text = truncatechars(text, target_field.max_length)
return text
diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.py
index 56218cbc34..c640b96d9a 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -742,11 +742,11 @@ def test_set_product_description_too_long_for_seo(unavailable_product):
assert form.is_valid()
form.save()
- assert len(unavailable_product.seo_description) < 300
+ assert len(unavailable_product.seo_description) <= 300
assert unavailable_product.seo_description == (
'Saying it fourth made saw light bring beginning kind over herb '
'won\'t creepeth multiply dry rule divided fish herb cattle greater '
'fly divided midst, gathering can\'t moveth seed greater subdue. '
'Lesser meat living fowl called. Dry don\'t wherein. Doesn\'t above '
- 'form sixth. Image moving earth without'
+ 'form sixth. Image moving earth without f...'
)
diff --git a/tests/test_core.py b/tests/test_core.py
index a11f3ec9f3..c5a2b034c3 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -8,8 +8,7 @@ from saleor.account.models import Address, User
from 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 (
- _get_last_word_separator_pos, get_cleaner, strip_html, trim_fullword)
+from saleor.core.utils.text import get_cleaner, strip_html
from saleor.discount.models import Sale, Voucher
from saleor.order.models import Order
from saleor.product.models import Product
@@ -164,24 +163,6 @@ def test_manifest(client, site_settings):
assert content['description'] == site_settings.description
-def test_utils_get_last_stop_char():
- assert _get_last_word_separator_pos('') is None
- assert _get_last_word_separator_pos('this a cut sentenc') == 10
- assert _get_last_word_separator_pos('.') == 0
- assert _get_last_word_separator_pos('こんにちは。') == 5
- assert _get_last_word_separator_pos('여보세요;세계') == 4
-
-
-def test_trim_fullword():
- assert trim_fullword('it\'s too long. but period.', 14) == 'it\'s too long'
- assert trim_fullword('it\'s not too long.', 50) == 'it\'s not too long.'
- assert trim_fullword('don\'t break', 10) == 'don\'t'
- assert trim_fullword('this is a sentence', 5) == 'this'
- assert trim_fullword('thisIsTooLongJustCutIt', 4) == 'this'
- assert trim_fullword('this\nis\nmultiline', 5) == 'this'
- assert trim_fullword('', 5) == ''
-
-
def test_utils_get_cleaner_invalid_parameters():
with pytest.raises(ValueError):
get_cleaner(bad=True)