'Returns the children and shared documents for a given directory, excluding history documents'

def get_children_and_shared_documents(self, 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).exclude(is_managed=True)
    documents = documents.exclude(is_trashed=True)
    directories_all = Document2.objects.filter(type='directory').exclude(id=self.id)
    directories_inside_home = directories_all.filter(((Q(document2permission__users=user) | Q(document2permission__groups__in=user.groups.all())) & (~ Q(owner=user))))
    documents = documents.exclude(parent_directory__in=directories_inside_home)
    return documents.defer('description', 'data', 'extra', 'search').distinct().order_by('-last_modified')
