Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add new field for rich text editor
akjanikd2c9a1239600
diff --git a/requirements.in b/requirements.inindex aee3353fb9..c9fd6b7923 100644--- a/requirements.in+++ b/requirements.in@@ -1,4 +1,5 @@Babel>=2.5+bleach<3.0boto3cairosvgDjango>=1.11diff --git a/requirements.txt b/requirements.txtindex 32ed31a5ea..c4352cc71c 100644--- a/requirements.txt+++ b/requirements.txt@@ -9,6 +9,7 @@ asn1crypto==0.24.0 # via cryptographyattrs==17.4.0 # via pytestbabel==2.5.3billiard==3.5.0.3 # via celery+bleach==2.1.2boto3==1.5.18botocore==1.8.32 # via boto3, s3transferbraintree==3.39.1 # via django-payments@@ -56,7 +57,7 @@ graphene-django==2.0.0graphene==2.0.1 # via graphene-djangographql-core==2.0 # via graphene, graphql-relaygraphql-relay==0.4.5 # via graphene-html5lib==1.0.1 # via weasyprint+html5lib==1.0.1 # via bleach, weasyprintidna==2.6 # via cryptography, requests, yarliso8601==0.1.12 # via graphene-djangojmespath==0.9.3 # via boto3, botocore@@ -94,7 +95,7 @@ rx==1.6.0 # via graphql-cores3transfer==0.1.12 # via boto3satchless==1.1.3singledispatch==3.4.0.3 # via graphene-django-six==1.11.0 # via cryptography, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpy+six==1.11.0 # via bleach, cryptography, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpysocial-auth-app-django==2.1.0social-auth-core==1.6.0 # via social-auth-app-djangostripe==1.77.1 # via django-paymentsdiff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex aebdef2005..ca13df61ff 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -1,3 +1,4 @@+from bleach.sanitizer import ALLOWED_TAGSfrom django import formsfrom django.db.models import Countfrom django.forms.models import ModelChoiceIterator@@ -5,6 +6,7 @@ from django.forms.widgets import CheckboxSelectMultiplefrom django.utils.encoding import smart_textfrom django.utils.text import slugifyfrom django.utils.translation import pgettext_lazy+import bleachfrom . import ProductBulkActionfrom ...product.models import (@@ -14,6 +16,20 @@ from ..widgets import RichTextEditorWidgetfrom .widgets import ImagePreviewWidget+class RichTextField(forms.CharField):+ """+ A field for rich text editor, providing backend sanitization+ """++ widget = RichTextEditorWidget+ ALLOWED_TAGS.extend([u'br', u'p', u'h2', u'h3'])++ def to_python(self, value):+ value = super().to_python(value)+ value = bleach.clean(value, tags=ALLOWED_TAGS)+ return value++class ProductTypeSelectorForm(forms.Form):"""Form that allows selecting product type."""@@ -131,6 +147,7 @@ class ProductForm(forms.ModelForm):collections = forms.ModelMultipleChoiceField(required=False, queryset=Collection.objects.all())+ description = RichTextField()def __init__(self, *args, **kwargs):self.product_attributes = []@@ -140,7 +157,6 @@ class ProductForm(forms.ModelForm):self.product_attributes = self.product_attributes.prefetch_related('values')self.prepare_fields_for_attributes()- self.fields['description'].widget = RichTextEditorWidget()self.fields["collections"].initial = Collection.objects.filter(products__name=self.instance)diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.pyindex 65aacb7242..45b050c3b5 100644--- a/tests/dashboard/test_product.py+++ b/tests/dashboard/test_product.py@@ -7,6 +7,7 @@ from PIL import Imagefrom django.conf import settingsfrom django.core.files.uploadedfile import SimpleUploadedFilefrom django.forms import HiddenInput+from django.forms.models import model_to_dictfrom django.urls import reversefrom django.utils.encoding import smart_textimport pytest@@ -678,3 +679,24 @@ def test_assign_collection_to_product(product_in_stock):form.save()assert product.collections.first().name == 'test_collections'assert collection.products.first().name == product.name+++def test_sanitize_product_description(product_type, default_category):+ product = Product.objects.create(+ name='Test Product', price=10, description='', pk=10,+ product_type=product_type, category=default_category)+ data = model_to_dict(product)+ data['description'] = '<b>bold</b><p><i>italic</i></p><h2>Header</h2>' \+ '<h3>subheader</h3><blockquote>quote</blockquote>' \+ '<p><a href="www.mirumee.com">link</a></p>' \+ '<p>an <script>evil()</script> example</p>'+ data['price'] = 20+ form = ProductForm(data, instance=product)+ assert form.is_valid()+ form.save()+ assert product.description == '<b>bold</b><p><i>italic</i></p>' \+ '<h2>Header</h2><h3>subheader</h3>' \+ '<blockquote>quote</blockquote>' \+ '<p><a href="www.mirumee.com">link</a></p>' \+ '<p>an <script>evil()</script>' \+ ' example</p>'