2011-11-01 11 views
7

私が取り組んでいるプロジェクトは、Ploneのすばらしい敏捷性プラグインを使用しています。いくつかの私のカスタムコンテンツタイプは、計算しなければならない非常に特殊な名前を持っています。私はもともと前にこれを達成した方法は、マニュアルの指示に従って、オブジェクトの汎用的な設定項目での動作としてplone.app.content.interfaces.INameFromTitleを追加することであった。Plone Dexterityの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 betaの後に動作しなくなりましたが、今度はコンテンツアイテムの名前が正しく割り当てられません。

非常に具体的な命名ユースケースに対して、敏捷性のINameFromTitle動作を拡張する方法を知っている人はいますか?

ご協力いただきありがとうございます!

答えて

4

以下を試すことができます。

from plone.app.content.interfaces import INameFromTitle 

class INameForMyType(INameFromTitle): 

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

behaviors.py私は、一般的にZCMLを使用して、私のアダプタを定義することを好む

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 

でinterfaces.py で。 のconfigure.zcml

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

ではなく、あなたはおそらくもgrok.global_adapterを使用することができます。

3

私は behaviors.zcmlまたはのconfigure.zcmlに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 

にINameFromTitle

に適合させることによって、行動でそれをやった

<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のタイトルフィールドを非表示にする必要があります。

関連する問題