Example #1
0
    def make_content_node(self, path, kind):
        title = os.path.basename(path)
        try:
            node = ContentNode.objects.get(path=path)
        except ContentNode.DoesNotExist:
            node = ContentNode(path=path)
        node.title = title
        node.owner = self.owner
        node.kind = kind

        # Set the creation date to the file modification time.
        mtime = os.path.getmtime(path)
        node.created = datetime.datetime.fromtimestamp(mtime)

        # Get the file size or size of files contained within the directory.
        if os.path.isfile(path):
            size = os.path.getsize(path)
        elif os.path.isdir(path):
            # This could be inefficient if we end up repeatedly walking the same
            # directory subtree.
            size = 0
            for (root, dirs, files) in os.walk(path):
                size += sum(
                    os.path.getsize(os.path.join(root, f)) for f in files)
        node.size = size

        try:
            node.metadata
        except ContentMetadata.DoesNotExist:
            meta = ContentMetadata()
            meta.save()
            node.metadata = meta
        node.save()
        self.nodes.add(node)
        print 'created node: %r; path %r' % (node, node.path)
        return node