From 3dcf1565f4f353074254c5a854fe39b8b43c9c4f Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Wed, 26 Nov 2014 18:48:19 +0100 Subject: [PATCH] data renderer aggregating multiple source results into one list --- .../renderers/data_source_list.py | 19 ++++++++++++ src/cmsplugin_blurp/tests.py | 29 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/cmsplugin_blurp/renderers/data_source_list.py diff --git a/src/cmsplugin_blurp/renderers/data_source_list.py b/src/cmsplugin_blurp/renderers/data_source_list.py new file mode 100644 index 0000000..1be4972 --- /dev/null +++ b/src/cmsplugin_blurp/renderers/data_source_list.py @@ -0,0 +1,19 @@ +import logging +log = logging.getLogger(__name__) + +from . import data_source + + +class Renderer(data_source.Renderer): + """ + Aggregates all data from the sources into a list and expose them to the + template with the name of its slug + """ + def render(self, context): + context[self.slug] = [] + for slug, data in self.get_sources(context): + try: + context[self.slug].append(data.content) + except Exception as e: + log.exception("exception occured while extending the list: %s", e) + return super(Renderer, self).render(context) diff --git a/src/cmsplugin_blurp/tests.py b/src/cmsplugin_blurp/tests.py index 9e01288..eb5de71 100644 --- a/src/cmsplugin_blurp/tests.py +++ b/src/cmsplugin_blurp/tests.py @@ -32,6 +32,25 @@ for kind in ('raw', 'json', 'rss', 'xml'): ] } +CMS_PLUGIN_BLURP_LIST_RENDERERS = { + 'json': { + 'template': 'test.html', + 'class': 'cmsplugin_blurp.renderers.data_source_list.Renderer', + 'sources': [ + { + 'slug': 'list_data_source1', + 'parser_type': 'json', + 'url': 'file://' + os.path.join(BASE_FILE, 'json'), + }, + { + 'slug': 'list_data_source2', + 'parser_type': 'json', + 'url': 'file://' + os.path.join(BASE_FILE, 'json'), + } + ] + } +} + @override_settings(CMS_PLUGIN_BLURP_RENDERERS=CMS_PLUGIN_BLURP_RENDERERS) class RendererTestCase(TestCase): def test_choices(self): @@ -78,3 +97,13 @@ class RendererTestCase(TestCase): c = r.render(Context()) self.assertTrue(c.has_key('xml')) self.assertEqual(c['xml']().tag, 'html') + +@override_settings(CMS_PLUGIN_BLURP_RENDERERS=CMS_PLUGIN_BLURP_LIST_RENDERERS) +class DataSourceListRenderTestCase(TestCase): + + def test_data_source_list_render_json(self): + r = utils.resolve_renderer('json') + self.assertIsNotNone(r) + c = r.render(Context()) + self.assertTrue(c.has_key('json')) + self.assertIsInstance(c['json'], list)