From 6af74ca84f587e6e166fa48ae7c35fb0bfe5b96f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 24 Feb 2019 09:39:15 +0100
Subject: [PATCH] api: add json view of visualization data (#26847)

---
 bijoe/visualization/urls.py  |  1 +
 bijoe/visualization/views.py | 16 ++++++++++++++++
 tests/test_views.py          | 23 +++++++++++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/bijoe/visualization/urls.py b/bijoe/visualization/urls.py
index fa66a3f..8ac409e 100644
--- a/bijoe/visualization/urls.py
+++ b/bijoe/visualization/urls.py
@@ -30,6 +30,7 @@ urlpatterns = [
     url(r'warehouse/(?P<warehouse>[^/]*)/(?P<cube>[^/]*)/save/$',
         views.create_visualization, name='create-visualization'),
     url(r'(?P<pk>\d+)/$', views.visualization, name='visualization'),
+    url(r'(?P<pk>\d+)/json/$', views.visualization_json, name='visualization-json'),
     url(r'(?P<pk>\d+)/geojson/$', views.visualization_geojson, name='visualization-geojson'),
     url(r'(?P<pk>\d+)/iframe/$', views.visualization_iframe, name='visualization-iframe'),
     url(r'(?P<pk>\d+)/rename/$', views.rename_visualization, name='rename-visualization'),
diff --git a/bijoe/visualization/views.py b/bijoe/visualization/views.py
index ef625ba..a771582 100644
--- a/bijoe/visualization/views.py
+++ b/bijoe/visualization/views.py
@@ -223,9 +223,11 @@ class VisualizationsJSONView(MultipleObjectMixin, View):
             path = reverse('visualization-iframe', kwargs={'pk': visualization.pk})
             sig = hashlib.sha1(path + settings.SECRET_KEY).hexdigest()
             path += '?signature=' + sig
+            data_uri = reverse('visualization-json', kwargs={'pk': visualization.pk})
             data.append({
                 'name': visualization.name,
                 'path': request.build_absolute_uri(path),
+                'data-url': request.build_absolute_uri(data_uri),
             })
         response = HttpResponse(content_type='application/json')
         response.write(json.dumps(data))
@@ -261,6 +263,19 @@ class VisualizationGeoJSONView(generics.GenericAPIView):
         return Response(geojson)
 
 
+class VisualizationJSONView(generics.GenericAPIView):
+    permission_classes = ()
+    queryset = models.Visualization.objects.all()
+
+    def get(self, request, pk, format=None):
+        instance = self.get_object()
+        visualization = Visualization.from_json(instance.parameters, request=request)
+        return Response({
+            'data': [x.data() for x in visualization],
+            'format': '1',
+            })
+
+
 warehouse = WarehouseView.as_view()
 cube = CubeView.as_view()
 cube_iframe = xframe_options_exempt(CubeIframeView.as_view())
@@ -272,6 +287,7 @@ rename_visualization = RenameVisualization.as_view()
 visualization = VisualizationView.as_view()
 visualization_iframe = xframe_options_exempt(VisualizationIframeView.as_view())
 visualization_geojson = VisualizationGeoJSONView.as_view()
+visualization_json = VisualizationJSONView.as_view()
 
 cube_iframe.mellon_no_passive = True
 visualization_iframe.mellon_no_passive = True
diff --git a/tests/test_views.py b/tests/test_views.py
index 1275f44..110d936 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -14,6 +14,10 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from django.core.urlresolvers import reverse
+
+from bijoe.visualization.models import Visualization
+
 from utils import login
 
 
@@ -29,3 +33,22 @@ def test_superuser(app, admin):
     assert len(resp.json) == 1
     assert resp.json[0]['slug'] == 'statistics'
     app.get('/', status=200)
+
+
+def test_visualization_json_api(schema1, app, admin):
+    visualization = Visualization(
+            name='test',
+            parameters={
+                'cube': 'facts1',
+                'warehouse': 'schema1',
+                'measure': 'simple_count',
+                'representation': 'table',
+                'loop': '',
+                'filters': {},
+                'drilldown_x': 'date__yearmonth'})
+    visualization.save()
+
+    login(app, admin)
+    resp = app.get(reverse('visualization-json', kwargs={'pk': visualization.id}))
+    # values from test_schem1/test_yearmonth_drilldown
+    assert [x[-1]['value'] for x in resp.json['data'][0]] == [10, 1, 1, 1, 1, 1, 1, 1]
-- 
2.20.1

