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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3fe49b47ea
commit
5bb9a70c11
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<script type="application/javascript">
|
||||
tinymce.init({
|
||||
selector: '{{ selector }}',
|
||||
license_key: 'gpl',
|
||||
menubar: '',
|
||||
toolbar: 'undo redo | bold italic | bullist numlist | link emoticons',
|
||||
plugins: [
|
||||
'link', 'emoticons', 'lists'
|
||||
],
|
||||
setup: function (editor) {
|
||||
editor.on('change', function () {
|
||||
tinymce.triggerSave();
|
||||
})
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -30,7 +30,7 @@
|
||||
<dt class="col-sm-3">Title</dt>
|
||||
<dd class="col-sm-9">{{ content_asset.title }}</dd>
|
||||
<dt class="col-sm-3">Description</dt>
|
||||
<dd class="col-sm-9">{{ content_asset.description|linebreaksbr|default:"—" }}</dd>
|
||||
<dd class="col-sm-9">{{ content_asset.description|default:"—"|safe }}</dd>
|
||||
<dt class="col-sm-3">Release date</dt>
|
||||
<dd class="col-sm-9">{{ content_asset.release_date|default:"—" }}</dd>
|
||||
<dt class="col-sm-3">Series / Collection</dt>
|
||||
@@ -72,4 +72,6 @@
|
||||
{% endif %}
|
||||
</form>
|
||||
{% endwith %}
|
||||
{% include 'includes/_tinymce.html' with selector="textarea#id_description" %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -16,6 +16,7 @@ dependencies = [
|
||||
"whitenoise~=6.11.0",
|
||||
"pytz~=2026.5",
|
||||
"django-allauth[socialaccount]~=65.0.2",
|
||||
"nh3==0.2.20",
|
||||
]
|
||||
dynamic = ["version"]
|
||||
|
||||
|
||||
@@ -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;
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user