'Returns all documents that are owned or shared with the user.
:param perms: both, shared, owned. Defaults to both.
:param include_history: boolean flag to return history documents. Defaults to False.
:param include_trashed: boolean flag to return trashed documents. Defaults to True.'

def documents(self, user, perms='both', include_history=False, include_trashed=False, include_managed=False):

    if (perms == 'both'):
        docs = self.filter(((Q(owner=user) | Q(document2permission__users=user)) | Q(document2permission__groups__in=user.groups.all())))
    elif (perms == 'shared'):
        docs = self.filter((Q(document2permission__users=user) | Q(document2permission__groups__in=user.groups.all())))
    else:
        docs = self.filter(owner=user)
    if (not include_history):
        docs = docs.exclude(is_history=True)
    if (not include_managed):
        docs = docs.exclude(is_managed=True)
    if (not include_trashed):
        docs = docs.exclude(is_trashed=True)
    return docs.defer('description', 'data', 'extra', 'search').distinct().order_by('-last_modified')
