1 # combo - content management system
2 # Copyright (C) 2018 Entr'ouvert
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from django.core.urlresolvers import reverse
18 from django.forms.utils import flatatt
19 from django.template.loader import render_to_string
20 from django.utils.encoding import force_text
21 from django.utils.html import conditional_escape
22 from django.utils.safestring import mark_safe
23 from django.utils.translation import get_language
26 import ckeditor.widgets
27 from ckeditor.image import pillow_backend
30 def ckeditor_render(self, name, value, attrs=None):
33 final_attrs = {'name': name}
34 if getattr(self, 'attrs', None):
35 final_attrs.update(self.attrs)
37 final_attrs.update(attrs)
38 if 'filebrowserUploadUrl' not in self.config:
39 self.config.setdefault('filebrowserUploadUrl', reverse('ckeditor_upload'))
40 if 'filebrowserBrowseUrl' not in self.config:
41 self.config.setdefault('filebrowserBrowseUrl', reverse('ckeditor_browse'))
42 if not self.config.get('language'):
43 self.config['language'] = get_language()
45 # Force to text to evaluate possible lazy objects
46 external_plugin_resources = [
47 [force_text(a), force_text(b), force_text(c)] for a, b, c in self.external_plugin_resources
52 'ckeditor/widget.html',
54 'final_attrs': flatatt(final_attrs),
55 'value': conditional_escape(force_text(value)),
56 'id': final_attrs['id'],
57 'config': ckeditor.widgets.json_encode(self.config),
58 'external_plugin_resources': ckeditor.widgets.json_encode(external_plugin_resources),
64 ckeditor.widgets.CKEditorWidget.render = ckeditor_render
66 orig_should_create_thumbnail = pillow_backend.should_create_thumbnail
69 def should_create_thumbnail(file_path):
70 if file_path.endswith('.gif'):
71 # disable thumbnails for gif to workaround
72 # https://github.com/python-pillow/Pillow/issues/2803
74 return orig_should_create_thumbnail(file_path)
78 backend = pillow_backend
79 backend.should_create_thumbnail = should_create_thumbnail
83 ckeditor.views.image_processing.get_backend = get_backend