0

I have a python-script, which returns returns a context-based, dynamically generated simple list:

def myVocabMethod(self):
    mylist = ['a','list','apart']
    # do sth dynamic

    return mylist

I would like to pass the result to the selection-field with the vocabulary-attribute looking s.th. like this:

atapi.StringField('theField'
    vocabulary=.myPythonScript.myVocabMethod(),
    (...)
),

How to glue the script-results and and the vocab-value together?

The documentation I found, always requires Grok. Also it's just a simple list, no i18n or other more complex features needed.

Ida
  • 3,994
  • 21
  • 40
  • Can you be more specific? What kind of dynamic vocabulary, what kind of field, etc? Show the code. – Lennart Regebro Jul 29 '13 at 10:17
  • @LennartRegebro: I haven't written any concrete code to glue it together because I haven't found an example for a grokless way, yet. Updated quest with the docs I am referring to. – Ida Jul 29 '13 at 11:20
  • No, but reasonably you have some sort of concrete code where you want to use the vocabulary a schema or form or content type or something. Is this Archetypes? Dexterity? PloneFormGen? Neither? Is what you are asking actually how you do `@grok.provider()` when you don't have grok? That seems to be the only grok-part. – Lennart Regebro Jul 29 '13 at 11:41
  • @LennartRegebro: Ok, thanks so far, I come back back with a code-example, just thought, maybe I missed the fitting docs, 'cause we shouldn't use Grok anymore, or did I get that wrong? – Ida Jul 29 '13 at 11:47
  • Opinions on that matter differs. I certainly have not seen any consensus. – Lennart Regebro Jul 29 '13 at 11:49
  • AFAICT, there is no decorator-based way to replace grok.provider, but you can just use zope.interface.alsoProvides() imperatively after defining your vocabulary function. – sdupton Jul 29 '13 at 12:46
  • Plone core no longer includes grok, but I don't think it will go away for add-ons anytime soon. – aclark Jul 29 '13 at 20:29
  • @sdupton: I didn't go for that one, but thanks for pointing this out! – Ida Jul 30 '13 at 08:55
  • @LennartRegebro: And what's your opinion? :) To me, the reasons described in PLIP #11773 for the exclusion seem to be quite compelling. – Ida Jul 30 '13 at 09:03
  • @aclark: Thanks. Do you by any chance know, where one could read about made decisions whether a component is deprecated or not? – Ida Jul 30 '13 at 09:04
  • I don't have an opinion on the issue. – Lennart Regebro Jul 30 '13 at 09:05
  • @IdaEbkes Not sure but in the worst case you can ask on the developers list – aclark Jul 31 '13 at 10:10

2 Answers2

1

Grokless way to register a named vocabulary:

http://developer.plone.org/forms/vocabularies.html#registering-a-named-vocabulary-provider-in-zcml

Basically you point it to a function which returns SimpleVocabulary instance.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Correct answer :) I was just just courious, if there would be a 'faster' way (see my own answer). And the main difference with Dexterity and Archetypes here is to use Choice(source=named-vocab), respectively get the vocab via getUtilities, right? – Ida Jul 30 '13 at 08:53
1

The post, where I found what I was looking for is this one: http://www.universalwebservices.net/web-programming-resources/zope-plone/dynamic-vocabularies-in-plone-archetypes/

And is referenced in the official docs here: http://developer.plone.org/content/archetypes/fields.html#dynamic-vocabularies

For anyone who might be interested, this is the code:

from Acquisition import aq_parent
from Products.Archetypes import atapi
from Products.Archetypes.public import DisplayList

YourArchetypeSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((

atapi.StringField(
    'somefieldname',
    vocabulary = yourVocabulary,
    ),

))

class YourArchetype(base.ATCTContent):

    def yourVocabulary(self):
        dl = DisplayList()
        # We can do something context-based here, 
        # f.e. get all Events in parent-folder:
        eventlist = aq_parent(self).contentValues(filter={'portal_type' : 'Event'})
        for event in eventlist:
            dl.add(event['id'], event['title'])
        return dl

atapi.registerType(YourArchetype, PROJECTNAME)
Ida
  • 3,994
  • 21
  • 40