Browse Source

[doc2] Add a get_by_uuid manager method

Jenny Kim 9 years ago
parent
commit
9b18910

+ 1 - 1
apps/oozie/src/oozie/models2.py

@@ -2342,7 +2342,7 @@ class Coordinator(Job):
 
 
   @property
   @property
   def workflow(self):
   def workflow(self):
-    wf_doc = Document2.objects.get(uuid=self.data['properties']['workflow'])
+    wf_doc = Document2.objects.get_by_uuid(uuid=self.data['properties']['workflow'])
     return Workflow(document=wf_doc)
     return Workflow(document=wf_doc)
 
 
   def get_absolute_url(self):
   def get_absolute_url(self):

+ 2 - 2
apps/oozie/src/oozie/tests.py

@@ -3139,7 +3139,7 @@ class TestOozieSubmissions(OozieBase):
 
 
   def test_submit_hiveserver2_action(self):
   def test_submit_hiveserver2_action(self):
     wf_uuid = "c1c3cba9-edec-fb6f-a526-9f80b66fe993"
     wf_uuid = "c1c3cba9-edec-fb6f-a526-9f80b66fe993"
-    wf = Document2.objects.get(uuid=wf_uuid)
+    wf = Document2.objects.get_by_uuid(uuid=wf_uuid)
     wf.data.replace('hive2://localhost:10000/default', _get_hiveserver2_url())
     wf.data.replace('hive2://localhost:10000/default', _get_hiveserver2_url())
     wf.save()
     wf.save()
 
 
@@ -3163,7 +3163,7 @@ class TestOozieSubmissions(OozieBase):
 
 
   def test_submit_spark_action(self):
   def test_submit_spark_action(self):
     wf_uuid = "2d667ab2-70f9-c2bf-0726-abe84fa7130d"
     wf_uuid = "2d667ab2-70f9-c2bf-0726-abe84fa7130d"
-    wf = Document2.objects.get(uuid=wf_uuid)
+    wf = Document2.objects.get_by_uuid(uuid=wf_uuid)
 
 
     # Somewhere we delete those by mistake
     # Somewhere we delete those by mistake
     doc = Document.objects.link(wf, owner=wf.owner, name=wf.name, description=wf.description, extra='workflow2')
     doc = Document.objects.link(wf, owner=wf.owner, name=wf.name, description=wf.description, extra='workflow2')

+ 1 - 1
apps/oozie/src/oozie/views/editor2.py

@@ -599,7 +599,7 @@ def submit_coordinator(request, doc_id):
 
 
 def _submit_coordinator(request, coordinator, mapping):
 def _submit_coordinator(request, coordinator, mapping):
   try:
   try:
-    wf_doc = Document2.objects.get(uuid=coordinator.data['properties']['workflow'])
+    wf_doc = Document2.objects.get_by_uuid(uuid=coordinator.data['properties']['workflow'])
     wf_dir = Submission(request.user, Workflow(document=wf_doc), request.fs, request.jt, mapping, local_tz=coordinator.data['properties']['timezone']).deploy()
     wf_dir = Submission(request.user, Workflow(document=wf_doc), request.fs, request.jt, mapping, local_tz=coordinator.data['properties']['timezone']).deploy()
 
 
     properties = {'wf_application_path': request.fs.get_hdfs_path(wf_dir)}
     properties = {'wf_application_path': request.fs.get_hdfs_path(wf_dir)}

+ 1 - 1
apps/search/src/search/search_controller.py

@@ -84,7 +84,7 @@ class SearchController(object):
     try:
     try:
       for doc2 in self.get_shared_search_collections():
       for doc2 in self.get_shared_search_collections():
         if doc2.id in collection_ids:
         if doc2.id in collection_ids:
-          doc2 = Document2.objects.get(uuid=doc2.uuid)
+          doc2 = Document2.objects.get_by_uuid(uuid=doc2.uuid)
           doc = doc2.doc.get()
           doc = doc2.doc.get()
 
 
           name = doc2.name + '-copy'
           name = doc2.name + '-copy'

+ 49 - 48
desktop/core/src/desktop/api2.py

@@ -75,7 +75,7 @@ def get_documents(request):
   uuid = request.GET.get('uuid')
   uuid = request.GET.get('uuid')
 
 
   if uuid:
   if uuid:
-    document = Document2.objects.get(uuid=uuid)
+    document = Document2.objects.get_by_uuid(uuid)
   else:  # Find by path
   else:  # Find by path
     document = Document2.objects.get_by_path(user=request.user, path=path)
     document = Document2.objects.get_by_path(user=request.user, path=path)
 
 
@@ -115,53 +115,12 @@ def get_documents(request):
   })
   })
 
 
 
 
-def _convert_documents(user):
-  """
-  Given a user, converts any existing Document objects to Document2 objects
-  """
-  from beeswax.models import HQL, IMPALA, RDBMS
-
-  with transaction.atomic():
-    # If user does not have a home directory, we need to create one and import any orphan documents to it
-    Document2.objects.create_user_directories(user)
-
-    docs = Document.objects.get_docs(user, SavedQuery).filter(owner=user).filter(extra__in=[HQL, IMPALA, RDBMS])
-
-    imported_tag = DocumentTag.objects.get_imported2_tag(user=user)
-
-    docs = docs.exclude(tags__in=[
-        DocumentTag.objects.get_trash_tag(user=user),  # No trashed docs
-        DocumentTag.objects.get_history_tag(user=user),  # No history yet
-        DocumentTag.objects.get_example_tag(user=user),  # No examples
-        imported_tag  # No already imported docs
-    ])
-
-    root_doc, created = Directory.objects.get_or_create(name='', owner=user)
-    imported_docs = []
-
-    for doc in docs:
-      if doc.content_object:
-        try:
-          notebook = import_saved_beeswax_query(doc.content_object)
-          data = notebook.get_data()
-          notebook_doc = Document2.objects.create(name=data['name'], type=data['type'], owner=user, data=notebook.get_json())
-
-          doc.add_tag(imported_tag)
-          doc.save()
-          imported_docs.append(notebook_doc)
-        except Exception, e:
-          raise e
-
-    if imported_docs:
-      root_doc.children.add(*imported_docs)
-
-
 @api_error_handler
 @api_error_handler
 def get_document(request):
 def get_document(request):
   if request.GET.get('id'):
   if request.GET.get('id'):
     doc = Document2.objects.get(id=request.GET['id'])
     doc = Document2.objects.get(id=request.GET['id'])
   else:
   else:
-    doc = Document2.objects.get(uuid=request.GET['uuid'])
+    doc = Document2.objects.get_by_uuid(uuid=request.GET['uuid'])
 
 
   doc_info = doc.to_dict()
   doc_info = doc.to_dict()
   return JsonResponse(doc_info)
   return JsonResponse(doc_info)
@@ -176,8 +135,8 @@ def move_document(request):
   if not source_doc_uuid or not destination_doc_uuid:
   if not source_doc_uuid or not destination_doc_uuid:
     raise PopupException(_('move_document requires source_doc_uuid and destination_doc_uuid'))
     raise PopupException(_('move_document requires source_doc_uuid and destination_doc_uuid'))
 
 
-  source = Directory.objects.get(uuid=source_doc_uuid)
-  destination = Directory.objects.get(uuid=destination_doc_uuid)
+  source = Directory.objects.get_by_uuid(uuid=source_doc_uuid)
+  destination = Directory.objects.get_by_uuid(uuid=destination_doc_uuid)
   source.move(destination, request.user)
   source.move(destination, request.user)
 
 
   return JsonResponse({'status': 0})
   return JsonResponse({'status': 0})
@@ -192,7 +151,7 @@ def create_directory(request):
   if not parent_uuid or not name:
   if not parent_uuid or not name:
     raise PopupException(_('create_directory requires parent_uuid and name'))
     raise PopupException(_('create_directory requires parent_uuid and name'))
 
 
-  parent_dir = Directory.objects.get(uuid=parent_uuid)
+  parent_dir = Directory.objects.get_by_uuid(uuid=parent_uuid)
   # TODO: Check permissions and move to manager
   # TODO: Check permissions and move to manager
   directory = Directory.objects.create(name=name, owner=request.user, parent_directory=parent_dir)
   directory = Directory.objects.create(name=name, owner=request.user, parent_directory=parent_dir)
 
 
@@ -220,7 +179,7 @@ def delete_document(request):
   if not uuid:
   if not uuid:
     raise PopupException(_('delete_document requires uuid'))
     raise PopupException(_('delete_document requires uuid'))
 
 
-  document = Document2.objects.get(uuid=uuid)
+  document = Document2.objects.get_by_uuid(uuid=uuid)
 
 
   if skip_trash:
   if skip_trash:
     # TODO: check if document is in the .Trash folder, if not raise exception
     # TODO: check if document is in the .Trash folder, if not raise exception
@@ -249,7 +208,7 @@ def share_document(request):
   if not uuid:
   if not uuid:
     raise PopupException(_('share_document requires uuid'))
     raise PopupException(_('share_document requires uuid'))
 
 
-  doc = Document2.objects.get(uuid=uuid)
+  doc = Document2.objects.get_by_uuid(uuid=uuid)
 
 
   for name, perm in perms_dict.iteritems():
   for name, perm in perms_dict.iteritems():
     users = groups = None
     users = groups = None
@@ -328,6 +287,7 @@ def import_documents(request):
 
 
     doc['fields']['tags'] = []
     doc['fields']['tags'] = []
 
 
+    # TODO: Check if this should be replaced by get_by_uuid
     if Document2.objects.filter(uuid=doc['fields']['uuid'], owner__username=owner).exists():
     if Document2.objects.filter(uuid=doc['fields']['uuid'], owner__username=owner).exists():
       doc['pk'] = Document2.objects.get(uuid=doc['fields']['uuid'], owner__username=owner).pk
       doc['pk'] = Document2.objects.get(uuid=doc['fields']['uuid'], owner__username=owner).pk
     else:
     else:
@@ -351,3 +311,44 @@ def import_documents(request):
     return redirect(request.POST.get('redirect'))
     return redirect(request.POST.get('redirect'))
   else:
   else:
     return JsonResponse({'message': stdout.getvalue()})
     return JsonResponse({'message': stdout.getvalue()})
+
+
+def _convert_documents(user):
+  """
+  Given a user, converts any existing Document objects to Document2 objects
+  """
+  from beeswax.models import HQL, IMPALA, RDBMS
+
+  with transaction.atomic():
+    # If user does not have a home directory, we need to create one and import any orphan documents to it
+    Document2.objects.create_user_directories(user)
+
+    docs = Document.objects.get_docs(user, SavedQuery).filter(owner=user).filter(extra__in=[HQL, IMPALA, RDBMS])
+
+    imported_tag = DocumentTag.objects.get_imported2_tag(user=user)
+
+    docs = docs.exclude(tags__in=[
+        DocumentTag.objects.get_trash_tag(user=user),  # No trashed docs
+        DocumentTag.objects.get_history_tag(user=user),  # No history yet
+        DocumentTag.objects.get_example_tag(user=user),  # No examples
+        imported_tag  # No already imported docs
+    ])
+
+    root_doc, created = Directory.objects.get_or_create(name='', owner=user)
+    imported_docs = []
+
+    for doc in docs:
+      if doc.content_object:
+        try:
+          notebook = import_saved_beeswax_query(doc.content_object)
+          data = notebook.get_data()
+          notebook_doc = Document2.objects.create(name=data['name'], type=data['type'], owner=user, data=notebook.get_json())
+
+          doc.add_tag(imported_tag)
+          doc.save()
+          imported_docs.append(notebook_doc)
+        except Exception, e:
+          raise e
+
+    if imported_docs:
+      root_doc.children.add(*imported_docs)

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

@@ -746,6 +746,9 @@ class DocumentPermission(models.Model):
     unique_together = ('doc', 'perms')
     unique_together = ('doc', 'perms')
 
 
 
 
+###################################################################################################
+# Document2
+###################################################################################################
 class FilesystemException(Exception):
 class FilesystemException(Exception):
   pass
   pass
 
 
@@ -771,6 +774,16 @@ class Document2Manager(models.Manager):
   def get_by_natural_key(self, uuid, version, is_history):
   def get_by_natural_key(self, uuid, version, is_history):
     return self.get(uuid=uuid, version=version, is_history=is_history)
     return self.get(uuid=uuid, version=version, is_history=is_history)
 
 
+  def get_by_uuid(self, uuid):
+    """
+    Since UUID is not a unique field, but part of a composite unique key, this returns the latest version by UUID
+    This should always be used in place of Document2.objects.get(uuid=) when a single document is expected
+    """
+    docs = self.filter(uuid=uuid).order_by('-last_modified')
+    if not docs.exists():
+      raise FilesystemException(_('Document with UUID %s not found.') % uuid)
+    return docs[0]
+
   def get_history(self, user, doc_type):
   def get_history(self, user, doc_type):
     return self.documents(user).filter(type=doc_type, is_history=True)
     return self.documents(user).filter(type=doc_type, is_history=True)
 
 
@@ -1005,6 +1018,9 @@ class Document2(models.Model):
     super(Document2, self).save(*args, **kwargs)
     super(Document2, self).save(*args, **kwargs)
 
 
   def move(self, directory, user):
   def move(self, directory, user):
+    if not directory.is_directory:
+      raise FilesystemException(_('Target with UUID %s is not a directory') % directory.uuid)
+
     if directory.can_write_or_exception(user=user):
     if directory.can_write_or_exception(user=user):
       self.parent_directory = directory
       self.parent_directory = directory
       self.save()
       self.save()
@@ -1061,7 +1077,8 @@ class Document2(models.Model):
       }
       }
     }
     }
 
 
-class DirectoryManager(models.Manager):
+
+class DirectoryManager(Document2Manager):
 
 
   def get_queryset(self):
   def get_queryset(self):
     return super(DirectoryManager, self).get_queryset().filter(type='directory')
     return super(DirectoryManager, self).get_queryset().filter(type='directory')

+ 1 - 1
desktop/libs/liboozie/src/liboozie/submission2.py

@@ -178,7 +178,7 @@ class Submission(object):
         # Don't support more than one level sub-workflow
         # Don't support more than one level sub-workflow
         if action.data['type'] == 'subworkflow':
         if action.data['type'] == 'subworkflow':
           from oozie.models2 import Workflow
           from oozie.models2 import Workflow
-          workflow = Workflow(document=Document2.objects.get(uuid=action.data['properties']['workflow']))
+          workflow = Workflow(document=Document2.objects.get_by_uuid(uuid=action.data['properties']['workflow']))
           sub_deploy = Submission(self.user, workflow, self.fs, self.jt, self.properties)
           sub_deploy = Submission(self.user, workflow, self.fs, self.jt, self.properties)
           workspace = sub_deploy.deploy()
           workspace = sub_deploy.deploy()
 
 

+ 2 - 2
desktop/libs/notebook/src/notebook/views.py

@@ -122,7 +122,7 @@ def delete(request):
   notebooks = json.loads(request.POST.get('notebooks', '[]'))
   notebooks = json.loads(request.POST.get('notebooks', '[]'))
 
 
   for notebook in notebooks:
   for notebook in notebooks:
-    doc2 = Document2.objects.get(uuid=notebook['uuid'])
+    doc2 = Document2.objects.get_by_uuid(uuid=notebook['uuid'])
     doc = doc2.doc.get()
     doc = doc2.doc.get()
     doc.can_write_or_exception(request.user)
     doc.can_write_or_exception(request.user)
 
 
@@ -137,7 +137,7 @@ def copy(request):
   notebooks = json.loads(request.POST.get('notebooks', '[]'))
   notebooks = json.loads(request.POST.get('notebooks', '[]'))
 
 
   for notebook in notebooks:
   for notebook in notebooks:
-    doc2 = Document2.objects.get(uuid=notebook['uuid'])
+    doc2 = Document2.objects.get_by_uuid(uuid=notebook['uuid'])
     doc = doc2.doc.get()
     doc = doc2.doc.get()
 
 
     name = doc2.name + '-copy'
     name = doc2.name + '-copy'