Skip to content

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

Add CharsLeft widget to all SEOable models

Pacu242979a4ae057
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index 88ad23b484..b427e7da9d 100644
--- a/saleor/dashboard/category/forms.py
+++ b/saleor/dashboard/category/forms.py
@@ -5,7 +5,9 @@ from django.utils.translation import pgettext_lazy
from text_unidecode import unidecode
from ...product.models import Category
-from ..seo.utils import SEO_HELP_TEXTS, SEO_LABELS
+from ..seo.utils import (
+ MIN_DESCRIPTION_LENGTH, MIN_TITLE_LENGTH, SEO_HELP_TEXTS, SEO_LABELS,
+ SEO_WIDGETS)
class CategoryForm(forms.ModelForm):
@@ -13,9 +15,11 @@ class CategoryForm(forms.ModelForm):
self.parent_pk = kwargs.pop('parent_pk')
super().__init__(*args, **kwargs)
self.fields['seo_description'].widget.attrs.update({
- 'data-bind': self['description'].auto_id})
+ 'data-bind': self['description'].auto_id,
+ 'min-recommended-length': MIN_DESCRIPTION_LENGTH})
self.fields['seo_title'].widget.attrs.update({
- 'data-bind': self['name'].auto_id})
+ 'data-bind': self['name'].auto_id,
+ 'min-recommended-length': MIN_TITLE_LENGTH})
class Meta:
model = Category
@@ -29,6 +33,7 @@ class CategoryForm(forms.ModelForm):
'Description'),
**SEO_LABELS}
help_texts = SEO_HELP_TEXTS
+ widgets = SEO_WIDGETS
def save(self, commit=True):
self.instance.slug = slugify(unidecode(self.instance.name))
diff --git a/saleor/dashboard/collection/forms.py b/saleor/dashboard/collection/forms.py
index 8db5b7a425..e9eefea8cc 100644
--- a/saleor/dashboard/collection/forms.py
+++ b/saleor/dashboard/collection/forms.py
@@ -6,8 +6,9 @@ from text_unidecode import unidecode
from ...product.models import Collection, Product
from ..forms import AjaxSelect2MultipleChoiceField
-from ..seo.utils import SEO_HELP_TEXTS, SEO_LABELS
-from ..widgets import CharsLeftWidget
+from ..seo.utils import (
+ MIN_DESCRIPTION_LENGTH, MIN_TITLE_LENGTH, SEO_HELP_TEXTS, SEO_LABELS,
+ SEO_WIDGETS)
class CollectionForm(forms.ModelForm):
@@ -29,9 +30,7 @@ class CollectionForm(forms.ModelForm):
'Products selection',
'Background Image'),
**SEO_LABELS}
- widgets = {
- 'seo_description': CharsLeftWidget,
- 'seo_title': CharsLeftWidget}
+ widgets = SEO_WIDGETS
help_texts = SEO_HELP_TEXTS
def __init__(self, *args, **kwargs):
@@ -39,10 +38,10 @@ class CollectionForm(forms.ModelForm):
if self.instance.pk:
self.fields['products'].set_initial(self.instance.products.all())
self.fields['seo_description'].widget.attrs.update({
- 'min-recommended-length': 130})
+ 'min-recommended-length': MIN_DESCRIPTION_LENGTH})
self.fields['seo_title'].widget.attrs.update({
'data-bind': self['name'].auto_id,
- 'min-recommended-length': 25})
+ 'min-recommended-length': MIN_TITLE_LENGTH})
def save(self, commit=True):
self.instance.slug = slugify(unidecode(self.instance.name))
diff --git a/saleor/dashboard/page/forms.py b/saleor/dashboard/page/forms.py
index 308bcce0db..d92c2b2d6a 100644
--- a/saleor/dashboard/page/forms.py
+++ b/saleor/dashboard/page/forms.py
@@ -3,7 +3,9 @@ from django.utils.translation import pgettext_lazy
from ...page.models import Page
from ..product.forms import RichTextField
-from ..seo.utils import SEO_HELP_TEXTS, SEO_LABELS, prepare_seo_description
+from ..seo.utils import (
+ MIN_DESCRIPTION_LENGTH, MIN_TITLE_LENGTH, SEO_HELP_TEXTS, SEO_LABELS,
+ SEO_WIDGETS, prepare_seo_description)
class PageForm(forms.ModelForm):
@@ -12,15 +14,18 @@ class PageForm(forms.ModelForm):
super().__init__(*args, **kwargs)
self.fields['seo_description'].widget.attrs.update({
'data-bind': self['content'].auto_id,
- 'data-materialize': self['content'].html_name})
+ 'data-materialize': self['content'].html_name,
+ 'min-recommended-length': MIN_DESCRIPTION_LENGTH})
self.fields['seo_title'].widget.attrs.update({
- 'data-bind': self['title'].auto_id})
+ 'data-bind': self['title'].auto_id,
+ 'min-recommended-length': MIN_TITLE_LENGTH})
class Meta:
model = Page
exclude = []
widgets = {
- 'slug': forms.TextInput(attrs={'placeholder': 'example-slug'})}
+ 'slug': forms.TextInput(attrs={'placeholder': 'example-slug'}),
+ **SEO_WIDGETS}
labels = {
'is_visible': pgettext_lazy(
'Visibility status indicator', 'Publish'),
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index b6a54b5889..fb6e600da8 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -9,15 +9,17 @@ from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from mptt.forms import TreeNodeChoiceField
+from . import ProductBulkAction
from ...product.models import (
AttributeChoiceValue, Category, Collection, Product, ProductAttribute,
ProductImage, ProductType, ProductVariant, Stock, StockLocation,
VariantImage)
from ..forms import OrderedModelMultipleChoiceField, ModelChoiceOrCreationField
+from ..seo.utils import (
+ MIN_DESCRIPTION_LENGTH, MIN_TITLE_LENGTH, SEO_HELP_TEXTS, SEO_LABELS,
+ SEO_WIDGETS, prepare_seo_description)
from ..widgets import RichTextEditorWidget
from .widgets import ImagePreviewWidget
-from . import ProductBulkAction
-from ..seo.utils import SEO_HELP_TEXTS, SEO_LABELS, prepare_seo_description
class RichTextField(forms.CharField):
@@ -204,6 +206,7 @@ class ProductForm(forms.ModelForm, AttributesMixin):
'Add to collection select', 'Collections'),
**SEO_LABELS}
help_texts = SEO_HELP_TEXTS
+ widgets = SEO_WIDGETS
category = TreeNodeChoiceField(queryset=Category.objects.all())
collections = forms.ModelMultipleChoiceField(
@@ -222,9 +225,11 @@ class ProductForm(forms.ModelForm, AttributesMixin):
products__name=self.instance)
self.fields['seo_description'].widget.attrs.update({
'data-bind': self['description'].auto_id,
- 'data-materialize': self['description'].html_name})
+ 'data-materialize': self['description'].html_name,
+ 'min-recommended-length': MIN_DESCRIPTION_LENGTH})
self.fields['seo_title'].widget.attrs.update({
- 'data-bind': self['name'].auto_id})
+ 'data-bind': self['name'].auto_id,
+ 'min-recommended-length': MIN_TITLE_LENGTH})
def clean_seo_description(self):
seo_description = prepare_seo_description(
diff --git a/saleor/dashboard/seo/utils.py b/saleor/dashboard/seo/utils.py
index b38401a40a..a9bb344b4a 100644
--- a/saleor/dashboard/seo/utils.py
+++ b/saleor/dashboard/seo/utils.py
@@ -1,7 +1,10 @@
from django.utils.translation import pgettext_lazy
from ...core.utils.text import strip_html_and_truncate
+from ..widgets import CharsLeftWidget
+MIN_TITLE_LENGTH = 25
+MIN_DESCRIPTION_LENGTH = 120
SEO_HELP_TEXTS = {
'seo_description': pgettext_lazy(
'Form field help text',
@@ -11,7 +14,6 @@ SEO_HELP_TEXTS = {
'Form field help text',
(
'If empty, the preview shows what will be autogenerated.'))}
-
SEO_LABELS = {
'seo_description': pgettext_lazy(
(
@@ -23,6 +25,9 @@ SEO_LABELS = {
'Field name, '
'Title that will be used to describe page in Search Engines'),
'SEO Title')}
+SEO_WIDGETS = {
+ 'seo_description': CharsLeftWidget,
+ 'seo_title': CharsLeftWidget}
def prepare_seo_description(seo_description, html_description, max_length):