dataviz: handle missing visualization (#40104)

This commit is contained in:
Valentin Deniaud 2020-02-24 17:10:16 +01:00
parent 63aa76b8fa
commit c11b1b08dd
3 changed files with 45 additions and 3 deletions

View File

@ -24,6 +24,7 @@ from django.utils.translation import ugettext_lazy as _, ungettext, gettext
from django.conf import settings
from jsonfield import JSONField
from requests.exceptions import HTTPError
import pygal
from combo.data.models import CellBase
@ -166,6 +167,9 @@ class ChartNgCell(CellBase):
chart = self.get_chart(raise_if_not_cached=not(context.get('synchronous')))
except UnsupportedDataSet:
ctx['table'] = '<p>%s</p>' % _('Unsupported dataset.')
except HTTPError as e:
if e.response.status_code == 404:
ctx['table'] = '<p>%s</p>' % _('Visualization not found.')
else:
ctx['table'] = chart.render_table(
transpose=bool(chart.axis_count == 2),
@ -178,7 +182,9 @@ class ChartNgCell(CellBase):
response = requests.get(
self.cached_json['data-url'],
cache_duration=300,
raise_if_not_cached=raise_if_not_cached).json()
raise_if_not_cached=raise_if_not_cached)
response.raise_for_status()
response = response.json()
style = pygal.style.DefaultStyle(
font_family='OpenSans, sans-serif',

View File

@ -18,6 +18,8 @@ from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, Http404
from django.utils.translation import ugettext_lazy as _
from requests.exceptions import HTTPError
from combo.utils import get_templated_url, requests
from .models import Gauge, ChartNgCell, UnsupportedDataSet
@ -36,12 +38,21 @@ def dataviz_graph(request, *args, **kwargs):
raise PermissionDenied()
if not cell.cached_json:
raise Http404('misconfigured cell')
error_text = None
try:
chart = cell.get_chart(
width=int(request.GET.get('width', 0)) or None,
height=int(request.GET.get('height', 0)) or int(cell.height)
)
except UnsupportedDataSet:
except UnsupportedDataSet as e:
error_text = _('Unsupported dataset.')
except HTTPError as e:
if e.response.status_code == 404:
error_text = _('Visualization not found.')
else:
error_text = _('Unknown HTTP error: %s' % e)
if error_text:
svg = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
viewBox="0 0 %(width)s 30" width="%(width)s" height="30">
@ -50,7 +61,7 @@ def dataviz_graph(request, *args, **kwargs):
x="10"
style="font-family: sans-serif; font-size: 16px; fill:#000000;">%(text)s</text>
</svg>""" % {'width': request.GET.get('width', 200),
'text': _('Unsupported dataset.')}
'text': error_text}
else:
svg = chart.render()
return HttpResponse(svg, content_type='image/svg+xml')

View File

@ -3,6 +3,7 @@ import json
import mock
import pytest
from httmock import HTTMock
from requests.exceptions import HTTPError
from django.contrib.auth.models import User, Group
from django.test import override_settings
@ -106,6 +107,12 @@ VISUALIZATION_JSON = [
'name': 'tenth visualization (percents)',
'slug': 'tenth',
},
{
'data-url': 'https://bijoe.example.com/visualization/11/json/',
'path': 'https://bijoe.example.com/visualization/11/iframe/?signature=123',
'name': 'eleventh visualization (not found)',
'slug': 'eleventh',
},
]
@ -232,6 +239,11 @@ def bijoe_mock(url, request):
'measure': 'percent',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/11/json/':
response = {
'detail': 'not found',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 404}
@ -342,6 +354,12 @@ def test_chartng_cell(app):
with pytest.raises(UnsupportedDataSet):
chart = cell.get_chart()
# deleted visualization
cell.data_reference = 'plop:eleventh'
cell.save()
with pytest.raises(HTTPError):
chart = cell.get_chart()
def test_chartng_cell_view(app, normal_user):
page = Page(title='One', slug='index')
@ -432,6 +450,12 @@ def test_chartng_cell_view(app, normal_user):
resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
assert '>10.0%<' in resp.text
# deleted visualization
cell.data_reference = 'plop:eleventh'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
assert 'not found' in resp.text
# cell with missing cached_json (probably after import and missing
# bijoe visualisation)
cell.chart_type = 'table'
@ -457,6 +481,7 @@ def test_chartng_cell_manager(app, admin_user):
resp = app.get('/manage/pages/%s/' % page.id)
assert resp.form['cdataviz_chartngcell-%s-data_reference' % cell.id].options == [
(u'plop:eighth', False, u'eighth visualization (duration)'),
(u'plop:eleventh', False, u'eleventh visualization (not found)'),
(u'plop:example', True, u'example visualization (X)'),
(u'plop:fifth', False, u'fifth visualization (loop/X)'),
(u'plop:fourth', False, u'fourth visualization (no axis)'),