How do you determine what attributes and methods are available in PloneFormGen

177 Views Asked by At

How can you determine the methods and attributes of objects in PloneFormGen? I want to write some scripts for a custom field adapter and would like to know what is available. My immediate problem comes from some online code I copied from: Python script to hide ploneformgen form after user has filled it out. (For Plone-4.3.2-64.)

alreadyInDB = False
savedData = ploneformgen.savefield.getSavedFormInputForEdit()
username = request.AUTHENTICATED_USER.getId()
return {'username': 'No way man!'}
usersInDB = [x.split(',')[1] for x in savedData.split('\r\n') if len(x)>0]

if username in usersInDB:
    alreadyInDB = True

if alreadyInDB:
    return {'username': 'No way man!'}

This is the error message I get.

AttributeError: savefield

What I want to do is see what is available for attributes and methods and either fix this or write my own. Any help is appreciated.

This is what we use: PloneFormGen 1.7.12 Products.PFGExtendedMailAdapter 2.4

Plone 4.3.3 (4308) CMF 2.2.7 Zope 2.13.22 Python 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] PIL 2.0.0 (Pillow)

3

There are 3 best solutions below

0
jfaccioni On

Call dir() on an object to return a list of all available methods and attributes it has.

Alternatively, use inspect.getsource() on a function, class, class method or class attribute to return the source code associated with it.

Example using a pandas DataFrame object:

>>> from inspect import getsource
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> dir(df)
    # returns methods and attributes of df
    # which is an instance of a DataFrame object
>>> getsource(pd.DataFrame.head)
    # returns source code for head attribute
    # of a DataFrame object

Note that getsource needs to access the attribute/method of the actual class (in this case pd.DataFrame) instead of an instance of that class (df in this example). So you can use dir first to figure out the methods/attributes, and getsource on a specific method/attribute afterwards to see the code.

0
MrTango On

PloneFormGen uses restricted Python for the TTW scripts. You can use dir() or vars() on your objects to find out what they offer. To see this, you probably want to print them.

TTW scripts are not that great to intropect. Maybe have a look at the related code or use the debug mode to get the Form and use the real debugger there with the same methods from above to see whats going on.

./bin/instance debug Then you will have the root object of the database as 'app'.

Getting your form can be as sdimple as this:

myform = app.unrestrictedTraverse('Plone/my-form')

Getting all form field objects (children):

myform.objectItems()

Getting a specific field (including a dada save adapter object):

myform.objectIds()
myform['adapter-id']

good luck ;)

0
Ida On

In the code-example ploneformgen is the available context-variable, representing the form-folder-object and savefield represents the ID of the save-data-adapter living within the form-folder.

You need to add a save-data-adapter named savefield, or if one exists already change the ID in the script accordingly.