限制 Plone 4 上文件夹中每个成员的一个内容项

发布于 2024-11-04 14:06:42 字数 149 浏览 3 评论 0原文

我创建了一个名为“Résumé”的自定义 Archetypes 内容类型,并希望强制执行一项限制,允许成员在文件夹中仅添加该类型的一项。更好的方法是将成员重定向到其项目的编辑页面(如果该文件夹中已存在该项目)。

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

I have created a custom Archetypes content type called "Résumé" and would like to enforce a limitation that lets a Member add only one item of this type inside a folder. Even better would be redirecting the member to the edit page of his or her item, if it already exists in that folder.

How can I enforce this limitation and provide this extra functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像你 2024-11-11 14:06:42

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

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

A solution to a similar usecase for Plone 3 can be found in the eestec.base. We did it by overriding the createObject.cpy and adding a special check for this.

Code is in the collective SVN, at http://dev.plone.org/collective/browser/eestec.base/trunk/eestec/base/skins/eestec_base_templates/createObject.cpy, lines 20-32.

你怎么这么可爱啊 2024-11-11 14:06:42

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

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

Well, it is a sort of validation constraint, so maybe add a validator to the title field that in reality does not bother about the title, but checks the user etc.? (I think a field validator is passed enough information to pull this off, if not, overriding the post_validate method or listening to the corresponding event should work.)

If you try this, bear in mind that post_validate is already called while the user is editing (ie on moving focus out of a field).

紅太極 2024-11-11 14:06:42

我不知道这是否是最佳实践,但您可以在创建时连接基础对象的 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() 的方法,您可能想探索一下。
注二:这不会在内容迁移后继续存在。

I dont't know if it's best practice, but you can hook up on def at_post_create_script from base object on creation and manage_beforeDelete on delete.

for example:

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)

Note: There is also a method called setImmediatelyAddableTypes(), which you may want to explore.
Note II: This does not survive content migration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文