summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorValentin Deniaud <vdeniaud@entrouvert.com>2020-02-24 16:10:16 (GMT)
committerValentin Deniaud <vdeniaud@entrouvert.com>2020-02-24 16:26:19 (GMT)
commitc11b1b08dd2b4f184a67109c0d7729b910b1956a (patch)
treef62ee4cf6c06bc4172973bc9faab09e852698e2d
parent63aa76b8fadf62ed6b46be81ec258781c6ed3a15 (diff)
downloadcombo-master.zip
combo-master.tar.gz
combo-master.tar.bz2
dataviz: handle missing visualization (#40104)HEADmaster
-rw-r--r--combo/apps/dataviz/models.py8
-rw-r--r--combo/apps/dataviz/views.py15
-rw-r--r--tests/test_dataviz.py25
3 files changed, 45 insertions, 3 deletions
diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py
index 52917d7..43af268 100644
--- a/combo/apps/dataviz/models.py
+++ b/combo/apps/dataviz/models.py
@@ -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',
diff --git a/combo/apps/dataviz/views.py b/combo/apps/dataviz/views.py
index 9810d6e..465353d 100644
--- a/combo/apps/dataviz/views.py
+++ b/combo/apps/dataviz/views.py
@@ -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')
diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py
index 6a057f2..6fc0125 100644
--- a/tests/test_dataviz.py
+++ b/tests/test_dataviz.py
@@ -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)'),