Changeset 5968


Ignore:
Timestamp:
23 Apr 2011, 01:46:57 (13 years ago)
Author:
uli
Message:

Replace themes source by themes vocabulary. It looks like this is much easier to handle in the long run.

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  
    2626from zope.component import getUtilitiesFor
    2727from zope.interface import Interface
    28 from waeup.sirp.interfaces import IThemesProvider
     28from zope.schema.interfaces import IVocabularyFactory
     29from waeup.sirp.interfaces import SimpleWAeUPVocabulary
    2930from waeup.sirp.browser.interfaces import ITheme
    3031from waeup.sirp.browser.resources import waeuptheme_red1, waeuptheme_gray1
     
    5859        yield name, theme
    5960
    60 class ThemesProvider(grok.GlobalUtility):
    61     """A global utility providing all themes available.
     61class 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.
    6282    """
    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.
    6588
    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  
    1414from waeup.sirp.permissions import RoleSource
    1515
    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 (aka
    21     skins) we cannot follow that path because we need a theme as an
    22     attribute of each `University` instance.
    23 
    24     Therefore we must tell the browser subpackage how we expect themes
    25     to be announced code-wise.
    26 
    27     What we tell here is that we might ask for a utility providing
    28     IThemeProvider and that this component then should be callable to
    29     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 where
    35         ``<NAME>`` is the internal name under which a theme was
    36         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.description
    57         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 ``source
    64     = ThemeSource()``. With `ThemeSource` we cannot do this, because
    65     the set of available themes is not available before the whole
    66     `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 are
    70     really needed and after the process of grokking and parsing
    71     everything on startup.
    72 
    73     So, although this binder binds to some context (`IUniversity` is
    74     currently the only interface making use of this source, so
    75     `University` instances are the only context used), the context
    76     does not really matter.
    77     """
    78     implements(schema.interfaces.IContextSourceBinder)
    79 
    80     def __call__(self, context):
    81         return ThemesSource()
    8216
    8317class FatalCSVError(Exception):
     
    9226    return SimpleVocabulary([
    9327            SimpleTerm(value, value, title) for title, value in terms])
    94 
    9528
    9629class IWAeUPObject(Interface):
     
    11346        title = u'Skin',
    11447        default = u'waeuptheme-gray1.css',
    115         source = ThemesSourceBinder(),
     48        vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary',
    11649        required = True,
    11750        )
Note: See TracChangeset for help on using the changeset viewer.