2013-03-02 59 views
1

.is_folderish屬性用於許多地方。例如當setting an object as the default viewactivating discussions on an object時。如何將is_folderish屬性添加到敏捷對象?

我的第一個問題是如何檢查一個對象是否具有該屬性集。我嘗試使用bin/instance debug像這樣的東西:

>>> app.site.news.is_folderish 
... 
AttributeError: is_folderish 

我想,我不能這樣達到的屬性,因爲app.site.news是一個包裝到具有屬性的對象。

我的第二個問題是如何將該屬性添加到新的敏捷對象。我想我可以使用下面的代碼來做到這一點(但在我的第一個問題解決之前,我無法測試它)。

from zope import schema 
from plone.dexterity.content import Item 

class IHorse(form.Schema): 
    ... 

class Horse(Item): 
    def __init__(self): 
     super(Horse, self).__init__(id) 
     is_folderish = False 

但我不確定兩個類如何鏈接。

+1

我修改了我以前的評論; 'is_folderish'是索引,'isPrincipiaFolderish'是對象屬性。這是設置在敏捷對象,請參閱[源](https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/content.py)。 – 2013-03-02 17:27:45

回答

2

您不需要將is_folderish添加到您的類型;它是目錄中的一個索引,敏捷類型已具有該索引isPrincipiaFolderish的適當屬性。

如果需要將屬性添加到內容類型,您可以使用事件的用戶或創建的plone.dexterity.content.Item自定義子類:

  • subscribers可以監聽IObjectCreatedEvent事件:

    from zope.app.container.interfaces import IObjectAddedEvent 
    
    @grok.subscribe(IYourDexterityType, IObjectCreatedEvent) 
    def add_foo_attribute(obj, event):    
        obj.foo = 'baz' 
    
  • custom content classes需要在您的類型XML中註冊:

    from plone.dexterity.content import Item 
    
    class MyItem(Item): 
        """A custom content class""" 
        ... 
    

    然後在你的敏捷FTI XML文件,添加一個klass屬性:

    <property name="klass">my.package.myitem.MyItem</property>