Prevent LocalizedBleachField from escaping values (#97)

This commit is contained in:
Gherman Razvan
2024-01-23 14:52:37 +02:00
committed by GitHub
parent a9906dd159
commit 911251ebaa
2 changed files with 40 additions and 8 deletions

View File

@@ -1,3 +1,5 @@
import html
from django.conf import settings
from .field import LocalizedField
@@ -7,6 +9,13 @@ class LocalizedBleachField(LocalizedField):
"""Custom version of :see:BleachField that is actually a
:see:LocalizedField."""
def __init__(self, *args, escape=True, **kwargs):
"""Initializes a new instance of :see:LocalizedBleachField."""
self.escape = escape
super().__init__(*args, **kwargs)
def pre_save(self, instance, add: bool):
"""Ran just before the model is saved, allows us to built the slug.
@@ -42,8 +51,14 @@ class LocalizedBleachField(LocalizedField):
if not value:
continue
cleaned_value = bleach.clean(
value if self.escape else html.unescape(value),
**get_bleach_default_options()
)
localized_value.set(
lang_code, bleach.clean(value, **get_bleach_default_options())
lang_code,
cleaned_value if self.escape else html.unescape(cleaned_value),
)
return localized_value