Эх сурвалжийг харах

HUE-3338 [doc2] Support saving a notebook/editor to a specific parent directory

POST /notebook/api/notebook/save

Required:
notebook: Notebook JSON

Optional:
parent_uuid: UUID of parent directory to save doc under. If not supplied, will save to existing parent or home directory by default.
Jenny Kim 9 жил өмнө
parent
commit
ad4caf3

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

@@ -1008,6 +1008,7 @@ class Document2(models.Model):
       'uuid': self.uuid,
       'id': self.id,
       'doc1_id': self.doc.get().id if self.doc.exists() else -1,
+      'parent_uuid': self.parent_directory.uuid if self.parent_directory else None,
       'type': self.type,
       'perms': self._massage_permissions(),
       'last_modified': self.last_modified.strftime(UTC_TIME_FORMAT),

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

@@ -207,6 +207,10 @@ 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 = Document2.objects.get_home_directory(request.user)
+  if parent_uuid:
+    parent = Document2.objects.get_by_uuid(parent_uuid)
 
   if notebook.get('id'):
     notebook_doc = Document2.objects.get(id=notebook['id'])
@@ -218,6 +222,7 @@ 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
   notebook_doc.save()
   notebook_doc1.save()
 

+ 47 - 1
desktop/libs/notebook/src/notebook/tests.py

@@ -24,7 +24,7 @@ from django.core.urlresolvers import reverse
 
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.test_utils import grant_access
-from desktop.models import Document, Document2
+from desktop.models import Directory, Document, Document2
 
 
 class TestNotebookApi(object):
@@ -65,6 +65,52 @@ class TestNotebookApi(object):
                                       description=self.doc2.description, extra=self.doc2.type)
 
 
+  def test_save_notebook(self):
+    # Test that saving an existing document with a new parent will update 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})
+    data = json.loads(response.content)
+
+    assert_equal(0, data['status'], data)
+    doc = Document2.objects.get(pk=self.doc2.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
+    notebook_json = """
+      {
+        "selectedSnippet": "hive",
+        "showHistory": false,
+        "description": "Test Hive Query",
+        "name": "Test Hive Query",
+        "sessions": [
+            {
+                "type": "hive",
+                "properties": [],
+                "id": null
+            }
+        ],
+        "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)
+
+    assert_equal(0, data['status'], data)
+    id = data['id']
+    doc = Document2.objects.get(pk=id)
+    assert_equal(new_dir.uuid, doc.parent_directory.uuid)
+
+
   def test_historify(self):
     # Test that only users with access permissions can create a history doc
     response = self.client_not_me.post(reverse('notebook:historify'), {'notebook': self.notebook_json})