-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathrichtext.py
More file actions
31 lines (24 loc) · 1012 Bytes
/
richtext.py
File metadata and controls
31 lines (24 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django import forms
from django.db import models
class RichTextFormField(forms.fields.CharField):
def __init__(self, *args, **kwargs):
self.cleanse = kwargs.pop("cleanse", None)
super().__init__(*args, **kwargs)
css_class = self.widget.attrs.get("class", "")
css_class += " item-richtext"
self.widget.attrs["class"] = css_class
def clean(self, value):
value = super().clean(value)
if self.cleanse:
value = self.cleanse(value)
return value
class RichTextField(models.TextField):
"""
Drop-in replacement for Django's ``models.TextField`` which allows editing
rich text instead of plain text in the item editor.
"""
def __init__(self, *args, **kwargs):
self.cleanse = kwargs.pop("cleanse", None)
super().__init__(*args, **kwargs)
def formfield(self, form_class=RichTextFormField, **kwargs):
return super().formfield(form_class=form_class, cleanse=self.cleanse, **kwargs)