How to add the is_folderish attribute to Dexterity objects?

試著忘記壹切 提交于 2019-12-13 19:50:45

问题


The .is_folderish attribute is used in many places. For example when setting an object as the default view or when activating discussions on an object.

My first question is how to check is an object has that attribute set. I tried using the bin/instance debug with something like this:

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

I imagine that I can't reach attributes on that way because app.site.news is a wrapper to the object that has that attribute.

My second question is how to add that attribute to a new Dexterity object. I suppose I can do it using the code below (but I can't test it before the my first question is resolved).

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

But I'm not sure about how both classes could be linked.


回答1:


You don't need to add is_folderish to your types; it is an index in the catalog, and Dexterity types already have the proper attribute for that index, isPrincipiaFolderish.

If you do need to add attributes to content types, you can either use an event subscriber or create a custom subclass of plone.dexterity.content.Item:

  • subscribers can listen for the IObjectCreatedEvent event:

    from zope.app.container.interfaces import IObjectAddedEvent
    
    @grok.subscribe(IYourDexterityType, IObjectCreatedEvent)
    def add_foo_attribute(obj, event):            
        obj.foo = 'baz'
    
  • custom content classes need to be registered in your type XML:

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

    then in your Dexterity FTI XML file, add a klass property:

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


来源:https://stackoverflow.com/questions/15177286/how-to-add-the-is-folderish-attribute-to-dexterity-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!