Sfoglia il codice sorgente

HUE-8758 [connectors] Allow organization admin to install connector examples

Also refresh the connector counts dynamically post install.
Romain 5 anni fa
parent
commit
5d0f32fe32

+ 19 - 4
apps/about/src/about/templates/admin_wizard.mako

@@ -78,17 +78,19 @@ ${ layout.menubar(section='quick_start') }
 
           <div id="step2" class="stepDetails hide">
             <h3>${ _('Connectors to data services') }</h3>
+            <span id="connectorCounts">...</span> ${ _('connectors are installed.') }
+
             % if has_connectors():
-            <ul class="unstyled samples">
+            <ul class="unstyled samples margin-top-20">
               <li>
                 <a href="${ url('desktop.lib.connectors.views.index') }" title="${ _('Open the connector configuration page') }">
                   <i class="fa fa-exchange"></i> ${ _('Configuration page') }
                 </a>
               </li>
               <li>
-                <a href="javascript:void(0)" class="installBtn" title="${ _('Install the connector examples') }"
+                <a href="javascript:void(0)" class="installBtn" title="${ _('Install the connector examples') }" data-is-connector="true"
                   data-loading-text="${ _('Installing...') }" data-sample-url="${ url('connectors.api.install_connector_examples') }">
-                  <i class="fa fa-download"></i> ${ _('Install Examples') }
+                  <i class="fa fa-download"></i> ${ _('Install examples') }
                 </a>
               </li>
             </ul>
@@ -305,12 +307,25 @@ $(document).ready(function(){
 
   $("[rel='popover']").popover();
 
+  % if has_connectors():
+    huePubSub.subscribe('cluster.config.set.config', config => {
+      if (config && config.app_config && config.app_config.editor) {
+        $('#connectorCounts').text(config.app_config.editor.interpreter_names.filter(c => c != 'notebook').length);
+      }
+    });
+
+    huePubSub.publish('cluster.config.refresh.config');
+  % endif
+
   $(".installBtn").click(function() {
     var button = $(this);
     $(button).button('loading');
-    $.post($(this).data("sample-url"), function(data) {
+    $.post(button.data("sample-url"), function(data) {
       if (data.status == 0) {
         $(document).trigger('info','${ _("Examples refreshed") }');
+        if ($(button).data("is-connector")) {
+          huePubSub.publish('cluster.config.refresh.config');
+        }
       } else {
         $(document).trigger('error', data.message);
       }

+ 20 - 1
desktop/core/src/desktop/decorators.py

@@ -21,7 +21,8 @@ from django.utils.translation import ugettext as _
 
 from desktop.auth.backend import is_admin
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.models import Document2
+from desktop.lib.django_util import JsonResponse
+from desktop.lib.i18n import force_unicode
 
 try:
   from functools import wraps
@@ -68,6 +69,7 @@ def check_document_access_permission():
           doc_id['id'] = kwargs['doc_id']
 
         if doc_id:
+          from desktop.models import Document2
           doc2 = Document2.objects.get(**doc_id)
           doc2.doc.get().can_read_or_exception(request.user)
       except Document2.DoesNotExist:
@@ -76,3 +78,20 @@ def check_document_access_permission():
       return view_func(request, *args, **kwargs)
     return wraps(view_func)(decorate)
   return inner
+
+
+def api_error_handler(func):
+  def decorator(*args, **kwargs):
+    response = {}
+
+    try:
+      return func(*args, **kwargs)
+    except Exception as e:
+      LOG.exception('Error running %s' % func)
+      response['status'] = -1
+      response['message'] = force_unicode(str(e))
+    finally:
+      if response:
+        return JsonResponse(response)
+
+  return decorator

+ 3 - 1
desktop/core/src/desktop/lib/connectors/api.py

@@ -23,6 +23,7 @@ from django.utils.translation import ugettext as _
 from useradmin.models import update_app_permissions
 
 from desktop.auth.decorators import admin_required
+from desktop.decorators import api_error_handler
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.connectors.models import _get_installed_connectors, get_connectors_types, Connector, _create_connector_examples
@@ -112,6 +113,7 @@ def delete_connector(request):
 
 
 @admin_required
+@api_error_handler
 def install_connector_examples(request):
   try:
     _create_connector_examples()
@@ -120,7 +122,7 @@ def install_connector_examples(request):
 
   update_app_permissions()
 
-  return JsonResponse({})
+  return JsonResponse({'status': 0})