Explorar o código

HUE-1754 [dbquery] provide an appropriate view when no databases are configured

Abraham Elmahrek %!s(int64=12) %!d(string=hai) anos
pai
achega
822fb31ca2

+ 72 - 0
apps/rdbms/src/rdbms/templates/error.mako

@@ -0,0 +1,72 @@
+## Licensed to Cloudera, Inc. under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  Cloudera, Inc. licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##     http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+<%!
+  from desktop.views import commonheader, commonfooter
+  from django.utils.translation import ugettext as _
+%>
+
+${ commonheader(_('Query'), app_name, user) | n,unicode }
+
+<div class="navbar navbar-inverse navbar-fixed-top">
+  <div class="navbar-inner">
+    <div class="container-fluid">
+      <div class="nav-collapse">
+        <ul class="nav">
+          <li class="currentApp">
+            <a href="/rdbms">
+              <img src="/rdbms/static/art/icon_rdbms_24.png" />
+              ${ _('DB Query') }
+            </a>
+          </li>
+          <li class="active"><a href="${ url('rdbms:execute_query') }">${_('Query Editor')}</a></li>
+          <li><a href="${ url('rdbms:my_queries') }">${_('My Queries')}</a></li>
+          <li><a href="${ url('rdbms:list_designs') }">${_('Saved Queries')}</a></li>
+          <li><a href="${ url('rdbms:list_query_history') }">${_('History')}</a></li>
+        </ul>
+      </div>
+    </div>
+  </div>
+</div>
+
+<div class="container-fluid">
+  <div class="card">
+    <div class="row-fluid">
+      <div class="span10 offset1 center error-wrapper">
+        <i class="fa fa-cogs"></i>
+        <br />
+        <br />
+        <h1>${_('There are currently no databases configured.')}</h1>
+        <h1>${_('Please go to your Hue configuration and add a database under the "rdbms" section.')}</h1>
+        <br />
+      </div>
+    </div>
+  </div>
+
+</div>
+
+<style type="text/css">
+.error-wrapper {
+  margin-top: 50px;
+  color: #BBB;
+  line-height: 60px;
+}
+
+.error-wrapper i {
+  font-size: 196px;
+}
+</style>
+
+${ commonfooter(messages) | n,unicode }

+ 11 - 0
apps/rdbms/src/rdbms/tests.py

@@ -52,6 +52,17 @@ class TestMockedRdbms:
     response = self.client.get("/rdbms/")
     assert_true('DB Query' in response.content, response.content)
 
+  def test_config_error(self):
+    self.finish = rdbms_conf.RDBMS.set_for_testing({})
+
+    response = self.client.get("/rdbms/")
+    assert_true('There are currently no databases configured.' in response.content)
+
+    response = self.client.get("/rdbms/execute/")
+    assert_true('There are currently no databases configured.' in response.content)
+
+    self.finish()
+
 
 class TestSQLiteRdbmsBase(object):
   @classmethod

+ 19 - 1
apps/rdbms/src/rdbms/views.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from functools import wraps
 import logging
 
 from django.utils.translation import ugettext as _
@@ -27,20 +28,36 @@ from desktop.lib.django_util import render
 from beeswax import models as beeswax_models
 from beeswax.views import safe_get_design
 
+from rdbms import conf
 from rdbms.design import SQLdesign
 
 
 LOG = logging.getLogger(__name__)
 
 
+def ensure_configuration(error_view_func):
+  def _temporary_decorator(view_func):
+    def _decorator(*args, **kwargs):
+      if len(conf.RDBMS.get()) > 0:
+        return view_func(*args, **kwargs)
+      else:
+        return error_view_func(*args, **kwargs)
+    return wraps(view_func)(_decorator)
+  return _temporary_decorator
+
+
 def index(request):
   return execute_query(request)
 
 
+def configuration_error(request, *args, **kwargs):
+  return render('error.mako', request, {})
+
+
 """
 Queries Views
 """
-
+@ensure_configuration(error_view_func=configuration_error)
 def execute_query(request, design_id=None):
   """
   View function for executing an arbitrary synchronously query.
@@ -58,6 +75,7 @@ def execute_query(request, design_id=None):
   })
 
 
+@ensure_configuration(error_view_func=configuration_error)
 def save_design(request, save_form, query_form, type_, design, explicit_save=False):
   """
   save_design(request, save_form, query_form, type_, design, explicit_save) -> SavedQuery