Back to index

plone3  3.1.7
skins.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 SimpleTerm
00004 from zope.schema.vocabulary import SimpleVocabulary
00005 
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 class SkinsVocabulary(object):
00009     """Vocabulary factory for skins.
00010 
00011       >>> from zope.component import queryUtility
00012       >>> from plone.app.vocabularies.tests.base import DummyContext
00013       >>> from plone.app.vocabularies.tests.base import DummyTool
00014 
00015       >>> name = 'plone.app.vocabularies.Skins'
00016       >>> util = queryUtility(IVocabularyFactory, name)
00017       >>> context1 = DummyContext()
00018       >>> context2 = DummyContext()
00019       >>> context1.context = context2
00020 
00021       >>> util(context1) is None
00022       True
00023 
00024       >>> tool = DummyTool('portal_skins')
00025       >>> def getSkinSelections():
00026       ...     return ('Plone Default', 'Plone Kitty')
00027       >>> tool.getSkinSelections = getSkinSelections
00028       >>> context2.portal_skins = tool
00029 
00030       >>> skins = util(context1)
00031       >>> skins
00032       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00033 
00034       >>> len(skins.by_token)
00035       2
00036 
00037       >>> de = skins.by_token['Plone Kitty']
00038       >>> de.title, de.token, de.value
00039       ('Plone Kitty', 'Plone Kitty', 'Plone Kitty')
00040     """
00041     implements(IVocabularyFactory)
00042 
00043     def __call__(self, context):
00044         context = getattr(context, 'context', context)
00045         stool = getToolByName(context, 'portal_skins', None)
00046         if stool is None:
00047             return None
00048         items = list(stool.getSkinSelections())
00049         items.sort()
00050         items = [SimpleTerm(i, i, i) for i in items]
00051         return SimpleVocabulary(items)
00052 
00053 SkinsVocabularyFactory = SkinsVocabulary()