2013-03-02 60 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>