|
| 1 | +# from plone import api |
| 2 | +from plone.dexterity.interfaces import IDexterityContent |
| 3 | +from tagung.plone.de import _ |
| 4 | +from zope.globalrequest import getRequest |
| 5 | +from zope.interface import implementer |
| 6 | +from zope.schema.interfaces import IVocabularyFactory |
| 7 | +from zope.schema.vocabulary import SimpleTerm |
| 8 | +from zope.schema.vocabulary import SimpleVocabulary |
| 9 | + |
| 10 | + |
| 11 | +class VocabItem: |
| 12 | + def __init__(self, token, value): |
| 13 | + self.token = token |
| 14 | + self.value = value |
| 15 | + |
| 16 | + |
| 17 | +@implementer(IVocabularyFactory) |
| 18 | +class Levels: |
| 19 | + """ """ |
| 20 | + |
| 21 | + def __call__(self, context): |
| 22 | + # Just an example list of content for our vocabulary, |
| 23 | + # this can be any static or dynamic data, a catalog result for example. |
| 24 | + items = [ |
| 25 | + VocabItem("Platinum", _("Platinum")), |
| 26 | + VocabItem("Gold", _("Gold")), |
| 27 | + VocabItem("Silver", _("Silver")), |
| 28 | + VocabItem("Bronze", _("Bronze")), |
| 29 | + VocabItem("Organizer", _("Organizer")), |
| 30 | + ] |
| 31 | + |
| 32 | + # Fix context if you are using the vocabulary in DataGridField. |
| 33 | + # See https://github.com/collective/collective.z3cform.datagridfield/issues/31: # NOQA: 501 |
| 34 | + if not IDexterityContent.providedBy(context): |
| 35 | + req = getRequest() |
| 36 | + context = req.PARENTS[0] |
| 37 | + |
| 38 | + # create a list of SimpleTerm items: |
| 39 | + terms = [] |
| 40 | + for item in items: |
| 41 | + terms.append( |
| 42 | + SimpleTerm( |
| 43 | + value=item.token, |
| 44 | + token=str(item.token), |
| 45 | + title=item.value, |
| 46 | + ) |
| 47 | + ) |
| 48 | + # Create a SimpleVocabulary from the terms list and return it: |
| 49 | + return SimpleVocabulary(terms) |
| 50 | + |
| 51 | + |
| 52 | +LevelsFactory = Levels() |
0 commit comments