Browse Source

[doc2] Better renaming of include_history flag on search_documents

Jenny Kim 9 years ago
parent
commit
d2e2756

+ 3 - 3
desktop/core/src/desktop/api2.py

@@ -62,7 +62,7 @@ def search_documents(request):
   Returns the directories and documents based on given params that are accessible by the current user
   Returns the directories and documents based on given params that are accessible by the current user
   Optional params:
   Optional params:
     perms=<mode>       - Controls whether to retrieve owned, shared, or both. Defaults to both.
     perms=<mode>       - Controls whether to retrieve owned, shared, or both. Defaults to both.
-    get_history=<bool> - Controls whether to retrieve history docs. Defaults to false.
+    include_history=<bool> - Controls whether to retrieve history docs. Defaults to false.
     flatten=<bool>     - Controls whether to return documents in a flat list, or roll up documents to a common directory
     flatten=<bool>     - Controls whether to return documents in a flat list, or roll up documents to a common directory
                          if possible. Defaults to true.
                          if possible. Defaults to true.
     page=<n>           - Controls pagination. Defaults to 1.
     page=<n>           - Controls pagination. Defaults to 1.
@@ -80,13 +80,13 @@ def search_documents(request):
   }
   }
 
 
   perms = request.GET.get('perms', 'both').lower()
   perms = request.GET.get('perms', 'both').lower()
-  get_history = json.loads(request.GET.get('get_history', 'false'))
+  include_history = json.loads(request.GET.get('include_history', 'false'))
   flatten = json.loads(request.GET.get('flatten', 'true'))
   flatten = json.loads(request.GET.get('flatten', 'true'))
 
 
   if perms not in ['owned', 'shared', 'both']:
   if perms not in ['owned', 'shared', 'both']:
     raise Exception(_('Invalid value for perms, acceptable values are: owned, shared, both.'))
     raise Exception(_('Invalid value for perms, acceptable values are: owned, shared, both.'))
 
 
-  documents = Document2.objects.documents(user=request.user, perms=perms, get_history=get_history)
+  documents = Document2.objects.documents(user=request.user, perms=perms, include_history=include_history)
 
 
   # Refine results
   # Refine results
   response.update(_filter_documents(request, queryset=documents, flatten=flatten))
   response.update(_filter_documents(request, queryset=documents, flatten=flatten))

+ 4 - 6
desktop/core/src/desktop/models.py

@@ -759,11 +759,11 @@ class Document2Manager(models.Manager):
   def document(self, user, doc_id):
   def document(self, user, doc_id):
     return self.documents(user).get(id=doc_id)
     return self.documents(user).get(id=doc_id)
 
 
-  def documents(self, user, perms='both', get_history=False):
+  def documents(self, user, perms='both', include_history=False):
     """
     """
     Returns all documents that are owned or shared with the user.
     Returns all documents that are owned or shared with the user.
     :param perms: both, shared, owned. Defaults to both.
     :param perms: both, shared, owned. Defaults to both.
-    :param get_history: boolean flag to return history documents. Defaults to False.
+    :param include_history: boolean flag to return history documents. Defaults to False.
     """
     """
     if perms == 'both':
     if perms == 'both':
       docs = Document2.objects.filter(
       docs = Document2.objects.filter(
@@ -779,12 +779,10 @@ class Document2Manager(models.Manager):
     else:  # only return documents owned by the user
     else:  # only return documents owned by the user
       docs = Document2.objects.filter(owner=user)
       docs = Document2.objects.filter(owner=user)
 
 
-    if not get_history:
+    if not include_history:
       docs = docs.exclude(is_history=True)
       docs = docs.exclude(is_history=True)
 
 
-    docs = docs.distinct()
-
-    return docs.order_by('-last_modified')
+    return docs.distinct().order_by('-last_modified')
 
 
   def refine_documents(self, documents, types=None, search_text=None, order_by=None):
   def refine_documents(self, documents, types=None, search_text=None, order_by=None):
     """
     """

+ 9 - 0
desktop/core/src/desktop/tests_doc2.py

@@ -633,6 +633,7 @@ class TestDocument2Permissions(object):
   def test_search_documents(self):
   def test_search_documents(self):
     owned_dir = Directory.objects.create(name='test_dir', owner=self.user, parent_directory=self.home_dir)
     owned_dir = Directory.objects.create(name='test_dir', owner=self.user, parent_directory=self.home_dir)
     owned_query = Document2.objects.create(name='query1.sql', type='query-hive', owner=self.user, data={}, parent_directory=owned_dir)
     owned_query = Document2.objects.create(name='query1.sql', type='query-hive', owner=self.user, data={}, parent_directory=owned_dir)
+    owned_history = Document2.objects.create(name='history.sql', type='query-hive', owner=self.user, data={}, is_history=True, parent_directory=owned_dir)
     owned_workflow = Document2.objects.create(name='test.wf', type='oozie-workflow2', owner=self.user, data={}, parent_directory=owned_dir)
     owned_workflow = Document2.objects.create(name='test.wf', type='oozie-workflow2', owner=self.user, data={}, parent_directory=owned_dir)
 
 
     other_home_dir = Document2.objects.get_home_directory(user=self.user_not_me)
     other_home_dir = Document2.objects.get_home_directory(user=self.user_not_me)
@@ -652,3 +653,11 @@ class TestDocument2Permissions(object):
     assert_true('query1.sql' in doc_names)
     assert_true('query1.sql' in doc_names)
     assert_true('other_query2.sql' in doc_names)
     assert_true('other_query2.sql' in doc_names)
     assert_true('other_query3.sql' in doc_names)
     assert_true('other_query3.sql' in doc_names)
+
+    # Return history docs
+    response = self.client.get('/desktop/api2/docs/', {'type': 'query-hive', 'include_history': 'true'})
+    data = json.loads(response.content)
+    assert_true('documents' in data)
+    assert_equal(4, data['count'])
+    doc_names = [doc['name'] for doc in data['documents']]
+    assert_true('history.sql' in doc_names)