forked from django-cms/djangocms-text-ckeditor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
widgets.py
43 lines (36 loc) · 1.72 KB
/
widgets.py
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
from django.conf import settings
from django.forms import Textarea
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation.trans_real import get_language
from django.utils import simplejson
import djangocms_text_ckeditor.settings as text_settings
class TextEditorWidget(Textarea):
def __init__(self, attrs=None, installed_plugins=None, pk=None, placeholder=None, plugin_language=None):
"""
Create a widget for editing text + plugins.
installed_plugins is a list of plugins to display that are text_enabled
"""
super(TextEditorWidget, self).__init__(attrs)
self.installed_plugins = installed_plugins
self.pk = pk
self.placeholder = placeholder
self.plugin_language = plugin_language
def render_textarea(self, name, value, attrs=None):
return super(TextEditorWidget, self).render(name, value, attrs)
def render_additions(self, name, value, attrs=None):
language = get_language().split('-')[0]
context = {
'name': name,
'language': language,
'settings': language.join(simplejson.dumps(text_settings.CKEDITOR_SETTINGS).split("{{ language }}")),
'STATIC_URL': settings.STATIC_URL,
'installed_plugins': self.installed_plugins,
'plugin_pk': self.pk,
'plugin_language': self.plugin_language,
'placeholder': self.placeholder
}
return mark_safe(render_to_string('cms/plugins/widgets/ckeditor.html', context))
def render(self, name, value, attrs=None):
return self.render_textarea(name, value, attrs) + \
self.render_additions(name, value, attrs)