Ploneformgen dynamically populate selection field

582 Views Asked by At

I am trying to customize a ploneformgen form by dynamically populating a selection field. I need to parse a file on my filesystem and select certain options based on their date and time. Creating the python script was no brainer but then where do I place the script?

  1. From what I understand, external methods are not the preferred choice in plone 4
  2. I tried to add a python script via ZMI but then I was hit by restricted python and my script would not execute.
  3. I developed an add-on and placed it in the eggs folder and then wrote a python script to call the code from the add-on but unfortunately I was hit again with the same error as previously "You do not have sufficient permissions to view this page". From what I understood, code inside an add-on is not restricted, or?

What would be the best option to achieve the customisation of the form?

1

There are 1 best solutions below

0
On

If you used a skin script, then yes, that is run as restricted python as well. It is the filesystem-stored equivalent of the ZMI python script.

Best practice is to use a browser view; it is simply a callable object that has a request and context associated to it:

from zope.publisher.browser import BrowserView

class MyBrowserView(BrowserView):
    def __call__(self):
        request = self.request
        context = self.context
        # Do something with the request and context

Register this in your configure.zcml:

<browser:view
    for="*"
    name="mybrowserview"
    class=".views.MyBrowserView"
    permission="zope2.Public"
    />

after which it'll be available as someobject/@@mybrowserview for PloneFormGen.

However, if this is the only thing you are creating a custom package for then by all means use an external method.