Computed Fields and defaultFactory works fine in Plone Classic but gives an error in Volto

Good afternoon, Plone Community!

We are in the process of preparing a Plone Classic addon to work in Volto. However, after testing several times, we were unable to figure out why the computed fields and defaultFactory works fine in Plone Classic but gives the following error in Volto:

TypeError: compute_employeeID() takes 0 positional arguments but 1 was given

We were using the following code in Plone Classic:

def compute_employeeID():
    user = api.user.get_current()
    property = user.getProperty('employeeID')
    return str(property)


class IRegistrarDocs(model.Schema):
    # directives.mode(employeeID='hidden')
    employeeID = schema.TextLine(
        title=_(u'employeeID'),
        required=False,
        defaultFactory=compute_employeeID,
    )

Why is Volto getting this error when it runs fine in Plone Classic?

TypeError: compute_employeeID() takes 0 positional arguments but 1 was given

I thank you kindly.

Sincerely,

rbrown12

I can give you a suggestion to debug this.
Modify your code to accept a keyword parameter context and add a breakpoint()

def compute_employeeID(context=None):
    breakpoint()
    user = api.user.get_current()
    property = user.getProperty('employeeID')
    return str(property)

Start your instance in foreground and when the PDB shell opens type w.
Than compare the two stacks.
Chances are that in volto it is trying to use a "context aware default factory"

It might as well be that this can be a fix valid for both classic and Volto:

from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory

@provider(IContextAwareDefaultFactory)
def compute_employeeID():
    user = api.user.get_current()
    property = user.getProperty('employeeID')
    return str(property)

More info: Defaults — Plone Documentation v5.2

1 Like
from zope.schema.interfaces import IContextAwareDefaultFactory

@provider(IContextAwareDefaultFactory)

Solved the issue, Thanks @alert

1 Like