Back to index

plone3  3.1.7
types.py
Go to the documentation of this file.
00001 from zope.app.schema.vocabulary import IVocabularyFactory
00002 from zope.interface import implements
00003 from zope.schema.vocabulary import SimpleVocabulary
00004 from zope.schema.vocabulary import SimpleTerm
00005 from Products.Archetypes.mimetype_utils import getAllowableContentTypes
00006 from Products.Archetypes.mimetype_utils import getAllowedContentTypes
00007 
00008 from Products.CMFCore.utils import getToolByName
00009 
00010 
00011 class AllowableContentTypesVocabulary(object):
00012     """Vocabulary factory for allowable content types.
00013 
00014       >>> from zope.component import queryUtility
00015       >>> from plone.app.vocabularies.tests.base import DummyContext
00016       >>> from plone.app.vocabularies.tests.base import DummyTool
00017 
00018       >>> name = 'plone.app.vocabularies.AllowableContentTypes'
00019       >>> util = queryUtility(IVocabularyFactory, name)
00020       >>> context1 = DummyContext()
00021       >>> context2 = DummyContext()
00022       >>> context1.context = context2
00023 
00024       >>> tool = DummyTool('portal_transforms')
00025       >>> def listAvailableTextInputs():
00026       ...     return ('text/plain', 'text/spam')
00027       >>> tool.listAvailableTextInputs = listAvailableTextInputs
00028       >>> context2.portal_transforms = tool
00029 
00030       >>> types = util(context1)
00031       >>> types
00032       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00033 
00034       >>> len(types.by_token)
00035       2
00036 
00037       >>> doc = types.by_token['text/plain']
00038       >>> doc.title, doc.token, doc.value
00039       ('text/plain', 'text/plain', 'text/plain')
00040     """
00041     implements(IVocabularyFactory)
00042 
00043     def __call__(self, context):
00044         context = getattr(context, 'context', context)
00045         items = list(getAllowableContentTypes(context))
00046         items.sort()
00047         items = [SimpleTerm(i, i, i) for i in items]
00048         return SimpleVocabulary(items)
00049 
00050 AllowableContentTypesVocabularyFactory = AllowableContentTypesVocabulary()
00051 
00052 
00053 class AllowedContentTypesVocabulary(object):
00054     """Vocabulary factory for allowed content types.
00055 
00056       >>> from zope.component import queryUtility
00057       >>> from plone.app.vocabularies.tests.base import DummyContext
00058       >>> from plone.app.vocabularies.tests.base import DummyTool
00059 
00060       >>> name = 'plone.app.vocabularies.AllowedContentTypes'
00061       >>> util = queryUtility(IVocabularyFactory, name)
00062       >>> context1 = DummyContext()
00063       >>> context2 = DummyContext()
00064       >>> context1.context = context2
00065 
00066       >>> tool = DummyTool('portal_transforms')
00067       >>> def listAvailableTextInputs():
00068       ...     return ('text/plain', 'text/spam')
00069       >>> tool.listAvailableTextInputs = listAvailableTextInputs
00070       >>> context2.portal_transforms = tool
00071 
00072       >>> tool = DummyTool('portal_properties')
00073       >>> class DummyProperties(object):
00074       ...     def hasProperty(self, value):
00075       ...         return True
00076       ...
00077       ...     def getProperty(self, value):
00078       ...         return ('text/spam', )
00079       >>> tool.site_properties = DummyProperties()
00080       >>> context2.portal_properties = tool
00081 
00082       >>> types = util(context1)
00083       >>> types
00084       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00085 
00086       >>> len(types.by_token)
00087       1
00088 
00089       >>> doc = types.by_token['text/plain']
00090       >>> doc.title, doc.token, doc.value
00091       ('text/plain', 'text/plain', 'text/plain')
00092     """
00093     implements(IVocabularyFactory)
00094 
00095     def __call__(self, context):
00096         context = getattr(context, 'context', context)
00097         items = list(getAllowedContentTypes(context))
00098         items.sort()
00099         items = [SimpleTerm(i, i, i) for i in items]
00100         return SimpleVocabulary(items)
00101 
00102 AllowedContentTypesVocabularyFactory = AllowedContentTypesVocabulary()
00103 
00104 
00105 class PortalTypesVocabulary(object):
00106     """Vocabulary factory for portal types.
00107 
00108       >>> from zope.component import queryUtility
00109       >>> from plone.app.vocabularies.tests.base import DummyContext
00110       >>> from plone.app.vocabularies.tests.base import DummyTypeTool
00111 
00112       >>> name = 'plone.app.vocabularies.PortalTypes'
00113       >>> util = queryUtility(IVocabularyFactory, name)
00114       >>> context1 = DummyContext()
00115       >>> context2 = DummyContext()
00116       >>> context1.context = context2
00117 
00118       >>> util(context1) is None
00119       True
00120 
00121       >>> context2.portal_types = DummyTypeTool()
00122 
00123       >>> types = util(context1)
00124       >>> types
00125       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00126 
00127       >>> len(types.by_token)
00128       2
00129 
00130       >>> doc = types.by_token['Document']
00131       >>> doc.title, doc.token, doc.value
00132       ('Page', 'Document', 'Document')
00133     """
00134     implements(IVocabularyFactory)
00135 
00136     def __call__(self, context):
00137         context = getattr(context, 'context', context)
00138         ttool = getToolByName(context, 'portal_types', None)
00139         if ttool is None:
00140             return None
00141         items = [ (ttool[t].Title(), t)
00142                   for t in ttool.listContentTypes() ]
00143         items.sort()
00144         items = [SimpleTerm(i[1], i[1], i[0]) for i in items]
00145         return SimpleVocabulary(items)
00146 
00147 PortalTypesVocabularyFactory = PortalTypesVocabulary()
00148 
00149 
00150 class UserFriendlyTypesVocabulary(object):
00151     """Vocabulary factory for user friendly portal types.
00152 
00153       >>> from zope.component import queryUtility
00154       >>> from plone.app.vocabularies.tests.base import DummyContext
00155       >>> from plone.app.vocabularies.tests.base import DummyTool
00156       >>> from plone.app.vocabularies.tests.base import DummyTypeTool
00157 
00158       >>> name = 'plone.app.vocabularies.UserFriendlyTypes'
00159       >>> util = queryUtility(IVocabularyFactory, name)
00160       >>> context1 = DummyContext()
00161       >>> context2 = DummyContext()
00162       >>> context1.context = context2
00163 
00164       >>> util(context1) is None
00165       True
00166 
00167       >>> context2.portal_types = DummyTypeTool()
00168       >>> tool = DummyTool('plone_utils')
00169       >>> def getUserFriendlyTypes():
00170       ...     return ('Document', )
00171       >>> tool.getUserFriendlyTypes = getUserFriendlyTypes
00172       >>> context2.plone_utils = tool
00173 
00174       >>> types = util(context1)
00175       >>> types
00176       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00177 
00178       >>> len(types.by_token)
00179       1
00180 
00181       >>> doc = types.by_token['Document']
00182       >>> doc.title, doc.token, doc.value
00183       ('Page', 'Document', 'Document')
00184     """
00185     implements(IVocabularyFactory)
00186 
00187     def __call__(self, context):
00188         context = getattr(context, 'context', context)
00189         ptool = getToolByName(context, 'plone_utils', None)
00190         ttool = getToolByName(context, 'portal_types', None)
00191         if ptool is None or ttool is None:
00192             return None
00193         items = [ (ttool[t].Title(), t)
00194                   for t in ptool.getUserFriendlyTypes() ]
00195         items.sort()
00196         items = [SimpleTerm(i[1], i[1], i[0]) for i in items]
00197         return SimpleVocabulary(items)
00198 
00199 UserFriendlyTypesVocabularyFactory = UserFriendlyTypesVocabulary()
00200 
00201 
00202 BAD_TYPES = ("ATBooleanCriterion", "ATDateCriteria", "ATDateRangeCriterion",
00203              "ATListCriterion", "ATPortalTypeCriterion", "ATReferenceCriterion",
00204              "ATSelectionCriterion", "ATSimpleIntCriterion", "Plone Site",
00205              "ATSimpleStringCriterion", "ATSortCriterion", "ChangeSet",
00206              "Discussion Item", "TempFolder", "ATCurrentAuthorCriterion",
00207              "ATPathCriterion", "ATRelativePathCriterion", )
00208 
00209 
00210 class ReallyUserFriendlyTypesVocabulary(object):
00211     """Vocabulary factory for really user friendly portal types.
00212 
00213       >>> from zope.component import queryUtility
00214       >>> from plone.app.vocabularies.tests.base import DummyContext
00215       >>> from plone.app.vocabularies.tests.base import DummyType
00216       >>> from plone.app.vocabularies.tests.base import DummyTypeTool
00217 
00218       >>> name = 'plone.app.vocabularies.ReallyUserFriendlyTypes'
00219       >>> util = queryUtility(IVocabularyFactory, name)
00220       >>> context1 = DummyContext()
00221       >>> context2 = DummyContext()
00222       >>> context1.context = context2
00223 
00224       >>> util(context1) is None
00225       True
00226 
00227       >>> tool = DummyTypeTool()
00228       >>> tool['ATBooleanCriterion'] = DummyType('Boolean Criterion')
00229       >>> context2.portal_types = tool
00230 
00231       >>> types = util(context1)
00232       >>> types
00233       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00234 
00235       >>> len(types.by_token)
00236       2
00237 
00238       >>> doc = types.by_token['Document']
00239       >>> doc.title, doc.token, doc.value
00240       ('Page', 'Document', 'Document')
00241     """
00242     implements(IVocabularyFactory)
00243 
00244     def __call__(self, context):
00245         context = getattr(context, 'context', context)
00246         ttool = getToolByName(context, 'portal_types', None)
00247         if ttool is None:
00248             return None
00249         items = [ SimpleTerm(t, t, ttool[t].Title())
00250                   for t in ttool.listContentTypes()
00251                   if t not in BAD_TYPES ]
00252         return SimpleVocabulary(items)
00253 
00254 ReallyUserFriendlyTypesVocabularyFactory = ReallyUserFriendlyTypesVocabulary()