Переглянути джерело

HUE-3207 [doc2] User's home directory response returns shared docs in the children collection

Jenny Kim 9 роки тому
батько
коміт
caba0a9

+ 8 - 2
desktop/core/src/desktop/api2.py

@@ -135,9 +135,15 @@ def get_document(request):
   # Get children documents if this is a directory
   if document.is_directory:
     directory = Directory.objects.get(id=document.id)
-    children = directory.get_children_documents()
+
+    # If this is the user's home directory, fetch shared docs too
+    if document.is_home_directory:
+      children = directory.get_children_and_shared_documents(user=request.user)
+    else:
+      children = directory.get_children_documents()
+
     # Filter and order results
-    response.update(_filter_documents(request, queryset=children))
+    response.update(_filter_documents(request, queryset=children, flatten=False))
 
   # Paginate and serialize Results
   if 'documents' in response:

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

@@ -1174,6 +1174,22 @@ class Directory(Document2):
     documents = self.children.filter(is_history=False)  # TODO: perms
     return documents
 
+  def get_children_and_shared_documents(self, user):
+    """
+    Returns the children and shared documents for a given directory, excluding history documents
+    """
+    # Get documents that are direct children, or shared with but not owned by the current user
+    documents = Document2.objects.filter(
+        Q(parent_directory=self) |
+        ( (Q(document2permission__users=user) | Q(document2permission__groups__in=user.groups.all())) &
+          ~Q(owner=user) )
+      )
+
+    documents = documents.exclude(is_history=True)
+
+    return documents.defer('description', 'data', 'extra').distinct().order_by('-last_modified')
+
+
   def save(self, *args, **kwargs):
     self.type = 'directory'
     super(Directory, self).save(*args, **kwargs)

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

@@ -606,12 +606,12 @@ class TestDocument2Permissions(object):
     assert_true('query3.sql' in doc_names)
     assert_false('query1.sql' in doc_names)
 
-    # they should not appear in the other user's regular get_documents response
+    # they should also appear in user's home directory get_documents response
     response = self.client_not_me.get('/desktop/api2/doc/')
     data = json.loads(response.content)
     doc_names = [doc['name'] for doc in data['children']]
-    assert_false('query2.sql' in doc_names)
-    assert_false('query3.sql' in doc_names)
+    assert_true('query2.sql' in doc_names)
+    assert_true('query3.sql' in doc_names)
 
 
   def test_get_shared_directories(self):