5

私の Plone サイトの 1 つに、文字を生成するために使用する器用さのモデルがいくつかあります。モデルは次のとおりです。「モデル」(レターの基本コンテンツ)、「連絡先」(名前、住所などの連絡先情報を含む)、および「マージ」(レンダリングされたモデル オブジェクトで、一部を置換します)受信者情報を含むモデルの一部)。「Merge」オブジェクトのスキーマは次のとおりです。

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 に答える