ソースを参照

HUE-9066 [gist] Adding create API

Romain 6 年 前
コミット
ac68b9c276

+ 20 - 0
desktop/core/src/desktop/api2.py

@@ -821,6 +821,26 @@ def user_preferences(request, key=None):
   return JsonResponse(response)
 
 
+def gist_create(request):
+  response = {'status': 0}
+
+  text = request.POST.get('text', '')
+  name = request.POST.get('name', '')
+  description = request.POST.get('description', '')
+
+  gist_doc = Document2.objects.create(
+    name=name,
+    type='gist',
+    owner=request.user,
+    data=json.dumps({'text': text})
+  )
+
+  response['id'] = gist_doc.id
+  response['uuid'] = gist_doc.uuid
+
+  return JsonResponse(response)
+
+
 def search_entities(request):
   sources = json.loads(request.POST.get('sources')) or []
 

+ 30 - 0
desktop/core/src/desktop/js/api/apiHelper.js

@@ -43,6 +43,7 @@ const SOLR_FIELDS_API = '/indexer/api/index/list/';
 const DASHBOARD_TERMS_API = '/dashboard/get_terms';
 const DASHBOARD_STATS_API = '/dashboard/get_stats';
 const FORMAT_SQL_API = '/notebook/api/format';
+const GIST_API = '/desktop/api2/gist/';
 const TOPO_URL = '/desktop/topo/';
 
 const SEARCH_API = '/desktop/api/search/entities';
@@ -2943,6 +2944,35 @@ class ApiHelper {
 
     return new CancellablePromise(deferred, request);
   }
+
+  /**
+   *
+   * @param {Object} options
+   * @param {string} options.text
+   * @param {string} options.name
+   * @param {string} options.description
+   * @param {boolean} [options.silenceErrors]
+   */
+  createGist(options) {
+    const self = this;
+    const deferred = $.Deferred();
+
+    const request = self.simplePost(
+      GIST_API,
+      {
+        text: options.text,
+        name: options.name,
+        description: options.description
+      },
+      {
+        silenceErrors: options.silenceErrors,
+        successCallback: deferred.resolve,
+        errorCallback: deferred.reject
+      }
+    );
+
+    return new CancellablePromise(deferred, request);
+  }
 }
 
 const apiHelper = new ApiHelper();

+ 22 - 0
desktop/core/src/desktop/js/apps/notebook/snippet.js

@@ -1911,6 +1911,28 @@ class Snippet {
       ); // ie: 5000 lines at 80 chars per line
     });
 
+    self.createGist = function() {
+      if (self.isSqlDialect()) {
+        apiHelper
+          .createGist({
+            text:
+              self.ace().getSelectedText() != ''
+                ? self.ace().getSelectedText()
+                : self.statement_raw(),
+            name: self.name(),
+            description: ''
+          })
+          .done(data => {
+            if (data.status == 0) {
+              $(document).trigger('info', 'Gist created at ..' + data);
+            } else {
+              self._ajaxError(data);
+            }
+          });
+      }
+      hueAnalytics.log('notebook', 'format');
+    };
+
     self.format = function() {
       if (self.isSqlDialect()) {
         apiHelper

+ 2 - 0
desktop/core/src/desktop/urls.py

@@ -167,6 +167,8 @@ dynamic_patterns += [
   url(r'^desktop/api2/doc/export/?$', desktop_api2.export_documents),
   url(r'^desktop/api2/doc/import/?$', desktop_api2.import_documents),
 
+  url(r'^/desktop/api2/gist/?$', desktop_api2.create_gist),
+
   url(r'^desktop/api/search/entities/?$', desktop_api2.search_entities),
   url(r'^desktop/api/search/entities_interactive/?$', desktop_api2.search_entities_interactive),
 ]

+ 1 - 1
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1794,7 +1794,7 @@ ${ sqlSyntaxDropdown.sqlSyntaxDropdown() }
           </a>
         </li>
         <li>
-          <a href="javascript:void(0)" data-bind="click: clear, css: {'disabled': ! isReady() }" title="${ _('Create a gist link for sharing the selected SQL queries') }">
+          <a href="javascript:void(0)" data-bind="click: createGist, css: {'disabled': ! isReady() }" title="${ _('Create a gist link for sharing the selected SQL queries') }">
             <i class="fa fa-fw fa-link"></i> ${_('Share')}
           </a>
         </li>