From ebb9cb3e395da5ef7753238532520bbdbe6ee8da Mon Sep 17 00:00:00 2001
From: Valentin Deniaud <vdeniaud@entrouvert.com>
Date: Tue, 24 Dec 2019 16:47:46 +0100
Subject: [PATCH 3/4] visualization: add warehouse slug support (#38596)

---
 bijoe/schemas.py                      |  4 +++-
 bijoe/visualization/utils.py          |  6 +++++-
 tests/fixtures/schema1/01_schema.json |  1 +
 tests/test_views.py                   | 19 ++++++++++++++++++-
 4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/bijoe/schemas.py b/bijoe/schemas.py
index 751c3bb..27ee824 100644
--- a/bijoe/schemas.py
+++ b/bijoe/schemas.py
@@ -395,9 +395,10 @@ class Cube(Base):
 
 
 class Warehouse(Base):
-    __slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes', 'path']
+    __slots__ = ['name', 'slug', 'label', 'pg_dsn', 'search_path', 'cubes', 'path']
     __types__ = {
         'name': str,
+        'slug': str,
         'label': unicode,
         'pg_dsn': str,
         'search_path': [str],
@@ -406,6 +407,7 @@ class Warehouse(Base):
     }
 
     path = None
+    slug = None
 
     def check(self):
         names = collections.Counter(cube.name for cube in self.cubes)
diff --git a/bijoe/visualization/utils.py b/bijoe/visualization/utils.py
index 034b141..9762ae5 100644
--- a/bijoe/visualization/utils.py
+++ b/bijoe/visualization/utils.py
@@ -61,6 +61,7 @@ class Visualization(object):
     def to_json(self):
         return {
             'warehouse': self.cube.engine.warehouse.name,
+            'warehouse_slug': self.cube.engine.warehouse.slug,
             'cube': self.cube.name,
             'representation': self.representation,
             'measure': self.measure and self.measure.name,
@@ -80,7 +81,10 @@ class Visualization(object):
         if not warehouses:
             warehouses = get_warehouses()
         for warehouse in warehouses:
-            if d['warehouse'] == warehouse.name:
+            if warehouse.slug and 'warehouse_slug' in d:
+                if d['warehouse_slug'] == warehouse.slug:
+                    break
+            elif d['warehouse'] == warehouse.name:  # legacy
                 break
         else:
             raise Http404('warehouse %s not found' % d['warehouse'])
diff --git a/tests/fixtures/schema1/01_schema.json b/tests/fixtures/schema1/01_schema.json
index 06ad255..8f28554 100644
--- a/tests/fixtures/schema1/01_schema.json
+++ b/tests/fixtures/schema1/01_schema.json
@@ -241,6 +241,7 @@
     ],
     "label": "test schema1",
     "name": "schema1",
+    "slug": "schema1_slug",
     "pg_dsn": "fixme",
     "search_path": [
         "schema1"
diff --git a/tests/test_views.py b/tests/test_views.py
index 1b18460..a7ade06 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -40,9 +40,10 @@ def test_visualizations_json_api(schema1, app, admin):
     Visualization(name='test', parameters={'warehouse': 'schema1', 'cube': 'test'}).save()
     Visualization(name='test', parameters={'warehouse': 'schema1', 'cube': 'test'}).save()
     Visualization(name='test', parameters={'warehouse': 'schema1', 'cube': 'test'}).save()
+    Visualization(name='test', parameters={'warehouse_slug': 'schema1_slug', 'cube': 'test'}).save()
     login(app, admin)
     resp = app.get(reverse('visualizations-json'))
-    assert set([x['slug'] for x in resp.json]) == set(['test', 'test-2', 'test-3'])
+    assert set([x['slug'] for x in resp.json]) == set(['test', 'test-2', 'test-3', 'test-4'])
 
 
 def test_visualization_json_api(schema1, app, admin):
@@ -110,3 +111,19 @@ def test_missing_data(schema1, app, admin):
 
     response = app.get('/')
     assert response.pyquery('ul li a.disabled').text() == visualization.name
+
+
+def test_visualization_creation_view(schema1, app, admin):
+    login(app, admin)
+    response = app.get('/').follow()
+    response = response.click('Facts 1')
+    form = response.form
+    form.set('representation', 'table')
+    form.set('measure', 'simple_count')
+    response = form.submit('visualize')
+    response = response.click(href='save')
+    response.form['name'] = 'test'
+    response.form.submit()
+
+    visu = Visualization.objects.get(name='test')
+    assert visu.parameters['warehouse_slug'] == 'schema1_slug'
-- 
2.20.1

