more on virtual attributes
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@2850 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
7e2efcedfc
commit
35dceda12f
1 changed files with 57 additions and 5 deletions
62
common.py
62
common.py
|
@ -36,8 +36,8 @@ from zope.traversing.api import getName
|
||||||
|
|
||||||
from cybertools.storage.interfaces import IStorageInfo
|
from cybertools.storage.interfaces import IStorageInfo
|
||||||
from cybertools.typology.interfaces import IType
|
from cybertools.typology.interfaces import IType
|
||||||
from loops.interfaces import ILoopsObject, ILoopsContained, IConcept, IResource
|
from loops.interfaces import ILoopsObject, ILoopsContained
|
||||||
from loops.interfaces import IResourceAdapter
|
from loops.interfaces import IConcept, IResource, IResourceAdapter
|
||||||
|
|
||||||
|
|
||||||
# convenience functions
|
# convenience functions
|
||||||
|
@ -215,9 +215,44 @@ class NameChooser(BaseNameChooser):
|
||||||
'\xdc': 'Ue', '\xfc': 'ue', '\xdf': 'ss'}
|
'\xdc': 'Ue', '\xfc': 'ue', '\xdf': 'ss'}
|
||||||
|
|
||||||
|
|
||||||
# relation set: use relations of a certain predicate as a collection attribute
|
# virtual attributes
|
||||||
|
|
||||||
class ParentRelationSet(object):
|
class ContainerAttribute(object):
|
||||||
|
""" Use objects within a ConceptManager object for a collection attribute.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, context, typeName, idAttr='name', prefix=''):
|
||||||
|
self.context = context
|
||||||
|
self.typeName = typeName
|
||||||
|
self.idAttr = idAttr
|
||||||
|
self.prefix = prefix
|
||||||
|
|
||||||
|
@Lazy
|
||||||
|
def typeConcept(self):
|
||||||
|
return self.context[self.typeName]
|
||||||
|
|
||||||
|
def create(self, id, **kw):
|
||||||
|
from loops.concept import Concept
|
||||||
|
from loops.setup import addAndConfigureObject
|
||||||
|
if self.idAttr != 'name' and self.idAttr not in kw:
|
||||||
|
kw[self.idAttr] = id
|
||||||
|
c = addAndConfigureObject(self.context, Concept, self.prefix + id,
|
||||||
|
conceptType=self.typeConcept, **kw)
|
||||||
|
return adapted(c)
|
||||||
|
|
||||||
|
def remove(self, id):
|
||||||
|
del self.context[self.prefix + id]
|
||||||
|
|
||||||
|
def get(self, id, default=None, langInfo=None):
|
||||||
|
return adapted(self.context.get(self.prefix + id, default), langInfo=langInfo)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for c in self.typeConcept.getChildren([self.context.getTypePredicate()]):
|
||||||
|
yield adapted(c)
|
||||||
|
|
||||||
|
|
||||||
|
class RelationSet(object):
|
||||||
|
""" Use relations of a certain predicate for a collection attribute."""
|
||||||
|
|
||||||
def __init__(self, context, predicateName):
|
def __init__(self, context, predicateName):
|
||||||
self.adapted = context
|
self.adapted = context
|
||||||
|
@ -239,17 +274,34 @@ class ParentRelationSet(object):
|
||||||
def predicate(self):
|
def predicate(self):
|
||||||
return self.conceptManager[self.predicateName]
|
return self.conceptManager[self.predicateName]
|
||||||
|
|
||||||
|
|
||||||
|
class ParentRelationSet(RelationSet):
|
||||||
|
|
||||||
|
def add(self, related):
|
||||||
|
if isinstance(related, AdapterBase):
|
||||||
|
related = related.context
|
||||||
|
self.context.assignParent(related, self.predicate)
|
||||||
|
|
||||||
|
def remove(self, related):
|
||||||
|
if isinstance(related, AdapterBase):
|
||||||
|
related = related.context
|
||||||
|
self.context.deassignParent(related, [self.predicate])
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for c in self.context.getParents([self.predicate]):
|
for c in self.context.getParents([self.predicate]):
|
||||||
yield adapted(c)
|
yield adapted(c)
|
||||||
|
|
||||||
|
|
||||||
class ChildRelationSet(ParentRelationSet):
|
class ChildRelationSet(RelationSet):
|
||||||
|
|
||||||
def add(self, related):
|
def add(self, related):
|
||||||
|
if isinstance(related, AdapterBase):
|
||||||
|
related = related.context
|
||||||
self.context.assignChild(related, self.predicate)
|
self.context.assignChild(related, self.predicate)
|
||||||
|
|
||||||
def remove(self, related):
|
def remove(self, related):
|
||||||
|
if isinstance(related, AdapterBase):
|
||||||
|
related = related.context
|
||||||
self.context.deassignChild(related, [self.predicate])
|
self.context.deassignChild(related, [self.predicate])
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue