Преглед на файлове

HUE-3422 [home] Save documents directly into a directory

Romain Rigaux преди 9 години
родител
ревизия
b12a780ed1

+ 2 - 4
desktop/libs/notebook/src/notebook/api.py

@@ -239,7 +239,6 @@ def save_notebook(request):
 
   notebook = json.loads(request.POST.get('notebook', '{}'))
   notebook_type = notebook.get('type', 'notebook')
-  directory_uuid = request.POST.get('directory_uuid', None)
 
   if notebook.get('parentUuid'):
     notebook_doc = Document2.objects.get(uuid=notebook['parentUuid'])
@@ -250,8 +249,8 @@ def save_notebook(request):
     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)
 
-    if directory_uuid:
-      notebook_doc.parent_directory = Document2.objects.get_by_uuid(user=request.user, uuid=directory_uuid, perm_type='write')
+    if notebook.get('directoryUuid'):
+      notebook_doc.parent_directory = Document2.objects.get_by_uuid(user=request.user, uuid=notebook.get('directoryUuid'), perm_type='write')
     else:
       notebook_doc.parent_directory = Document2.objects.get_home_directory(request.user)
 
@@ -272,7 +271,6 @@ def save_notebook(request):
   return JsonResponse(response)
 
 
-
 def _historify(notebook, user):
   query_type = notebook['type']
   name = notebook['name'] if (notebook['name'] and notebook['name'].strip() != '') else DEFAULT_HISTORY_NAME

+ 1 - 0
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1029,6 +1029,7 @@
         return new Session(vm, value.data);
       }
     });
+    self.directoryUuid = ko.observable(typeof notebook.directoryUuid != "undefined" && notebook.directoryUuid != null ? notebook.directoryUuid : null);
 
     self.loadingHistory = ko.observable(true);
     self.history = ko.observableArray(typeof notebook.fetchedHistory != "undefined" && notebook.fetchedHistory != null ? notebook.fetchedHistory : []);

+ 3 - 0
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -299,6 +299,9 @@ ${ hueIcons.symbols() }
           <li data-bind="visible: isHistory" style="display: none">
             <a title="${ _('Query history query') }"><i class="fa fa-fw fa-history"></i></a>
           </li>
+          <li data-bind="visible: directoryUuid" style="display: none">
+            <a title="${ _('Open home directory') }" data-bind="attr: { 'href': '/home?uuid=' + directoryUuid() }"><i class="fa fa-fw fa-folder-o"></i></a>
+          </li>
           <li class="query-name">
             <a href="javascript:void(0)">
               <div class="notebook-name-desc" data-bind="editable: name, editableOptions: { enabled: true, placement: 'bottom', emptytext: '${_ko('Add a name...')}', tpl: '<input type=\'text\' maxlength=\'255\'>' }"></div>

+ 4 - 4
desktop/libs/notebook/src/notebook/tests.py

@@ -77,16 +77,17 @@ class TestNotebookApi(object):
     new_dir = Directory.objects.create(name='new_dir', owner=self.user, parent_directory=home_dir)
     notebook_cp = self.notebook.copy()
     notebook_cp.pop('id')
+    notebook_cp['directoryUuid'] = new_dir.uuid
     notebook_json = json.dumps(notebook_cp)
 
-    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json, 'directory_uuid': new_dir.uuid})
+    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json})
     data = json.loads(response.content)
 
     assert_equal(0, data['status'], data)
     doc = Document2.objects.get(pk=data['id'])
     assert_equal(new_dir.uuid, doc.parent_directory.uuid)
 
-    # Test that saving a new document with no parent will map it to its home dir
+    # Test that saving a new document with a no parent will map it to its home dir
     notebook_json = """
       {
         "selectedSnippet": "hive",
@@ -111,8 +112,7 @@ class TestNotebookApi(object):
     data = json.loads(response.content)
 
     assert_equal(0, data['status'], data)
-    id = data['id']
-    doc = Document2.objects.get(pk=id)
+    doc = Document2.objects.get(pk=data['id'])
     assert_equal(Document2.objects.get_home_directory(self.user).uuid, doc.parent_directory.uuid)
 
 

+ 6 - 0
desktop/libs/notebook/src/notebook/views.py

@@ -56,11 +56,15 @@ def notebooks(request):
 @check_document_access_permission()
 def notebook(request):
   notebook_id = request.GET.get('notebook')
+  directory_uuid = request.GET.get('directory_uuid')
 
   if notebook_id:
     notebook = Notebook(document=Document2.objects.get(id=notebook_id))
   else:
     notebook = Notebook()
+    data = notebook.get_data()
+    data['directoryUuid'] = directory_uuid
+    notebook.data = json.dumps(data)    
 
   is_yarn_mode = False
   try:
@@ -84,6 +88,7 @@ def notebook(request):
 def editor(request):
   editor_id = request.GET.get('editor')
   editor_type = request.GET.get('type', 'hive')
+  directory_uuid = request.GET.get('directory_uuid')
 
   if editor_id:  # Open existing saved editor document
     editor = Notebook(document=Document2.objects.get(id=editor_id))
@@ -95,6 +100,7 @@ def editor(request):
 
     data['name'] = ''
     data['type'] = 'query-%s' % editor_type  # TODO: Add handling for non-SQL types
+    data['directoryUuid'] = directory_uuid
     editor.data = json.dumps(data)
 
   return render('editor.mako', request, {