From 7a5373c91ed6ef5e1b6eca095a8cf0b28972d5ca Mon Sep 17 00:00:00 2001
From: Valentin Deniaud <vdeniaud@entrouvert.com>
Date: Thu, 21 Nov 2019 11:53:27 +0100
Subject: [PATCH 1/4] utils: get tenant from import rather than request
 (#27412)

---
 bijoe/utils.py               |  7 ++++---
 bijoe/views.py               |  2 +-
 bijoe/visualization/utils.py |  4 ++--
 bijoe/visualization/views.py | 13 ++++++-------
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/bijoe/utils.py b/bijoe/utils.py
index 879f85e..300ba26 100644
--- a/bijoe/utils.py
+++ b/bijoe/utils.py
@@ -19,18 +19,19 @@ import glob
 import json
 
 from django.conf import settings
+from django.db import connection
 from django.utils.translation import ugettext as _
 
 from .schemas import Warehouse
 
 
-def get_warehouses(request=None):
+def get_warehouses():
     warehouses = []
     for pattern in settings.BIJOE_SCHEMAS:
         for path in glob.glob(pattern):
             warehouses.append(Warehouse.from_json(json.load(open(path))))
-    if hasattr(request, 'tenant'):
-        pattern = os.path.join(request.tenant.get_directory(), 'schemas', '*.model')
+    if hasattr(connection, 'tenant'):
+        pattern = os.path.join(connection.tenant.get_directory(), 'schemas', '*.model')
         for path in glob.glob(pattern):
             warehouses.append(Warehouse.from_json(json.load(open(path))))
     return warehouses
diff --git a/bijoe/views.py b/bijoe/views.py
index aa46b81..548af1e 100644
--- a/bijoe/views.py
+++ b/bijoe/views.py
@@ -59,7 +59,7 @@ class HomepageView(AuthorizationMixin, TemplateView):
     def get_context_data(self, **kwargs):
         ctx = super(HomepageView, self).get_context_data(**kwargs)
         ctx['visualizations'] = Visualization.objects.all()
-        ctx['warehouses'] = sorted((Engine(w) for w in get_warehouses(self.request)),
+        ctx['warehouses'] = sorted((Engine(w) for w in get_warehouses()),
                                    key=lambda w: w.label)
         return ctx
 
diff --git a/bijoe/visualization/utils.py b/bijoe/visualization/utils.py
index 5ec242f..89b8418 100644
--- a/bijoe/visualization/utils.py
+++ b/bijoe/visualization/utils.py
@@ -75,8 +75,8 @@ class Visualization(object):
                              filters=copy.deepcopy(self.filters), loop=self.loop)
 
     @classmethod
-    def from_json(cls, d, request=None):
-        for warehouse in get_warehouses(request=request):
+    def from_json(cls, d):
+        for warehouse in get_warehouses():
             if d['warehouse'] == warehouse.name:
                 break
         else:
diff --git a/bijoe/visualization/views.py b/bijoe/visualization/views.py
index 0cd7c26..8d5f91f 100644
--- a/bijoe/visualization/views.py
+++ b/bijoe/visualization/views.py
@@ -46,7 +46,7 @@ class WarehouseView(views.AuthorizationMixin, TemplateView):
     def get_context_data(self, **kwargs):
         ctx = super(WarehouseView, self).get_context_data(**kwargs)
         try:
-            warehouse = [warehouse for warehouse in get_warehouses(self.request)
+            warehouse = [warehouse for warehouse in get_warehouses()
                          if warehouse.name == self.kwargs['warehouse']][0]
         except IndexError:
             raise Http404
@@ -72,7 +72,7 @@ class CubeMixin(object):
 
     def dispatch(self, request, *args, **kwargs):
         try:
-            self.warehouse = Engine([warehouse for warehouse in get_warehouses(self.request)
+            self.warehouse = Engine([warehouse for warehouse in get_warehouses()
                                      if warehouse.name == self.kwargs['warehouse']][0])
         except IndexError:
             raise Http404
@@ -123,8 +123,7 @@ class VisualizationView(views.AuthorizationMixin, CubeDisplayMixin, DetailView):
     def get_object(self):
         named_visualization = super(VisualizationView, self).get_object()
         if not hasattr(self, 'visualization'):
-            self.visualization = Visualization.from_json(named_visualization.parameters,
-                                                         request=self.request)
+            self.visualization = Visualization.from_json(named_visualization.parameters)
         self.cube = self.visualization.cube
         self.warehouse = self.cube.engine
         return named_visualization
@@ -235,7 +234,7 @@ class VisualizationODSView(views.AuthorizationMixin, DetailView):
 
     def get(self, request, *args, **kwargs):
         instance = self.get_object()
-        visualization = Visualization.from_json(instance.parameters, request=request)
+        visualization = Visualization.from_json(instance.parameters)
         response = HttpResponse(content_type='application/vnd.oasis.opendocument.spreadsheet')
         response['Content-Disposition'] = 'attachment; filename=%s.ods' % slugify(instance.name)
         workbook = visualization.ods()
@@ -249,7 +248,7 @@ class VisualizationGeoJSONView(generics.GenericAPIView):
 
     def get(self, request, pk, format=None):
         instance = self.get_object()
-        visualization = Visualization.from_json(instance.parameters, request=request)
+        visualization = Visualization.from_json(instance.parameters)
         visualization.measure = visualization.cube.measures['geolocation']
         drilldown = visualization.drilldown
         geojson = []
@@ -282,7 +281,7 @@ class VisualizationJSONView(generics.GenericAPIView):
 
         instance = self.get_object()
         loop = []
-        all_visualizations = Visualization.from_json(instance.parameters, request=request)
+        all_visualizations = Visualization.from_json(instance.parameters)
         for visualization in all_visualizations:
             drilldowns = visualization.drilldown
             if len(drilldowns) == 2:
-- 
2.20.1

