provide a virtual / dummy folder root object for top-down folder creation and access

This commit is contained in:
Helmut Merz 2024-02-27 19:07:29 +01:00
parent 77f5abc7bf
commit 2382abf129
3 changed files with 33 additions and 15 deletions

View file

@ -22,15 +22,26 @@ class Folder(Track):
def __getitem__(self, key):
value = self.container.queryLast(parent=self.uid, name=key)
if value is None:
raise KeyError
raise KeyError(key)
return value
def __setitem__(self, key, value):
value.head['parent'] = self.uid
value.head['name']= key
value.set('parent', self.uid)
value.set('name', key)
self.container.save(value)
class Root(Folder):
"""A dummy (virtual) root folder for creating real folders
using the Folder API."""
def __init__(self, storage):
cont = storage.create(Folders)
super(Root, self).__init__(container=cont)
uid = ''
@registerContainerClass
class Folders(Container):

View file

@ -36,6 +36,15 @@ class Track(object):
self.trackId = trackId
self.container = container
def set(self, attr, value):
if attr in self.headFields:
if value is None:
value = ''
self.head[attr] = value
setattr(self, attr, value)
else:
raise AttributeError(attr)
def update(self, data, overwrite=False):
if data is None:
return

View file

@ -72,18 +72,16 @@ class Test(unittest.TestCase):
def testFolder(self):
storage.dropTable('folders')
cont = storage.create(folder.Folders)
self.assertEqual(list(cont.query(parent='')), [])
cont.save(folder.Folder('', 'root'))
folders = list(cont.query(parent=''))
self.assertEqual(len(folders), 1)
root = folders[0]
root['child1'] = folder.Folder(data=dict(title='First Child'))
folders = list(cont.query(parent=root.uid))
self.assertEqual(len(folders), 1)
ch1 = root['child1']
self.assertEqual(ch1.parent, root.uid)
assert list(root.keys()) == ['child1']
root = folder.Root(storage)
self.assertEqual(list(root.keys()), [])
root['top'] = folder.Folder()
self.assertEqual(list(root.keys()), ['top'])
top = root['top']
top['child1'] = folder.Folder(data=dict(title='First Child'))
self.assertEqual(list(top.keys()), ['child1'])
ch1 = top['child1']
self.assertEqual(ch1.parent, top.uid)
assert list(top.keys()) == ['child1']
def suite():
return unittest.TestSuite((