在plone4的文件夹中限制每个成员一个内容项



我已经创建了一个自定义的原型内容类型"r sum",并且想要强制执行一个限制,允许成员在一个文件夹中只添加一个这种类型的项。更好的方法是将成员重定向到他或她的条目的edit页面,如果该条目已经存在于该文件夹中。

如何执行此限制并提供此额外功能?

Plone 3的类似用例的解决方案可以在eestec.base中找到。我们通过重写createObject来做到这一点。复制并添加一个特殊的检查

代码在集合SVN中,在http://dev.plone.org/collective/browser/eestec.base/trunk/eestec/base/skins/eestec_base_templates/createObject.cpy,第20-32行。

嗯,这是一种验证约束,所以也许添加一个验证器到标题字段,在现实中不关心标题,但检查用户等?(我认为字段验证器传递了足够的信息来完成此操作,如果没有,覆盖post_validate方法或监听相应的事件应该可以工作。)

如果您尝试这样做,请记住,当用户正在编辑时(即在将焦点移出字段时),post_validate已经被调用。

我不知道这是否是最佳实践,但是您可以在创建时从基础对象连接def at_post_create_script,在删除时连接manage_beforeDelete

例如:

from Products.ATContentTypes.lib import constraintypes
class YourContentype(folder.ATFolder)
[...]
    def at_post_create(self):
        origin = aq_parent(aq_inner(self))
        origin.setConstrainTypesMode(constraintypes.ENABLED)  # enable constrain types
        org_types = origin.getLocallyAllowedTypes()           # returns an immutable tuple
        new_types = [x for x in org_types if x! = self.portal_type]       # filter tuple into a list
        origin.setLocallyAllowedTypes(new_types)
    def manage_beforeDelete(self, item, container)          
        BaseObject.manage_beforeDelete(self, item, container)     # from baseObject
        self._v_cp_refs = None                                    # from baseObject
        origin = aq_parent(aq_inner(self))
        origin.setConstrainTypesMode(constraintypes.ENABLED)  # enable constrain types
        org_types = origin.getLocallyAllowedTypes()           # returns an immutable tuple
        new_types = [x for x in org_types].append(self.portal_type)
        origin.setLocallyAllowedTypes(new_types)

注意:还有一个叫做setImmediatelyAddableTypes()的方法,你可能想探索一下。注意二:这不能在内容迁移中存活。

相关内容

最新更新