Browse Source

HUE-8330 [importer] Saving pipeline API skeleton

Romain Rigaux 7 years ago
parent
commit
b409450

+ 20 - 0
desktop/libs/indexer/src/indexer/api3.py

@@ -22,8 +22,10 @@ import urllib
 import StringIO
 
 from urlparse import urlparse
+
 from django.urls import reverse
 from django.utils.translation import ugettext as _
+from django.views.decorators.http import require_POST
 from simple_salesforce.api import Salesforce
 
 from desktop.lib import django_mako
@@ -46,6 +48,7 @@ from indexer.indexers.rdbms import run_sqoop, _get_api
 from indexer.indexers.sql import SQLIndexer
 from indexer.solr_client import SolrClient, MAX_UPLOAD_SIZE
 from indexer.indexers.flume import FlumeIndexer
+from indexer.models import _save_pipeline
 
 
 LOG = logging.getLogger(__name__)
@@ -660,3 +663,20 @@ def _create_solr_collection(user, fs, client, destination, index_name, kwargs):
         shards=destination['indexerNumShards'],
         replication=destination['indexerReplicationFactor']
     )
+
+@api_error_handler
+@require_POST
+# @check_document_modify_permission()
+def save_pipeline(request):
+  response = {'status': -1}
+
+  notebook = json.loads(request.POST.get('notebook', '{}'))
+
+  notebook_doc, save_as = _save_pipeline(notebook, request.user)
+
+  response['status'] = 0
+  response['save_as'] = save_as
+  response.update(notebook_doc.to_dict())
+  response['message'] = request.POST.get('editorMode') == 'true' and _('Query saved successfully') or _('Notebook saved successfully')
+
+  return JsonResponse(response)

+ 35 - 0
desktop/libs/indexer/src/indexer/models.py

@@ -15,6 +15,41 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from desktop.models import Document2, Document
+
+
+def _save_pipeline(pipeline, user):
+  pipeline_type = pipeline.get('type', 'pipeline')
+  save_as = False
+
+  if pipeline.get('parentSavedQueryUuid'): # We save into the original saved query, not into the query history
+    pipeline_doc = Document2.objects.get_by_uuid(user=user, uuid=pipeline['parentSavedQueryUuid'])
+  elif pipeline.get('id'):
+    pipeline_doc = Document2.objects.get(id=pipeline['id'])
+  else:
+    pipeline_doc = Document2.objects.create(name=pipeline['name'], uuid=pipeline['uuid'], type=pipeline_type, owner=user)
+    Document.objects.link(pipeline_doc, owner=pipeline_doc.owner, name=pipeline_doc.name, description=pipeline_doc.description, extra=pipeline_type)
+    save_as = True
+
+    if pipeline.get('directoryUuid'):
+      pipeline_doc.parent_directory = Document2.objects.get_by_uuid(user=user, uuid=pipeline.get('directoryUuid'), perm_type='write')
+    else:
+      pipeline_doc.parent_directory = Document2.objects.get_home_directory(user)
+
+  pipeline['isSaved'] = True
+  pipeline['isHistory'] = False
+  pipeline['id'] = pipeline_doc.id
+  pipeline_doc1 = pipeline_doc._get_doc1(doc2_type=pipeline_type)
+  pipeline_doc.update_data(pipeline)
+  #pipeline_doc.search = _get_statement(pipeline)
+  pipeline_doc.name = pipeline_doc1.name = pipeline['name']
+  pipeline_doc.description = pipeline_doc1.description = pipeline['description']
+  pipeline_doc.save()
+  pipeline_doc1.save()
+
+  return pipeline_doc, save_as
+
+
 FIELD_TYPES = (
   "alphaOnlySort",
   "ancestor_path",

+ 2 - 1
desktop/libs/indexer/src/indexer/urls.py

@@ -68,7 +68,8 @@ urlpatterns += [
   url(r'^api/indexer/guess_format/$', indexer_api3.guess_format, name='guess_format'),
   url(r'^api/indexer/guess_field_types/$', indexer_api3.guess_field_types, name='guess_field_types'),
 
-  url(r'^api/importer/submit', indexer_api3.importer_submit, name='importer_submit')
+  url(r'^api/importer/submit', indexer_api3.importer_submit, name='importer_submit'),
+  url(r'^api/importer/save/?$', indexer_api3.save_pipeline, name='save_pipeline'),
 ]
 
 urlpatterns += [