5

在我的一个 Plone 站点中,我有一些用于生成字母的灵巧模型。模型是:“Model”(信件的基本内容)、“Contact”(包含联系信息,例如姓名、地址等)和“Merge”(渲染的 Model 对象,我们在其中替换一些带有收件人信息的模型部分)。“合并”对象的架构如下:

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'),
        )

在创建新的“合并”对象时,我希望预设“收件人”字段,其中包含创建新对象的文件夹中可用的所有联系人。我按照 Martin Aspelli 的指南为字段添加默认值: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'].value”时,我得到这种类型的数据)它们的 UID,但没有任何解决方案有任何效果。

我还尝试返回元组而不是列表甚至生成器,但仍然没有任何效果。

肯定会调用该方法,因为我在实例日志中看到了痕迹。

4

1 回答 1

3

我认为您需要获取相关内容的“int_id”。这就是敏捷关系字段存储关系信息的方式:

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
于 2013-12-02T18:33:25.383 回答