Changeset 5968 for main/waeup.sirp/trunk/src/waeup/sirp
- Timestamp:
- 23 Apr 2011, 01:46:57 (14 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/browser/theming.py
r5963 r5968 26 26 from zope.component import getUtilitiesFor 27 27 from zope.interface import Interface 28 from waeup.sirp.interfaces import IThemesProvider 28 from zope.schema.interfaces import IVocabularyFactory 29 from waeup.sirp.interfaces import SimpleWAeUPVocabulary 29 30 from waeup.sirp.browser.interfaces import ITheme 30 31 from waeup.sirp.browser.resources import waeuptheme_red1, waeuptheme_gray1 … … 58 59 yield name, theme 59 60 60 class ThemesProvider(grok.GlobalUtility): 61 """A global utility providing all themes available. 61 class ThemesVocabulary(grok.GlobalUtility): 62 """A vocabulary that provides all themes available. 63 64 A named global utility implementing 65 66 :class:`zope.schema.interfaces.IVocabularyFactory 67 68 and registered under the name 69 70 'waeup.sirp.browser.theming.ThemesVocabulary' 71 72 Interface fields that wish to provide a list of available themes 73 can require a 'named vocabulary', i.e. set: 74 75 vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary' 76 77 and the vocabulary will deliver themes and their descriptive text 78 for instance in select boxes of forms. 79 80 The name is chosen so that the location of this vocabulary can be 81 found easily when used in an interface definition. 62 82 """ 63 grok.implements(IThemesProvider) 64 grok.name('default') 83 grok.implements(IVocabularyFactory) 84 grok.name('waeup.sirp.browser.theming.ThemesVocabulary') 85 86 def __call__(self, context): 87 """Deliver a vocabulary of available themes. 65 88 66 def __call__(self): 67 return get_all_themes() 89 The `description`s of available themes are used as 'title' and 90 the utility names delivering the different themes as 'value' 91 and 'token'. 92 """ 93 terms = [(theme.description, name) 94 for name, theme in get_all_themes()] 95 vocab = SimpleWAeUPVocabulary(*terms) 96 return vocab -
main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py
r5967 r5968 14 14 from waeup.sirp.permissions import RoleSource 15 15 16 class IThemesProvider(Interface):17 """A component that delivers all themes defined.18 19 We normally do not deal with browser-related stuff in `waeup.sirp`20 but leave that to the `browser` subpackage. With themes (aka21 skins) we cannot follow that path because we need a theme as an22 attribute of each `University` instance.23 24 Therefore we must tell the browser subpackage how we expect themes25 to be announced code-wise.26 27 What we tell here is that we might ask for a utility providing28 IThemeProvider and that this component then should be callable to29 give us a list of themes and their respective internal names.30 """31 def __call__():32 """Get a list of all themes available.33 34 Returns a list of ``(<NAME>, <THEME>)`` tuples where35 ``<NAME>`` is the internal name under which a theme was36 registered and ``<THEME>`` is the real theme.37 38 Each <THEME> must have a `description` attribute.39 """40 41 class ThemesSource(BasicSourceFactory):42 """A source for themes.43 """44 def getValues(self):45 """Get names of themes.46 """47 themes_provider = getUtility(IThemesProvider, name="default")48 return [x for x, y in themes_provider()]49 50 def getTitle(self, value):51 """Get a user-presentable description for a theme.52 """53 themes_provider = getUtility(IThemesProvider, name="default")54 for name, theme in themes_provider():55 if name == value:56 return theme.description57 return u'unnamed'58 59 class ThemesSourceBinder(object):60 """A source binder for the `ThemeSource`.61 62 Normally, we can pass instances of a source (like `ThemeSource`)63 directly to fields in interfaces (writing something like ``source64 = ThemeSource()``. With `ThemeSource` we cannot do this, because65 the set of available themes is not available before the whole66 `waeup.sirp` package was grokked (and parsed).67 68 We bypass that problem by defining this trivial source-binder,69 which is asked for themes only when the values of the source are70 really needed and after the process of grokking and parsing71 everything on startup.72 73 So, although this binder binds to some context (`IUniversity` is74 currently the only interface making use of this source, so75 `University` instances are the only context used), the context76 does not really matter.77 """78 implements(schema.interfaces.IContextSourceBinder)79 80 def __call__(self, context):81 return ThemesSource()82 16 83 17 class FatalCSVError(Exception): … … 92 26 return SimpleVocabulary([ 93 27 SimpleTerm(value, value, title) for title, value in terms]) 94 95 28 96 29 class IWAeUPObject(Interface): … … 113 46 title = u'Skin', 114 47 default = u'waeuptheme-gray1.css', 115 source = ThemesSourceBinder(),48 vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary', 116 49 required = True, 117 50 )
Note: See TracChangeset for help on using the changeset viewer.