From 89ada40bea72745be4ebae6fedcf0d90d16b290d Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Thu, 4 Jan 2018 19:27:19 +0100 Subject: [PATCH] adapt ckeditor inclusion for Django 1.11 (#20959) --- corbo/__init__.py | 20 ++++++++++++++++++++ corbo/apps.py | 23 +++++++++++++++++++++++ corbo/monkeypatch.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ corbo/urls.py | 10 +++++++++- 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 corbo/apps.py create mode 100644 corbo/monkeypatch.py diff --git a/corbo/__init__.py b/corbo/__init__.py index e69de29..a90930a 100644 --- a/corbo/__init__.py +++ b/corbo/__init__.py @@ -0,0 +1,20 @@ +# corbo - Announces Manager +# Copyright (C) 2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import corbo.monkeypatch + +default_app_config = 'corbo.apps.ManagerAppConfig' diff --git a/corbo/apps.py b/corbo/apps.py new file mode 100644 index 0000000..7903a5e --- /dev/null +++ b/corbo/apps.py @@ -0,0 +1,23 @@ +# corbo - Announces Manager +# Copyright (C) 2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.apps import AppConfig + +class ManagerAppConfig(AppConfig): + name = 'corbo' + + def ready(self): + import corbo.monkeypatch diff --git a/corbo/monkeypatch.py b/corbo/monkeypatch.py new file mode 100644 index 0000000..9162bf5 --- /dev/null +++ b/corbo/monkeypatch.py @@ -0,0 +1,51 @@ +# corbo - Announces Manager +# Copyright (C) 2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.core.urlresolvers import reverse +from django.forms.utils import flatatt +from django.template.loader import render_to_string +from django.utils.encoding import force_text +from django.utils.html import conditional_escape +from django.utils.safestring import mark_safe +from django.utils.translation import get_language + +import ckeditor.widgets + +def ckeditor_render(self, name, value, attrs=None): + if value is None: + value = '' + final_attrs = {'name': name} + if attrs: + final_attrs.update(attrs) + 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] + + return mark_safe(render_to_string('ckeditor/widget.html', { + 'final_attrs': flatatt(final_attrs), + 'value': conditional_escape(force_text(value)), + 'id': final_attrs['id'], + 'config': ckeditor.widgets.json_encode(self.config), + 'external_plugin_resources' : ckeditor.widgets.json_encode(external_plugin_resources) + })) + +ckeditor.widgets.CKEditorWidget.render = ckeditor_render diff --git a/corbo/urls.py b/corbo/urls.py index 227499f..1f3de1b 100644 --- a/corbo/urls.py +++ b/corbo/urls.py @@ -4,18 +4,22 @@ from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.contrib import admin +from django.views.decorators.cache import never_cache +from django.contrib.admin.views.decorators import staff_member_required + from .urls_utils import decorated_includes, manager_required from .views import homepage, atom, unsubscribe, unsubscription_done, login, logout from manage_urls import urlpatterns as manage_urls from api_urls import urlpatterns as api_urls +import ckeditor.views as ckeditor_views + urlpatterns = patterns('', url(r'^$', homepage, name='home'), url(r'^atom$', atom, name='atom'), url(r'^manage/', decorated_includes(manager_required, include(manage_urls))), - url(r'^ckeditor/', include('ckeditor.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^api/', include(api_urls)), url(r'^unsubscribe/done/$', unsubscription_done, @@ -24,6 +28,10 @@ urlpatterns = patterns('', name='unsubscribe'), url(r'^logout/$', logout, name='auth_logout'), url(r'^login/$', login, name='auth_login'), + url(r'^ckeditor/upload/', staff_member_required(ckeditor_views.upload), + name='ckeditor_upload'), + url(r'^ckeditor/browse/', never_cache(staff_member_required(ckeditor_views.browse)), + name='ckeditor_browse'), ) if 'mellon' in settings.INSTALLED_APPS: -- 2.15.1