-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathold_richtext.py
More file actions
70 lines (51 loc) · 2.08 KB
/
Copy pathold_richtext.py
File metadata and controls
70 lines (51 loc) · 2.08 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Provides a rich text area whose content is automatically cleaned using a
very restrictive allowlist of tags and attributes.
Depends on
`django-ckeditor <https://github.com/django-ckeditor/django-ckeditor/>`__ and
`html-sanitizer <https://pypi.org/project/html-sanitizer>`__.
"""
import warnings
from content_editor.admin import ContentEditorInline
from django.db import models
from django.utils.html import mark_safe, strip_tags
from django.utils.text import Truncator
from django.utils.translation import gettext_lazy as _
from feincms3.cleanse import CleansedRichTextField
__all__ = ("RichText", "RichTextInline", "render_richtext")
warnings.warn(
"The richtext plugin depending on django-ckeditor will be removed"
" in the close future. Sorry for the inconvenience.",
DeprecationWarning,
stacklevel=2,
)
class RichText(models.Model):
"""
Rich text plugin
To use this, a `django-ckeditor
<https://github.com/django-ckeditor/django-ckeditor>`_ configuration named
``richtext-plugin`` is required. See the section :mod:`HTML cleansing
<feincms3.cleanse>` for the recommended configuration.
"""
text = CleansedRichTextField(_("text"), config_name="richtext-plugin")
class Meta:
abstract = True
verbose_name = _("rich text")
verbose_name_plural = _("rich texts")
def __str__(self):
# Return the first few words of the content (with tags stripped)
return Truncator(strip_tags(self.text)).words(10, truncate=" ...")
class RichTextInline(ContentEditorInline):
"""
The only difference with the standard ``ContentEditorInline`` is that this
inline adds the ``feincms3/plugin-ckeditor.css`` file which adjusts the
width of the django-ckeditor widget inside the content editor.
"""
button = '<span class="material-icons">notes</span>'
class Media:
css = {"screen": ["feincms3/plugin-ckeditor.css"]}
def render_richtext(plugin, context=None, **kwargs):
"""
Return the text of the rich text plugin as a safe string (``mark_safe``)
"""
return mark_safe(plugin.text)