data renderer aggregating multiple source results into one list

This commit is contained in:
Serghei Mihai 2014-11-26 18:48:19 +01:00
parent defce175d0
commit 3dcf1565f4
2 changed files with 48 additions and 0 deletions

View File

@ -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)

View File

@ -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)