diff --git a/configure.zcml b/configure.zcml index b50a3a4..c3a41f7 100644 --- a/configure.zcml +++ b/configure.zcml @@ -237,6 +237,7 @@ + diff --git a/helpers.txt b/helpers.txt index ba76cf9..656b174 100755 --- a/helpers.txt +++ b/helpers.txt @@ -127,6 +127,24 @@ lazy properties, one should always get a new adapter: >>> cc1_type.typeProvider == topic True +Now let's have a look at resources. + + >>> from loops.interfaces import IResource + >>> from loops.type import ResourceType + >>> ztapi.provideAdapter(IResource, IType, ResourceType) + + >>> doc1_type = IType(doc1) + >>> doc1_type.title + u'Document' + >>> doc1_type.token + '.loops/resources/document' + >>> doc1_type.tokenForSearch + 'loops:resource:document' + + >>> img1_type = IType(img1) + >>> img1_type.title + u'Media Asset' + Can we find out somehow which types are available? This is the time to look for a type manager. This could be a utility; but in the loops package it is again an adapter, now for the loops root object. Nevertheless one can @@ -142,7 +160,8 @@ get a type manager from all loops objects, always with the same context: >>> types = typeManager.types >>> sorted(t.token for t in types) - ['.loops/concepts/topic', '.loops/concepts/type'] + ['.loops/concepts/topic', '.loops/concepts/type', + '.loops/resources/document', '.loops/resources/mediaasset'] >>> typeManager.getType('.loops/concepts/topic') == cc1_type True diff --git a/type.py b/type.py index 736a87e..7d4b7d0 100644 --- a/type.py +++ b/type.py @@ -26,7 +26,8 @@ from zope.app import zapi from zope.component import adapts from zope.cachedescriptors.property import Lazy from cybertools.typology.type import BaseType, TypeManager -from loops.interfaces import ILoopsObject, IConcept +from loops.interfaces import ILoopsObject, IConcept, IResource +from loops.resource import Document, MediaAsset class LoopsType(BaseType): @@ -71,6 +72,43 @@ class ConceptType(LoopsType): return self.context.conceptType +class ResourceType(LoopsType): + + adapts(IResource) + + typeTitles = {'MediaAsset': u'Media Asset'} + + @Lazy + def title(self): + cn = self.className + return self.typeTitles.get(cn, unicode(cn)) + + @Lazy + def token(self): + cn = self.className + return '/'.join(('.loops/resources', cn.lower(),)) + + @Lazy + def tokenForSearch(self): + cn = self.className + return ':'.join(('loops:resource', cn.lower(),)) + + @Lazy + def className(self): + return self.context.__class__.__name__ + + + +class ResourceTypeInfo(ResourceType): + + def __init__(self, cls): + self.cls = cls + + @Lazy + def className(self): + return self.cls.__name__ + + class LoopsTypeManager(TypeManager): adapts(ILoopsObject) @@ -88,6 +126,8 @@ class LoopsTypeManager(TypeManager): result = to.getChildren([tp]) if to not in result: result.append(to) - return tuple(LoopsTypeInfo(c) for c in result) + cTypes = [LoopsTypeInfo(c) for c in result] + rTypes = [ResourceTypeInfo(cls) for cls in (Document, MediaAsset)] + return tuple(cTypes + rTypes)