renderers: add debug content to templates when DEBUG is True (fixes #5092)
The debug content for the data_source renderer is (using pseudo-template code): <!-- DEBUG: config: {{ blurp.config|pprint }} {% for each source %} slug {{ source.slug }}: {% source.content|pprint %} {% endfor %} --> The debug content is extracted from a template context variable named 'blurp_debug__' that any blurp renderer can set when DEBUG is True.
This commit is contained in:
parent
ba5d718042
commit
d73a37fb1d
|
@ -3,6 +3,7 @@ import hashlib
|
|||
from xml.etree import ElementTree as ET
|
||||
import time
|
||||
import threading
|
||||
import pprint
|
||||
|
||||
import feedparser
|
||||
import requests
|
||||
|
@ -81,11 +82,28 @@ class Renderer(template.TemplateRenderer):
|
|||
slug = '{0}.{1}'.format(self.slug, source['slug'])
|
||||
data = Data(slug, self.config, source, context)
|
||||
yield source['slug'], data
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
yield 'blurp_debug__', '\n'.join(self.debug_content(context))
|
||||
|
||||
def debug_content(self, context):
|
||||
try:
|
||||
yield u'config: {0}'.format(pprint.pformat(self.config))
|
||||
except Exception, e:
|
||||
yield u'config: pformat failed {0!r}'.format(e)
|
||||
for source in self.config.get('sources', []):
|
||||
slug = source.get('slug')
|
||||
if not slug:
|
||||
continue
|
||||
try:
|
||||
yield u'slug {0!r}: {1}'.format(slug,
|
||||
pprint.pformat(context.get(slug)))
|
||||
except Exception, e:
|
||||
yield u'slug {0!r}: pformat failed {1!r}'.format(slug, e)
|
||||
|
||||
def render(self, context):
|
||||
for slug, source in self.get_sources(context):
|
||||
context[slug] = source
|
||||
return context
|
||||
return super(Renderer, self).render(context)
|
||||
|
||||
class Data(object):
|
||||
'''Encapsulate data from a source'''
|
||||
|
@ -317,3 +335,4 @@ class Data(object):
|
|||
|
||||
def __call__(self):
|
||||
return self.get_content()
|
||||
|
||||
|
|
|
@ -2,7 +2,8 @@ import logging
|
|||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.template.loader import get_template
|
||||
from django.template import TemplateDoesNotExist, Template
|
||||
from django.template import Template
|
||||
from django.conf import settings
|
||||
|
||||
from .base import BaseRenderer
|
||||
|
||||
|
@ -20,16 +21,28 @@ class TemplateRenderer(BaseRenderer):
|
|||
raise ImproperlyConfigured('{0} configuration is missing a template key: {1!r}'.format(
|
||||
cls.__name__, config))
|
||||
|
||||
def render(self, context):
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
context['__blurb'] = self
|
||||
return context
|
||||
|
||||
def render_template(self):
|
||||
'''First try to get a template by path, then compile the inline
|
||||
template, and if none of that works show an error message.'''
|
||||
|
||||
if 'template_name' in self.config:
|
||||
try:
|
||||
return get_template(self.config['template_name'])
|
||||
template = get_template(self.config['template_name'])
|
||||
except TemplateDoesNotExist:
|
||||
pass
|
||||
if 'template' in self.config:
|
||||
return Template(self.config['template'])
|
||||
log.error('template not found: %r', self.config)
|
||||
return 'cmsplugin_blurp/template_not_found.html'
|
||||
elif 'template' in self.config:
|
||||
template = Template(self.config['template'])
|
||||
else:
|
||||
log.error('template not found: %r', self.config)
|
||||
template = 'cmsplugin_blurp/template_not_found.html'
|
||||
if settings.TEMPLATE_DEBUG:
|
||||
if not hasattr(template, 'render'):
|
||||
template = get_template(template)
|
||||
debug_template = Template('<!-- DEBUG:\n{{ blurp_debug__ }}\n-->')
|
||||
template.nodelist.extend(debug_template.nodelist)
|
||||
return template
|
||||
|
|
Reference in New Issue