-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.py
More file actions
163 lines (139 loc) · 5.41 KB
/
widgets.py
File metadata and controls
163 lines (139 loc) · 5.41 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import absolute_import
from django import forms
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.encoding import force_text
from django.utils.functional import Promise
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from django.utils.translation import get_language
from js_asset import JS, static
try:
# Django >=1.11
from django.forms.widgets import get_default_renderer
except ImportError:
# Django <1.11
from django.template.loader import render_to_string
def get_default_renderer():
class DummyDjangoRenderer(object):
@staticmethod
def render(*args, **kwargs):
return render_to_string(*args, **kwargs)
return DummyDjangoRenderer
try:
# Django >=1.7
from django.forms.utils import flatatt
except ImportError:
# Django <1.7
from django.forms.util import flatatt
class LazyEncoder(DjangoJSONEncoder):
def default(self, obj):
if isinstance(obj, Promise):
return force_text(obj)
return super(LazyEncoder, self).default(obj)
json_encode = LazyEncoder().encode
DEFAULT_CONFIG = {
'skin': 'moono-lisa',
'toolbar_Basic': [
['Source', '-', 'Bold', 'Italic']
],
'toolbar_Full': [
['Styles', 'Format', 'Bold', 'Italic', 'Underline', 'Strike', 'SpellChecker', 'Undo', 'Redo'],
['Link', 'Unlink', 'Anchor'],
['Image', 'Flash', 'Table', 'HorizontalRule'],
['TextColor', 'BGColor'],
['Smiley', 'SpecialChar'], ['Source'],
],
'toolbar': 'Full',
'height': 291,
'width': 835,
'filebrowserWindowWidth': 940,
'filebrowserWindowHeight': 725,
}
class CKEditorWidget(forms.Textarea):
"""
Widget providing CKEditor for Rich Text Editing.
Supports direct image uploads and embed.
"""
class Media:
js = (
JS('ckeditor/ckeditor-init.js', {
'id': 'ckeditor-init-script',
'data-ckeditor-basepath': getattr(
settings,
'CKEDITOR_BASEPATH',
static('ckeditor/ckeditor/'),
),
}),
'ckeditor/ckeditor/ckeditor.js',
)
def __init__(self, config_name='default', extra_plugins=None, external_plugin_resources=None, *args, **kwargs):
super(CKEditorWidget, self).__init__(*args, **kwargs)
# Setup config from defaults.
self.config = DEFAULT_CONFIG.copy()
# Try to get valid config from settings.
configs = getattr(settings, 'CKEDITOR_CONFIGS', None)
if configs:
if isinstance(configs, dict):
# Make sure the config_name exists.
if config_name in configs:
config = configs[config_name]
# Make sure the configuration is a dictionary.
if not isinstance(config, dict):
raise ImproperlyConfigured('CKEDITOR_CONFIGS["%s"] \
setting must be a dictionary type.' %
config_name)
# Override defaults with settings config.
self.config.update(config)
else:
raise ImproperlyConfigured("No configuration named '%s' \
found in your CKEDITOR_CONFIGS setting." %
config_name)
else:
raise ImproperlyConfigured('CKEDITOR_CONFIGS setting must be a\
dictionary type.')
extra_plugins = (
extra_plugins
or self.config.pop("extra_plugins", None)
or []
)
if extra_plugins:
self.config['extraPlugins'] = ','.join(extra_plugins)
self.external_plugin_resources = (
external_plugin_resources
or self.config.pop("external_plugin_resources", None)
or []
)
def render(self, name, value, attrs=None, renderer=None):
if renderer is None:
renderer = get_default_renderer()
if value is None:
value = ''
final_attrs = self.build_attrs(self.attrs, attrs, name=name)
self._set_config()
external_plugin_resources = [[force_text(a), force_text(b), force_text(c)]
for a, b, c in self.external_plugin_resources]
return mark_safe(renderer.render('ckeditor/widget.html', {
'final_attrs': flatatt(final_attrs),
'value': conditional_escape(force_text(value)),
'id': final_attrs['id'],
'config': json_encode(self.config),
'external_plugin_resources': json_encode(external_plugin_resources)
}))
def build_attrs(self, base_attrs, extra_attrs=None, **kwargs):
"""
Helper function for building an attribute dictionary.
This is combination of the same method from Django<=1.10 and Django1.11+
"""
attrs = dict(base_attrs, **kwargs)
if extra_attrs:
attrs.update(extra_attrs)
return attrs
def _set_config(self):
lang = get_language()
if lang == 'zh-hans':
lang = 'zh-cn'
elif lang == 'zh-hant':
lang = 'zh'
self.config['language'] = lang