2011-11-01 75 views
7

我正在使用的項目使用了Plone的超棒的敏捷插件。我的一些自定義內容類型具有必須計算的非常具體的名稱。我原本之前完成此操作的方法是通過增加plone.app.content.interfaces.INameFromTitle作爲對象的通用設置條目的行爲,按照說明書的指示:有沒有辦法擴展Plone靈巧的INameFromTitle行爲?

<?xml version="1.0"?> 
<object name="avrc.aeh.cycle" meta_type="Dexterity FTI"> 
    ... 
    <property name="schema">myproject.mytype.IMyType</property> 
    <property name="klass">plone.dexterity.content.Item</property> 
    ... 
    <property name="behaviors"> 
    <element value="plone.app.content.interfaces.INameFromTitle" /> 
    </property> 
    ... 
</object> 

然後我創建了一個適配器將提供INameFromTitle:

from five import grok 
from zope.interface import Interface 
import zope.schema 
from plone.app.content.interfaces import INameFromTitle 

class IMyType(Interface): 

    foo = zope.schema.TextLine(
     title=u'Foo' 
     ) 

class NameForMyType(grok.Adapter): 
    grok.context(IMyType) 
    grok.provides(INameFromTitle) 

    @property 
    def title(self): 
     return u'Custom Title %s' % self.context.foo 

這種方法是非常相似的是,在這篇博客文章建議:

http://davidjb.com/blog/2010/04/plone-and-dexterity-working-with-computed-fields

不幸的是,這種方法在plone.app.dexterity測試版後停止工作,現在我的內容項目沒有正確指定其名稱。

有人會碰巧知道如何爲非常特定的命名用例擴展敏捷的INameFromTitle行爲嗎?

非常感謝您的幫助,謝謝!

回答

4

您可以嘗試以下操作。

interfaces.py

from plone.app.content.interfaces import INameFromTitle 

class INameForMyType(INameFromTitle): 

    def title(): 
     """Return a custom title""" 

behaviors.py

from myproject.mytype.interfaces import INameForMyType 

class NameForMyType(object): 
    implements(INameForMyType) 

    def __init__(self, context): 
     self.context = context 

    @property 
    def title(self): 
     return u"Custom Title %s" % self.context.foo 

我一般喜歡使用定義我的ZCML適配器;在configure.zcml中

<adapter for="myproject.mytype.IMyType" 
     factory=".behaviors.NameForMyType" 
     provides=".behaviors.INameForMyType" 
     /> 

,但你很可能也使用grok.global_adapter。

3

我用行爲做了,在behaviors.zcml的configure.zcml適應INameFromTitle

behaviors.py

class INameFromBrandAndModel(Interface): 
    """ Interface to adapt to INameFromTitle """ 

class NameFromBrandAndModel(object): 
    """ Adapter to INameFromTitle """ 
    implements(INameFromTitle) 
    adapts(INameFromBrandAndModel) 

    def __init__(self, context): 
     pass 

    def __new__(cls, context): 
     brand = context.brand 
     model = context.modeltype  
     title = u'%s %s' % (brand,model) 
     inst = super(NameFromBrandAndModel, cls).__new__(cls) 

     inst.title = title 
     context.setTitle(title) 

     return inst 

<plone:behavior 

    title="Name from brand and model" 
    description="generates a name from brand and model attributes" 
    for="plone.dexterity.interfaces.IDexterityContent" 
    provides=".behavios.INameFromBrandAndModel" 
    /> 

<adapter factory=".behaviors.NameFromBrandAndModel" /> 

然後禁用INameFromTitle行爲profiles/types/your.contenttype.xml

瞧。這整合得非常好,並在默認視圖和導航中顯示正確的標題。從適配器中刪除context.setTitle(title)只會給我們留下一個正確的ID,但不會有標題集。

這並不會在編輯後自動更改標題。正如經常提示的那樣,到目前爲止,我沒有成功覆蓋我的內容類型的klass屬性。

如果你定義在你的架構中的title屬性,如:

class IBike(form.Schema): 
    """A bike 
    """ 

    title = schema.TextLine(
     title = _(u'Title'), 
     required = False, 
    ) 

以後可以輕鬆地更改標題。應該在addForm中隱藏標題字段,以避免誤解。

相關問題