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): def __getitem__(self, key):
value = self.container.queryLast(parent=self.uid, name=key) value = self.container.queryLast(parent=self.uid, name=key)
if value is None: if value is None:
raise KeyError raise KeyError(key)
return value return value
def __setitem__(self, key, value): def __setitem__(self, key, value):
value.head['parent'] = self.uid value.set('parent', self.uid)
value.head['name']= key value.set('name', key)
self.container.save(value) 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 @registerContainerClass
class Folders(Container): class Folders(Container):

View file

@ -36,6 +36,15 @@ class Track(object):
self.trackId = trackId self.trackId = trackId
self.container = container 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): def update(self, data, overwrite=False):
if data is None: if data is None:
return return

View file

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