From da33884cd28f7b2d1f1fcd0c04fb256e383522e3 Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Wed, 11 Dec 2013 14:29:52 +0100 Subject: [PATCH] Initial commit --- __init__.py | 1 + cms_plugins.py | 29 +++++++++++++++++++++++++++++ templates/plugin/ajax_text.html | 18 ++++++++++++++++++ urls.py | 7 +++++++ views.py | 11 +++++++++++ 5 files changed, 66 insertions(+) create mode 100644 __init__.py create mode 100644 cms_plugins.py create mode 100644 templates/plugin/ajax_text.html create mode 100644 urls.py create mode 100644 views.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ + diff --git a/cms_plugins.py b/cms_plugins.py new file mode 100644 index 0000000..96eb85b --- /dev/null +++ b/cms_plugins.py @@ -0,0 +1,29 @@ +from django.utils.translation import ugettext_lazy as _ +from django.template import loader + +from cmsplugin_text_wrapper.cms_plugins import TextPlugin +from cms.plugin_pool import plugin_pool + +class AjaxTextPlugin(TextPlugin): + name = _(u'Text (asynchronous loading)') + ajax_render_template = 'plugin/ajax_text.html' + + def get_ajax_body(self, instance, context): + t = loader.get_template(self.ajax_render_template) + return t.render(context) + + def render(self, context, instance, placeholder): + context = super(AjaxTextPlugin, self).render(context, instance, placeholder) + context['object'] = instance + request = context.get('request') + edit_mode = request and 'edit' in request.GET + is_ajax = request and request.is_ajax() + if edit_mode or is_ajax: + return context + + context['body'] = self.get_ajax_body(instance, context) + return context + + + +plugin_pool.register_plugin(AjaxTextPlugin) diff --git a/templates/plugin/ajax_text.html b/templates/plugin/ajax_text.html new file mode 100644 index 0000000..4f98780 --- /dev/null +++ b/templates/plugin/ajax_text.html @@ -0,0 +1,18 @@ +{% load i18n %} +{% load sekizai_tags %} + +{% addtoblock "js" %}{% endaddtoblock %} + +{% addtoblock "js" %} + +{% endaddtoblock %} + +
+ {% trans "loading..." %} +
+ + diff --git a/urls.py b/urls.py new file mode 100644 index 0000000..d3b568c --- /dev/null +++ b/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import patterns, url +from views import ajax_render + +urlpatterns = patterns('', + url(r'^async_text/(?P\d+)/$', ajax_render, + name = 'ajax_render') + ) diff --git a/views.py b/views.py new file mode 100644 index 0000000..b0e19b8 --- /dev/null +++ b/views.py @@ -0,0 +1,11 @@ +from django.http import HttpResponse +from django.template import RequestContext + + +from cms.models import CMSPlugin + +def ajax_render(request, plugin_id): + plugin = CMSPlugin.objects.get(pk=plugin_id) + context = RequestContext(request) + rendered = plugin.render_plugin(context) + return HttpResponse(rendered)