Make typeInterface field available for type concepts

git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1131 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-03-20 08:39:02 +00:00
parent 7622600c99
commit 273a05a5d1
7 changed files with 96 additions and 20 deletions

View file

@ -27,7 +27,9 @@ from zope.app.dublincore.interfaces import ICMFDublinCore
from zope.app.form.browser.interfaces import ITerms from zope.app.form.browser.interfaces import ITerms
from zope.app.intid.interfaces import IIntIds from zope.app.intid.interfaces import IIntIds
from zope.cachedescriptors.property import Lazy from zope.cachedescriptors.property import Lazy
from zope.dottedname.resolve import resolve
from zope.interface import implements from zope.interface import implements
from zope.schema.vocabulary import SimpleTerm
from zope.security.proxy import removeSecurityProxy from zope.security.proxy import removeSecurityProxy
from cybertools.typology.interfaces import IType from cybertools.typology.interfaces import IType
@ -116,3 +118,21 @@ class LoopsTerms(object):
return self.loopsRoot.loopsTraverse(token) return self.loopsRoot.loopsTraverse(token)
class InterfaceTerms(object):
""" Provide the ITerms interface for source list of interfaces.
"""
implements(ITerms)
def __init__(self, source, request):
self.source = source
self.request = request
def getTerm(self, value):
token = '.'.join((value.__module__, value.__name__))
return SimpleTerm(value, token, token)
def getValue(self, token):
return resolve(token)

View file

@ -39,7 +39,7 @@ from zope.publisher.interfaces.browser import IBrowserRequest
from zope import schema from zope import schema
from zope.schema.interfaces import IIterableSource from zope.schema.interfaces import IIterableSource
from zope.security.proxy import removeSecurityProxy from zope.security.proxy import removeSecurityProxy
from cybertools.typology.interfaces import ITypeManager from cybertools.typology.interfaces import IType, ITypeManager
from loops.interfaces import IConcept from loops.interfaces import IConcept
from loops.concept import Concept, ConceptTypeSourceList, PredicateSourceList from loops.concept import Concept, ConceptTypeSourceList, PredicateSourceList
from loops.browser.common import BaseView, LoopsTerms from loops.browser.common import BaseView, LoopsTerms
@ -48,9 +48,25 @@ from loops import util
class ConceptEditForm(EditForm): class ConceptEditForm(EditForm):
form_fields = FormFields(IConcept)
template = NamedTemplate('pageform') template = NamedTemplate('pageform')
@Lazy
def typeInterface(self):
print IType(self.context)
print IType(self.context).typeInterface
return IType(self.context).typeInterface
@property
def form_fields(self):
fields = FormFields(IConcept)
typeInterface = self.typeInterface
if typeInterface is not None:
fields = FormFields(fields, typeInterface)
#typeAdapter = zapi.queryAdapter(self.context, typeInterface)
#if typeAdapter is not None:
#...
return fields
class ConceptView(BaseView): class ConceptView(BaseView):

View file

@ -478,6 +478,10 @@
for="loops.concept.PredicateSourceList for="loops.concept.PredicateSourceList
zope.publisher.interfaces.browser.IBrowserRequest" /> zope.publisher.interfaces.browser.IBrowserRequest" />
<zope:adapter factory="loops.browser.common.InterfaceTerms"
for="loops.type.TypeInterfaceSourceList
zope.publisher.interfaces.browser.IBrowserRequest" />
<zope:view factory="loops.view.NodeTraverser" <zope:view factory="loops.view.NodeTraverser"
for="loops.interfaces.INode" for="loops.interfaces.INode"
type="zope.publisher.interfaces.browser.IBrowserRequest" type="zope.publisher.interfaces.browser.IBrowserRequest"

View file

@ -258,6 +258,11 @@
name="loops.conceptTypeSource" name="loops.conceptTypeSource"
/> />
<vocabulary
factory="loops.type.TypeInterfaceSourceList"
name="loops.TypeInterfaceSource"
/>
<vocabulary <vocabulary
factory="loops.concept.PredicateSourceList" factory="loops.concept.PredicateSourceList"
name="loops.PredicateSource" name="loops.PredicateSource"

View file

@ -190,28 +190,30 @@ condition:
>>> sorted(t.token for t in types) >>> sorted(t.token for t in types)
['loops.resource.Document', 'loops.resource.MediaAsset'] ['loops.resource.Document', 'loops.resource.MediaAsset']
Type-based interfaces Type-based interfaces and adapters
--------------------- ----------------------------------
>>> from loops.interfaces import ITypeConcept
>>> from loops.type import TypeConcept
>>> ztapi.provideAdapter(IConcept, ITypeConcept, TypeConcept)
A type has an optional typeInterface attribute that objects of this type A type has an optional typeInterface attribute that objects of this type
will be adaptable to. The default for this is None: will be adaptable to. The default for this is None:
>>> cc1_type.typeInterface
>>> cc1_type.typeInterface is None >>> cc1_type.typeInterface is None
True True
For concept objects that provide types (type providers) the value of For concept objects that provide types (type providers) the value of
the typeInterface attribute is the ITypeConcept interface: the typeInterface attribute is the ITypeConcept interface:
>>> from loops.interfaces import ITypeConcept
>>> topic_type.typeInterface is ITypeConcept >>> topic_type.typeInterface is ITypeConcept
True True
We now want to have a topic (i.e. a concept that has topic_type as its type) We now want to have a topic (i.e. a concept that has topic as its
to provide the interface ITopic. This is done by assigning this interface conceptType and thus topic_type as its type) to be adaptable to ITopic.
to topic_type.typeProvider, i.e. the 'topic' concept, via an adapter: This is done by assigning this interface to topic_type.typeProvider,
i.e. the 'topic' concept, via an adapter:
>>> from loops.type import TypeConcept
>>> ztapi.provideAdapter(IConcept, ITypeConcept, TypeConcept)
>>> class ITopic(Interface): pass >>> class ITopic(Interface): pass
>>> from zope.interface import implements >>> from zope.interface import implements

View file

@ -473,9 +473,14 @@ class IIndexAttributes(Interface):
# types stuff # types stuff
class ITypeConcept(Interface): class ITypeConcept(Interface):
""" This interface should be provided by concepts of type 'type'. """ Concepts of type 'type' should be adaptable to this interface.
""" """
typeInterface = Attribute('The interface provided by objects of this type') typeInterface = schema.Choice(
title=_(u'Type Interface'),
description=_(u'An interface that objects of this type can '
'be adapted to'),
default=None,
source="loops.TypeInterfaceSource",
required=False)

36
type.py
View file

@ -27,6 +27,8 @@ from zope.component import adapts
from zope.interface import implements from zope.interface import implements
from zope.cachedescriptors.property import Lazy from zope.cachedescriptors.property import Lazy
from zope.dottedname.resolve import resolve from zope.dottedname.resolve import resolve
from zope import schema
from zope.security.proxy import removeSecurityProxy
from cybertools.typology.type import BaseType, TypeManager from cybertools.typology.type import BaseType, TypeManager
from loops.interfaces import ILoopsObject, IConcept, IResource from loops.interfaces import ILoopsObject, IConcept, IResource
from loops.interfaces import ITypeConcept from loops.interfaces import ITypeConcept
@ -58,7 +60,7 @@ class LoopsType(BaseType):
class ConceptType(LoopsType): class ConceptType(LoopsType):
""" The type adapter for concept objects. """ The IType adapter for concept objects.
""" """
adapts(IConcept) adapts(IConcept)
@ -77,11 +79,11 @@ class ConceptType(LoopsType):
@Lazy @Lazy
def typeInterface(self): def typeInterface(self):
adapter = zapi.queryAdapter(self.typeProvider, ITypeConcept) adapter = zapi.queryAdapter(self.typeProvider, ITypeConcept)
if adapter is not None: if adapter is not None: # always gives TypeConcept
return adapter.typeInterface return adapter.typeInterface
else: else:
conceptType = self.context.conceptType conceptType = self.typeProvider
if conceptType is conceptType.getConceptManager().getTypeConcept(): if conceptType is conceptType.getLoopsRoot().getConceptManager().getTypeConcept():
return ITypeConcept return ITypeConcept
return None return None
@ -177,17 +179,39 @@ class LoopsTypeManager(TypeManager):
class TypeConcept(object): class TypeConcept(object):
""" typeInterface adapter for concepts of type 'type'.
"""
implements(ITypeConcept) implements(ITypeConcept)
adapts(IConcept) adapts(IConcept)
def __init__(self, context): def __init__(self, context):
self.context = context self.context = removeSecurityProxy(context)
def getTypeInterface(self): def getTypeInterface(self):
return getattr(self.context, '_typeInterface', None) ti = getattr(self.context, '_typeInterface', None)
if ti is None:
conceptType = self.context
if conceptType == conceptType.getLoopsRoot().getConceptManager().getTypeConcept():
return ITypeConcept
return ti
def setTypeInterface(self, ifc): def setTypeInterface(self, ifc):
self.context._typeInterface = ifc self.context._typeInterface = ifc
typeInterface = property(getTypeInterface, setTypeInterface) typeInterface = property(getTypeInterface, setTypeInterface)
class TypeInterfaceSourceList(object):
implements(schema.interfaces.IIterableSource)
typeInterfaces = (ITypeConcept,)
def __init__(self, context):
self.context = context
def __iter__(self):
return iter(self.typeInterfaces)
def __len__(self):
return len(self.typeInterfaces)