fix type methods
This commit is contained in:
parent
3e1127e798
commit
1e165a711c
4 changed files with 32 additions and 11 deletions
|
@ -33,12 +33,16 @@ class Storage(object):
|
||||||
def add(self, container):
|
def add(self, container):
|
||||||
self.containers[container.itemFactory.prefix] = container
|
self.containers[container.itemFactory.prefix] = container
|
||||||
|
|
||||||
|
def getContainer(self, prefix):
|
||||||
|
container = self.containers.get(prefix)
|
||||||
|
if container is None:
|
||||||
|
return self.create(registry[prefix])
|
||||||
|
return container
|
||||||
|
|
||||||
def getItem(self, uid):
|
def getItem(self, uid):
|
||||||
prefix, id = uid.split('-')
|
prefix, id = uid.split('-')
|
||||||
id = int(id)
|
id = int(id)
|
||||||
container = self.containers.get(prefix)
|
container = self.getContainer(prefix)
|
||||||
if container is None:
|
|
||||||
container = self.create(registry[prefix])
|
|
||||||
return container.get(id)
|
return container.get(id)
|
||||||
|
|
||||||
def getExistingTable(self, tableName):
|
def getExistingTable(self, tableName):
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
from zope.interface import implementer
|
from zope.interface import implementer
|
||||||
from scopes.interfaces import IConcept
|
from scopes.interfaces import IConcept
|
||||||
from scopes.storage.common import registerContainerClass
|
from scopes.storage.common import registerContainerClass, registry
|
||||||
from scopes.storage.tracking import Container, Track
|
from scopes.storage.tracking import Container, Track
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,16 +51,21 @@ class Rels(Container):
|
||||||
|
|
||||||
class Type(Concept):
|
class Type(Concept):
|
||||||
|
|
||||||
headFields = ['name', 'prefix']
|
headFields = ['name', 'tprefix']
|
||||||
prefix = 'type'
|
prefix = 'type'
|
||||||
|
|
||||||
def get(key, default=None):
|
def get(self, key, default=None):
|
||||||
return self.container.queryLast(name=key) or default
|
cont = self.container.storage.getContainer(self.tprefix)
|
||||||
|
return cont.queryLast(name=key) or default
|
||||||
|
|
||||||
|
def values(self):
|
||||||
|
cont = self.container.storage.getContainer(self.tprefix)
|
||||||
|
return cont.query()
|
||||||
|
|
||||||
|
|
||||||
@registerContainerClass
|
@registerContainerClass
|
||||||
class Types(Concepts):
|
class Types(Concepts):
|
||||||
|
|
||||||
itemFactory = Type
|
itemFactory = Type
|
||||||
indexes = [('name',), ('prefix',)]
|
indexes = [('name',), ('tprefix',)]
|
||||||
tableName = 'types'
|
tableName = 'types'
|
||||||
|
|
|
@ -98,8 +98,11 @@ class Container(object):
|
||||||
return tr
|
return tr
|
||||||
|
|
||||||
def query(self, **crit):
|
def query(self, **crit):
|
||||||
stmt = self.table.select().where(
|
if crit:
|
||||||
|
stmt = self.table.select().where(
|
||||||
and_(*self.setupWhere(crit))).order_by(self.table.c.trackid)
|
and_(*self.setupWhere(crit))).order_by(self.table.c.trackid)
|
||||||
|
else:
|
||||||
|
stmt = self.table.select().order_by(self.table.c.trackid)
|
||||||
for r in self.session.execute(stmt):
|
for r in self.session.execute(stmt):
|
||||||
yield self.makeTrack(r)
|
yield self.makeTrack(r)
|
||||||
|
|
||||||
|
@ -109,6 +112,7 @@ class Container(object):
|
||||||
return self.makeTrack(self.session.execute(stmt).first())
|
return self.makeTrack(self.session.execute(stmt).first())
|
||||||
|
|
||||||
def save(self, track):
|
def save(self, track):
|
||||||
|
track.container = self
|
||||||
crit = dict((hf, track.head[hf]) for hf in track.headFields)
|
crit = dict((hf, track.head[hf]) for hf in track.headFields)
|
||||||
found = self.queryLast(**crit)
|
found = self.queryLast(**crit)
|
||||||
if found is None:
|
if found is None:
|
||||||
|
|
|
@ -78,7 +78,15 @@ def test_type(self, config):
|
||||||
storage = config.storageFactory(config.dbschema)
|
storage = config.storageFactory(config.dbschema)
|
||||||
storage.dropTable('types')
|
storage.dropTable('types')
|
||||||
types = storage.create(concept.Types)
|
types = storage.create(concept.Types)
|
||||||
tid01 = types.save(concept.Type('type', 'type'))
|
ttype = concept.Type('type', concept.Type.prefix)
|
||||||
self.assertEqual(tid01, 1)
|
ttid = types.save(ttype)
|
||||||
|
self.assertEqual(ttid, 1)
|
||||||
|
tps = list(ttype.values())
|
||||||
|
self.assertEqual(len(tps), 1)
|
||||||
|
|
||||||
|
tfolder = concept.Type('folder', folder.Folder.prefix)
|
||||||
|
tfid = types.save(tfolder)
|
||||||
|
fldrs = list(tfolder.values())
|
||||||
|
self.assertEqual(len(fldrs), 2)
|
||||||
|
|
||||||
storage.commit()
|
storage.commit()
|
||||||
|
|
Loading…
Add table
Reference in a new issue