Projet

Général

Profil

0001-add-error-template-on-cell-renderer-15768.patch

Thomas Noël, 06 avril 2017 01:24

Télécharger (5,39 ko)

Voir les différences:

Subject: [PATCH] add error template on cell renderer (#15768)

 combo/data/models.py                               |  8 +++-
 .../templates/combo/cell-template-error.html       | 10 +++++
 tests/templates-1/combo/json/badsyntax.html        |  1 +
 tests/test_cells.py                                | 48 ++++++++++++++++++++++
 4 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 combo/public/templates/combo/cell-template-error.html
 create mode 100644 tests/templates-1/combo/json/badsyntax.html
combo/data/models.py
462 462
        if self.slug:
463 463
            template_names.append('combo/cells/%s/%s' % (self.slug, base_template_name))
464 464
        template_names.reverse()
465
        tmpl = template.loader.select_template(template_names)
465
        try:
466
            tmpl = template.loader.select_template(template_names)
467
        except (template.TemplateDoesNotExist, template.TemplateSyntaxError) as exception:
468
            tmpl = template.loader.get_template('combo/cell-template-error.html')
469
            context['exception'] = exception
470
            context['exception_type'] = exception.__class__.__name__
471
            context['template_names'] = template_names
466 472
        return tmpl.render(context)
467 473

  
468 474
    def modify_global_context(self, context, request=None):
combo/public/templates/combo/cell-template-error.html
1
{% load i18n %}
2
{% trans "template error:" %} {{ exception_type }}
3

  
4
{% if debug %}
5
<pre>
6
{% trans "cell:" %} {{ cell }}
7
{% trans "template names:" %} {{ template_names }}
8
{% trans "exception message:" %} {{ exception }}
9
</pre>
10
{% endif %}
tests/templates-1/combo/json/badsyntax.html
1
{% foo_boom_bar %}
tests/test_cells.py
10 10
from django.test import override_settings
11 11
from django.test.client import RequestFactory
12 12
from django.contrib.auth.models import User
13
from django.core.urlresolvers import reverse
13 14

  
14 15
from combo.utils import NothingInCacheException
15 16

  
......
231 232
            context = cell.get_cell_extra_context(Context({'request': request}))
232 233
            assert context['json'] == {'hello': 'world'}
233 234
            assert context['parameters'] == {'blah': 'plop'}
235

  
236
def test_cell_template_error(app):
237
    Page.objects.all().delete()
238
    page = Page(title='example page', slug='example-page')
239
    page.save()
240

  
241
    with override_settings(JSON_CELL_TYPES={'foobar': {'name': 'Foobar', 'url': 'http://test/'},
242
                                            'badsyntax': {'name': 'Error', 'url': 'http://test/'}},
243
                           TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]):
244
        # missing template
245
        cell = ConfigJsonCell()
246
        cell.key = 'foobar'
247
        cell.page = page
248
        cell.order = 0
249
        cell.save()
250

  
251
        url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id,
252
                                                             'cell_reference': cell.get_reference()})
253
        data = {'data': []}
254

  
255
        with mock.patch('combo.utils.requests.get') as requests_get:
256
            requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
257
            with override_settings(DEBUG=False):
258
                resp_body = app.get(url).body.strip()
259
                assert resp_body == 'template error: TemplateDoesNotExist'
260
            with override_settings(DEBUG=True):
261
                resp_body = app.get(url).body.strip()
262
                assert resp_body.startswith('template error: TemplateDoesNotExist')
263
                assert 'cell: config json cell' in resp_body
264
                assert 'template names: [' in resp_body
265
                assert 'exception message: combo/json/foobar.html, combo/configjsoncell.html' in resp_body
266

  
267
        # syntax error (see badsyntax.html)
268
        cell.key = 'badsyntax'
269
        cell.save()
270
        with mock.patch('combo.utils.requests.get') as requests_get:
271
            requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
272
            with override_settings(DEBUG=False):
273
                resp_body = app.get(url).body.strip()
274
                assert resp_body == 'template error: TemplateSyntaxError'
275
            with override_settings(DEBUG=True):
276
                resp_body = app.get(url).body.strip()
277
                assert resp_body.startswith('template error: TemplateSyntaxError')
278
                assert 'cell: config json cell' in resp_body
279
                assert 'template names: [' in resp_body
280
                assert 'exception message: Invalid block tag' in resp_body
281
                assert 'foo_boom_bar' in resp_body
234
-