2012-03-21 66 views
5

在我的一個Plone站點中,我有一些靈巧模型用於生成字母。這些模型是:「模型」(信件的基本內容),「聯繫人」(包含聯繫人信息,例如姓名,地址等)和「合併」(這是一個模型對象呈現,部分模型與收件人信息)。 「合併」對象的架構如下:Plone和敏捷:「relation」字段的默認值

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'] .value」,我得到這種類型的數據)他們的UID,但沒有解決方案的任何影響。

我也嘗試返回元組而不是列表甚至是生成器,但依然沒有任何效果。

當我在實例日誌中看到痕跡時,肯定會調用該方法。

回答

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