debian-django-ckeditor/ckeditor/widgets.py

127 lines
4.8 KiB
Python
Raw Normal View History

2010-03-31 16:46:17 +02:00
from django import forms
from django.conf import settings
2015-07-07 20:35:43 +02:00
from django.core.exceptions import ImproperlyConfigured
2010-03-31 17:05:15 +02:00
from django.core.urlresolvers import reverse
2015-07-07 20:35:43 +02:00
from django.core.serializers.json import DjangoJSONEncoder
2011-09-28 13:26:30 +02:00
from django.template.loader import render_to_string
from django.utils.encoding import force_text
2015-07-07 20:35:43 +02:00
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
2015-07-07 20:35:43 +02:00
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)
2010-03-31 16:46:17 +02:00
json_encode = LazyEncoder().encode
2010-12-09 12:43:29 +01:00
DEFAULT_CONFIG = {
2013-03-17 06:53:54 +01:00
'skin': 'moono',
'toolbar_Basic': [
['Source', '-', 'Bold', 'Italic']
],
'toolbar_Full': [
2013-03-17 06:53:54 +01:00
['Styles', 'Format', 'Bold', 'Italic', 'Underline', 'Strike', 'SpellChecker', 'Undo', 'Redo'],
['Link', 'Unlink', 'Anchor'],
2013-03-17 06:53:54 +01:00
['Image', 'Flash', 'Table', 'HorizontalRule'],
['TextColor', 'BGColor'],
['Smiley', 'SpecialChar'], ['Source'],
],
'toolbar': 'Full',
2010-12-09 12:43:29 +01:00
'height': 291,
'width': 835,
2010-12-09 12:43:29 +01:00
'filebrowserWindowWidth': 940,
'filebrowserWindowHeight': 725,
2010-12-09 12:43:29 +01:00
}
2011-08-30 12:31:49 +02:00
2010-03-31 16:46:17 +02:00
class CKEditorWidget(forms.Textarea):
2010-03-31 17:05:15 +02:00
"""
Widget providing CKEditor for Rich Text Editing.
Supports direct image uploads and embed.
"""
2010-03-31 16:46:17 +02:00
class Media:
js = ()
jquery_url = getattr(settings, 'CKEDITOR_JQUERY_URL', None)
if jquery_url:
js += (jquery_url, )
2010-06-22 13:05:56 +02:00
try:
js += (
settings.STATIC_URL + 'ckeditor/ckeditor/ckeditor.js',
settings.STATIC_URL + 'ckeditor/ckeditor-init.js',
2010-06-22 13:05:56 +02:00
)
except AttributeError:
2011-08-30 12:31:49 +02:00
raise ImproperlyConfigured("django-ckeditor requires \
CKEDITOR_MEDIA_PREFIX setting. This setting specifies a \
URL prefix to the ckeditor JS and CSS media (not \
uploaded media). Make sure to use a trailing slash: \
CKEDITOR_MEDIA_PREFIX = '/media/ckeditor/'")
def __init__(self, config_name='default', extra_plugins=None, external_plugin_resources=None, *args, **kwargs):
super(CKEditorWidget, self).__init__(*args, **kwargs)
2010-12-09 12:43:29 +01:00
# Setup config from defaults.
self.config = DEFAULT_CONFIG.copy()
2010-12-09 12:43:29 +01:00
# Try to get valid config from settings.
configs = getattr(settings, 'CKEDITOR_CONFIGS', None)
2013-03-17 06:55:02 +01:00
if configs:
2010-12-09 13:03:48 +01:00
if isinstance(configs, dict):
# Make sure the config_name exists.
2012-04-20 17:34:39 +02:00
if config_name in configs:
2010-12-09 13:03:48 +01:00
config = configs[config_name]
# Make sure the configuration is a dictionary.
if not isinstance(config, dict):
2011-08-30 12:31:49 +02:00
raise ImproperlyConfigured('CKEDITOR_CONFIGS["%s"] \
setting must be a dictionary type.' % \
config_name)
2010-12-09 13:03:48 +01:00
# Override defaults with settings config.
self.config.update(config)
else:
2011-08-30 12:31:49 +02:00
raise ImproperlyConfigured("No configuration named '%s' \
found in your CKEDITOR_CONFIGS setting." % \
config_name)
2010-12-09 12:43:29 +01:00
else:
2011-08-30 12:31:49 +02:00
raise ImproperlyConfigured('CKEDITOR_CONFIGS setting must be a\
dictionary type.')
extra_plugins = extra_plugins or []
if extra_plugins:
self.config['extraPlugins'] = ','.join(extra_plugins)
self.external_plugin_resources = external_plugin_resources or []
2011-08-30 12:31:49 +02:00
2010-03-31 16:46:17 +02:00
def render(self, name, value, attrs={}):
2011-08-30 12:31:49 +02:00
if value is None:
value = ''
2010-03-31 16:46:17 +02:00
final_attrs = self.build_attrs(attrs, name=name)
2015-01-25 13:59:06 +01:00
if 'filebrowserUploadUrl' not in self.config:
self.config.setdefault('filebrowserUploadUrl', reverse('ckeditor_upload'))
if 'filebrowserBrowseUrl' not in self.config:
self.config.setdefault('filebrowserBrowseUrl', reverse('ckeditor_browse'))
if not self.config.get('language'):
self.config['language'] = get_language()
# Force to text to evaluate possible lazy objects
external_plugin_resources = [[force_text(a), force_text(b), force_text(c)] for a, b, c in self.external_plugin_resources]
2011-09-28 13:26:30 +02:00
return mark_safe(render_to_string('ckeditor/widget.html', {
'final_attrs': flatatt(final_attrs),
'value': conditional_escape(force_text(value)),
2011-09-28 13:26:30 +02:00
'id': final_attrs['id'],
'config': json_encode(self.config),
'external_plugin_resources' : json_encode(external_plugin_resources)
2013-03-17 06:55:02 +01:00
}))