]> git.0d.be Git - chloro.git/blob - chloro/monkeypatch.py
do not include non-feed posts on homepage
[chloro.git] / chloro / monkeypatch.py
1 # combo - content management system
2 # Copyright (C) 2018  Entr'ouvert
3 #
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.
8 #
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.
13 #
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/>.
16
17 import ckeditor.views
18 import ckeditor.widgets
19 from ckeditor.image import pillow_backend
20 from django.forms.utils import flatatt
21 from django.template.loader import render_to_string
22 from django.urls import reverse
23 from django.utils.encoding import force_text
24 from django.utils.html import conditional_escape
25 from django.utils.safestring import mark_safe
26 from django.utils.translation import get_language
27
28
29 def ckeditor_render(self, name, value, attrs=None, renderer=None):
30     if value is None:
31         value = ''
32     final_attrs = {'name': name}
33     if getattr(self, 'attrs', None):
34         final_attrs.update(self.attrs)
35     if attrs:
36         final_attrs.update(attrs)
37     if 'filebrowserUploadUrl' not in self.config:
38         self.config.setdefault('filebrowserUploadUrl', reverse('ckeditor_upload'))
39     if 'filebrowserBrowseUrl' not in self.config:
40         self.config.setdefault('filebrowserBrowseUrl', reverse('ckeditor_browse'))
41     if not self.config.get('language'):
42         self.config['language'] = get_language()
43
44     # Force to text to evaluate possible lazy objects
45     external_plugin_resources = [
46         [force_text(a), force_text(b), force_text(c)] for a, b, c in self.external_plugin_resources
47     ]
48
49     return mark_safe(
50         render_to_string(
51             'ckeditor/widget.html',
52             {
53                 'final_attrs': flatatt(final_attrs),
54                 'value': conditional_escape(force_text(value)),
55                 'id': final_attrs['id'],
56                 'config': ckeditor.widgets.json_encode(self.config),
57                 'external_plugin_resources': ckeditor.widgets.json_encode(external_plugin_resources),
58             },
59         )
60     )
61
62
63 ckeditor.widgets.CKEditorWidget.render = ckeditor_render
64
65 orig_should_create_thumbnail = pillow_backend.should_create_thumbnail
66
67
68 def should_create_thumbnail(file_path):
69     if file_path.endswith('.gif'):
70         # disable thumbnails for gif to workaround
71         # https://github.com/python-pillow/Pillow/issues/2803
72         return False
73     return orig_should_create_thumbnail(file_path)
74
75
76 def get_backend():
77     backend = pillow_backend
78     backend.should_create_thumbnail = should_create_thumbnail
79     return backend
80
81
82 ckeditor.views.image_processing.get_backend = get_backend