浏览代码

HUE-3625 [editor] Saving a notebook resets its directory to Home

Conflicts:
	desktop/libs/notebook/src/notebook/api.py
Romain Rigaux 9 年之前
父节点
当前提交
6ef25fd
共有 2 个文件被更改,包括 16 次插入14 次删除
  1. 6 5
      desktop/libs/notebook/src/notebook/api.py
  2. 10 9
      desktop/libs/notebook/src/notebook/tests.py

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

@@ -239,10 +239,7 @@ def save_notebook(request):
 
   notebook = json.loads(request.POST.get('notebook', '{}'))
   notebook_type = notebook.get('type', 'notebook')
-  parent_uuid = notebook.get('parent_uuid', None)
-  parent_directory = Document2.objects.get_home_directory(request.user)
-  if parent_uuid:
-    parent_directory = Document2.objects.get_by_uuid(user=request.user, uuid=parent_uuid, perm_type='write')
+  directory_uuid = request.POST.get('directory_uuid', None)
 
   if notebook.get('parentUuid'):
     notebook_doc = Document2.objects.get(uuid=notebook['parentUuid'])
@@ -253,6 +250,11 @@ 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')
+    else:
+      notebook_doc.parent_directory = Document2.objects.get_home_directory(request.user)
+
   notebook['isSaved'] = True
   notebook['isHistory'] = False
   notebook['id'] = notebook_doc.id
@@ -260,7 +262,6 @@ def save_notebook(request):
   notebook_doc.update_data(notebook)
   notebook_doc.name = notebook_doc1.name = notebook['name']
   notebook_doc.description = notebook_doc1.description = notebook['description']
-  notebook_doc.parent_directory = parent_directory
   notebook_doc.save()
   notebook_doc1.save()
 

+ 10 - 9
desktop/libs/notebook/src/notebook/tests.py

@@ -70,21 +70,23 @@ class TestNotebookApi(object):
 
 
   def test_save_notebook(self):
-    # Test that saving an existing document with a new parent will update the parent_directory
+    # Test that saving a new document with a new parent will set the parent_directory
     home_dir = Document2.objects.get_home_directory(self.user)
     assert_equal(home_dir.uuid, self.doc2.parent_directory.uuid)
 
     new_dir = Directory.objects.create(name='new_dir', owner=self.user, parent_directory=home_dir)
-    self.notebook['parent_uuid'] = new_dir.uuid
-    notebook_json = json.dumps(self.notebook)
-    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json, 'parent_uuid': new_dir.uuid})
+    notebook_cp = self.notebook.copy()
+    notebook_cp.pop('id')
+    notebook_json = json.dumps(notebook_cp)
+
+    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json, 'directory_uuid': new_dir.uuid})
     data = json.loads(response.content)
 
     assert_equal(0, data['status'], data)
-    doc = Document2.objects.get(pk=self.doc2.id)
+    doc = Document2.objects.get(pk=data['id'])
     assert_equal(new_dir.uuid, doc.parent_directory.uuid)
 
-    # Test that saving a new document with a specific parent will map it to that parent directory
+    # Test that saving a new document with no parent will map it to its home dir
     notebook_json = """
       {
         "selectedSnippet": "hive",
@@ -100,11 +102,10 @@ class TestNotebookApi(object):
         ],
         "type": "query-hive",
         "id": null,
-        "parent_uuid": "%(uuid)s",
         "snippets": [],
         "uuid": "d9efdee1-ef25-4d43-b8f9-1a170f69a05a"
     }
-    """ % {'uuid': new_dir.uuid}
+    """
 
     response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json})
     data = json.loads(response.content)
@@ -112,7 +113,7 @@ class TestNotebookApi(object):
     assert_equal(0, data['status'], data)
     id = data['id']
     doc = Document2.objects.get(pk=id)
-    assert_equal(new_dir.uuid, doc.parent_directory.uuid)
+    assert_equal(Document2.objects.get_home_directory(self.user).uuid, doc.parent_directory.uuid)
 
 
   def test_historify(self):