diff --git a/privatestudio/fields.py b/privatestudio/fields.py new file mode 100644 index 0000000..1eb50ab --- /dev/null +++ b/privatestudio/fields.py @@ -0,0 +1,42 @@ +import nh3 + +from django import forms +from django.forms import Textarea + +NH3_TAGS = { + "a", + "p", + "strong", + "img", + "ul", + "ol", + "li", + "b", + "s", + "u", +} + +NH3_ATTR = { + "a": { + "href", + "target", + }, + "img": { + "alt", + "src", + }, +} + + +class HtmlTextField(forms.CharField): + widget = Textarea + + def to_python(self, value): + value = super().to_python(value) + if value not in self.empty_values: + value = nh3.clean( + value, + tags=NH3_TAGS, + attributes=NH3_ATTR, + ) + return value diff --git a/privatestudio/studio/forms.py b/privatestudio/studio/forms.py index b982270..32366af 100644 --- a/privatestudio/studio/forms.py +++ b/privatestudio/studio/forms.py @@ -1,6 +1,7 @@ from django import forms from privatestudio.studio.models import ContentAsset +from privatestudio.fields import HtmlTextField class ContentAssetForm(forms.ModelForm): @@ -14,6 +15,9 @@ class ContentAssetForm(forms.ModelForm): "release_date": forms.DateInput(attrs={"type": "date"}, format="%Y-%m-%d"), "description": forms.Textarea(attrs={"rows": 4}), } + field_classes = { + "description": HtmlTextField, + } def __init__(self, *args, studio, **kwargs): super().__init__(*args, **kwargs) diff --git a/privatestudio/studio/templates/includes/_tinymce.html b/privatestudio/studio/templates/includes/_tinymce.html new file mode 100644 index 0000000..7724df9 --- /dev/null +++ b/privatestudio/studio/templates/includes/_tinymce.html @@ -0,0 +1,16 @@ + diff --git a/privatestudio/studio/templates/privatestudio/content_asset_form.html b/privatestudio/studio/templates/privatestudio/content_asset_form.html index fb185a1..849c25e 100644 --- a/privatestudio/studio/templates/privatestudio/content_asset_form.html +++ b/privatestudio/studio/templates/privatestudio/content_asset_form.html @@ -30,7 +30,7 @@