django-summernote icon indicating copy to clipboard operation
django-summernote copied to clipboard

Using a SummernoteTextField for sanitization, turns a SummernoteInPlaceWidget into a SummernoteWidget.

Open millerthegorilla opened this issue 3 years ago • 2 comments

Hi, when trying to sanitize input, on a SummerNoteInPlaceWidget introduced on a form, if I use a SummernoteTextField in my model, the form displayed starts requiring an iframe. This is a bug, I think.

millerthegorilla avatar Jan 27 '21 14:01 millerthegorilla

I am guessing this is due to the following code in fields.py

class SummernoteTextFormField(fields.CharField):
    def __init__(self, *args, **kwargs):
        kwargs.update({'widget': SummernoteWidget()})
        super().__init__(*args, **kwargs)

millerthegorilla avatar Jan 27 '21 14:01 millerthegorilla

Hi, my suggestion is to cange to

`class SummernoteTextFormField(fields.CharField): # You can define widget in form with SummernoteTextFormField(widget=OtherWidget). def to_python(self, value): value = super().to_python(value) return bleach.clean(value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)

class SummernoteTextField(models.TextField): # Default widgetas For this field type. # You can define any other widget in model with SummernoteTextField(widget=OtherWidget). widget = CustomWidthSummernoteInplaceWidget()

def __init__(self, widget=None, *args, **kwargs):
    if widget:
        self.widget = widget
    super().__init__(*args, **kwargs)

def formfield(self, **kwargs):
    kwargs.update({'form_class': SummernoteTextFormField, 'widget': self.widget})
    return super().formfield(**kwargs)

def to_python(self, value):
    value = super().to_python(value)
    return bleach.clean(value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)

`

simonasjutkevicius avatar Feb 04 '22 10:02 simonasjutkevicius