Преглед изворни кода

HUE-3410 [editor] Properly persist the parent of an history query

Romain Rigaux пре 9 година
родитељ
комит
db33c72ede

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

@@ -141,6 +141,7 @@ def get_document(request):
     'document': document.to_dict(),
     'parent': document.parent_directory.to_dict() if document.parent_directory else None,
     'children': [],
+    'dependencies': [dependency.uuid for dependency in document.dependencies.all()],
     'data': ''
   }
 

+ 15 - 6
desktop/libs/notebook/src/notebook/api.py

@@ -92,6 +92,10 @@ def execute(request):
       history = _historify(notebook, request.user)
       response['history_id'] = history.id
       response['history_uuid'] = history.uuid
+      if notebook['isSaved']: # Keep track of history of saved queries
+        response['history_parent_uuid'] = history.dependencies.filter(type__startswith='query-').latest('last_modified').uuid
+      print notebook['isSaved']
+      print notebook
 
   # Materialize and HTML escape results
   if response['handle'].get('sync') and response['handle']['result'].get('data'):
@@ -242,12 +246,17 @@ def save_notebook(request):
   if parent_uuid:
     parent_directory = Document2.objects.get_by_uuid(parent_uuid)
 
-  if notebook.get('id'):
+  if notebook.get('parentUuid'):
+    notebook_doc = Document2.objects.get(uuid=notebook['parentUuid']) # TODO security
+  elif notebook.get('id'):
     notebook_doc = Document2.objects.get(id=notebook['id'])
   else:
     notebook_doc = Document2.objects.create(name=notebook['name'], uuid=notebook['uuid'], type=notebook_type, owner=request.user)
     Document.objects.link(notebook_doc, owner=notebook_doc.owner, name=notebook_doc.name, description=notebook_doc.description, extra=notebook_type)
 
+  notebook['isSaved'] = True
+  notebook['isHistory'] = False
+  notebook['id'] = notebook_doc.id
   notebook_doc1 = notebook_doc.doc.get()
   notebook_doc.update_data(notebook)
   notebook_doc.name = notebook_doc1.name = notebook['name']
@@ -275,11 +284,11 @@ def _historify(notebook, user):
     is_history=True
   )
 
-  print notebook['parentUuid']
-  if notebook['parentUuid']:
-    parent_doc = Document2.objects.get(uuid=notebook['parentUuid'])
-    if parent_doc.can_write(user):
-      history_doc.dependencies.add(parent_doc)
+  # Link history of saved query
+  if notebook['isSaved']:
+    parent_doc = Document2.objects.get(uuid=notebook.get('parentUuid') or notebook['uuid']) # From previous history query or initial saved query
+    notebook['parentUuid'] = parent_doc.uuid
+    history_doc.dependencies.add(parent_doc)
 
   Document.objects.link(
     history_doc,

+ 18 - 4
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -600,6 +600,8 @@
           notebook.id(data.history_id);
           notebook.uuid(data.history_uuid);
           notebook.isHistory(true);
+//          notebook.isSaved(true);
+          notebook.parentUuid(data.history_parent_uuid);
 
           notebook.history.unshift(
             notebook._makeHistoryRecord(
@@ -998,12 +1000,13 @@
     var self = this;
 
     self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
-    self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
-    self.parentUuid = ko.observable(typeof notebook.parentUuid != "undefined" && notebook.parentUuid != null ? notebook.parentUuid : null);
+    self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());    
     self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
     self.description = ko.observable(typeof notebook.description != "undefined" && notebook.description != null ? notebook.description: '');
     self.type = ko.observable(typeof notebook.type != "undefined" && notebook.type != null ? notebook.type : 'notebook');
-    self.isHistory = ko.observable(typeof notebook.is_history != "undefined" && notebook.is_history != null ? notebook.is_history : false);
+    self.isHistory = ko.observable(typeof notebook.is_history != "undefined" && notebook.is_history != null ? notebook.is_history : false);	
+    self.parentUuid = ko.observable(typeof notebook.parentUuid != "undefined" && notebook.parentUuid != null ? notebook.parentUuid : null); // History parent
+    self.isSaved = ko.observable(typeof notebook.isSaved != "undefined" && notebook.isSaved != null ? notebook.isSaved : false);
     self.snippets = ko.observableArray();
     self.selectedSnippet = ko.observable(vm.availableSnippets().length > 0 ? vm.availableSnippets()[0].type() : 'NO_SNIPPETS');
     self.creatingSessionLocks = ko.observableArray();
@@ -1015,7 +1018,6 @@
 
     self.history = ko.observableArray([]);
     // TODO: Move fetchHistory and clearHistory into the Snippet and drop self.selectedSnippet
-
     self.getSession = function (session_type) {
       var _s = null;
       $.each(self.sessions(), function (index, s) {
@@ -1186,6 +1188,7 @@
          id: self.id,
          uuid: self.uuid,
          parentUuid: self.parentUuid,
+         isSaved: self.isSaved,
          sessions: self.sessions,
          type: self.type
       };
@@ -1199,6 +1202,8 @@
       }, function (data) {
         if (data.status == 0) {
           self.id(data.id);
+          self.isSaved(true);
+          self.isHistory(false);
           $(document).trigger("info", data.message);
           if (vm.editorMode){
             hueUtils.changeURL('/notebook/editor?editor=' + data.id);
@@ -1415,6 +1420,14 @@
     self.isFullscreenMode = ko.observable(false);
     self.successUrl = ko.observable(options.success_url);
     self.isOptimizerEnabled = ko.observable(options.is_optimizer_enabled);
+    self.canSave = ko.computed(function() {
+        // Saved query or history but history coming from a saved query
+        return self.selectedNotebook() && (
+        		  self.selectedNotebook().isSaved() ||
+        		  //(! self.selectedNotebook().isSaved() && self.selectedNotebook().isHistory() && self.selectedNotebook().parentUuid()) ||
+      			  (self.selectedNotebook().isHistory() && self.selectedNotebook().parentUuid())
+        );
+      });
 
     self.sqlSourceTypes = [];
 
@@ -1574,6 +1587,7 @@
     self.saveAsNotebook = function () {
       self.selectedNotebook().id(null);
       self.selectedNotebook().uuid(UUID());
+      self.selectedNotebook().parentUuid(null);
       self.saveNotebook();
     };
   }

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

@@ -183,9 +183,9 @@ ${ hueIcons.symbols() }
       <div class="pull-right">
 
         <div class="btn-group">
-          <a class="btn" title="${ _('Save') }" rel="tooltip" data-placement="bottom" data-loading-text="${ _("Saving...") }" data-bind="click: function() { if ($root.selectedNotebook() && $root.selectedNotebook().id() && ! $root.selectedNotebook().isHistory()) { saveNotebook() } else { $('#saveAsModal').modal('show');} }"><i class="fa fa-save"></i></a>
+          <a class="btn" rel="tooltip" data-placement="bottom" data-loading-text="${ _("Saving...") }" data-bind="click: function() { if ($root.canSave() ) { saveNotebook() } else { $('#saveAsModal').modal('show');} }, attr: { title: $root.canSave() ? '${ _ko('Save') }' : '${ _ko('Save As') }' }"><i class="fa fa-save"></i></a>
 
-          <!-- ko if: $root.selectedNotebook() && $root.selectedNotebook().id() && ! $root.selectedNotebook().isHistory() -->
+          <!-- ko if: $root.canSave -->
           <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
           <ul class="dropdown-menu">
             <li>