Plone and Dexterity: Default Values ​​for a Relationship Field

On one of my Plone sites, I have several dexterity patterns that I use to create letters. Models are: “Model” (the basic contents of the letter), “Contact” (which contains contact information, such as name, address, etc.) and “Combine” (which is the object of the model, which we substitute parts of the model with recipient information). The scheme of the Merge object is as follows:

class IMergeSchema(form.Schema):
    """
    """
    title = schema.TextLine(
        title=_p(u"Title"),
        )

    form.widget(text='plone.app.z3cform.wysiwyg.WysiwygFieldWidget')
    text = schema.Text(
        title=_p(u"Text"),
        required=False,
        )

    form.widget(recipients=MultiContentTreeFieldWidget)
    recipients = schema.List(
        title=_('label_recipients',
                 default='Recipients'),
        value_type=schema.Choice(
            title=_('label_recipients',
                      default='Recipients'),
            # Note that when you change the source, a plone.reload is
            # not enough, as the source gets initialized on startup.
            source=UUIDSourceBinder(portal_type='Contact')),
        )

    form.widget(model=ContentTreeFieldWidget)
    form.mode(model='display')
    model = schema.Choice(
        title=_('label_model',
                  default='Model'),
        source=UUIDSourceBinder(portal_type='Model'),
        )

"" , "" , , . , : http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors

, "". ( , ;)):

@form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
    contacts =  [x for x in data.context.contentValues()
                 if IContact.providedBy(x)]
    paths =  [u'/'.join(c.getPhysicalPath()) for c in contacts]
    uids = [IUUID(c, None) for c in contacts]

    print 'Contacts: %s' % contacts
    print 'Paths: %s' % paths
    print 'UIDs: %s' % uids

    return paths

, ( "self.widgets ['recipients'.] ", ) UID, .

, .

, .

+5
1

, "int_id" . , info::

from zope.component import getUtility
from zope.intid.interfaces import IIntIds

@form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
    contacts =  [x for x in data.context.contentValues()
                 if IContact.providedBy(x)]
    intids = getUtility(IIntIds)
    # The following gets the int_id of the object and turns it into
    # RelationValue
    values = [RelationValue(intids.getId(c)) for c in contacts]

    print 'Contacts: %s' % contacts
    print 'Values: %s' % values

    return values
+3

All Articles