From 5bb9a70c116985f4138272eea553e497815ed5ca Mon Sep 17 00:00:00 2001 From: Shaun Kruger Date: Tue, 1 Dec 2026 20:57:31 -0700 Subject: [PATCH] Add sanitized rich-text descriptions for content assets Introduce HtmlTextField, which cleans submitted HTML with nh3 against a tag/attribute allowlist, and use it for ContentAsset.description via Meta.field_classes. Edit descriptions with TinyMCE and render the sanitized HTML on the asset page. Co-Authored-By: Claude Opus 5.5 --- privatestudio/fields.py | 42 +++++++++++++++++++ privatestudio/studio/forms.py | 4 ++ .../studio/templates/includes/_tinymce.html | 16 +++++++ .../privatestudio/content_asset_form.html | 4 +- pyproject.toml | 1 + src/scss/styles.scss | 8 ++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 privatestudio/fields.py create mode 100644 privatestudio/studio/templates/includes/_tinymce.html 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 @@
Title
{{ content_asset.title }}
Description
-
{{ content_asset.description|linebreaksbr|default:"—" }}
+
{{ content_asset.description|default:"—"|safe }}
Release date
{{ content_asset.release_date|default:"—" }}
Series / Collection
@@ -72,4 +72,6 @@ {% endif %} {% endwith %} + {% include 'includes/_tinymce.html' with selector="textarea#id_description" %} + {% endblock %} diff --git a/pyproject.toml b/pyproject.toml index f839841..1293935 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ dependencies = [ "whitenoise~=6.11.0", "pytz~=2026.5", "django-allauth[socialaccount]~=65.0.2", + "nh3==0.2.20", ] dynamic = ["version"] diff --git a/src/scss/styles.scss b/src/scss/styles.scss index e751dd0..0e26f35 100644 --- a/src/scss/styles.scss +++ b/src/scss/styles.scss @@ -16,3 +16,11 @@ form.simpleform label { font-weight: bold; font-size: 1.2em; } + +div.tox-tinymce div.tox-promotion { + display: none; +} + +//span.tox-statusbar__branding { +// display: none; +//}