2011-11-01 2 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을 제공 할 것이다 :

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

:이 방법은이 블로그 게시물에 제시된 것과 매우 유사하다

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 

불행히도이 메서드는 plone.app.dexterity 베타 후에 작동을 멈추었으므로 이제 내 콘텐츠 항목의 이름이 올바르게 지정되지 않았습니다.

매우 특정한 명명 사용 사례에 대한 민첩성의 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에 제목 필드를 숨기십시오.

관련 문제