'Returns the parent directory object that should be used for a given document. If the document is tagged with a
project name (non-RESERVED DocumentTag), a Directory object with the first project tag found is returned.
Otherwise, the owner\'s home directory is returned.'

def _get_parent_directory(self, document):

    parent_dir = self.home_dir
    project_tags = document.tags.exclude(tag__in=DocumentTag.RESERVED)
    if project_tags.exists():
        first_tag = project_tags[0]
        (parent_dir, created) = Directory.objects.get_or_create(owner=self.user, name=first_tag.tag, parent_directory=self.home_dir)
    return parent_dir
