2

我想务实地控制单个对象的允许内容类型的添加菜单列表。

我正在使用 archgenxml 构建内容类型的集合。在一种情况下,我有一个由 RangeBase 类组成的模拟类,该类具有三个实现,valueRange、vectorRange 和 uniformRange。模拟可以只包含一个范围,即 RangeBase 的多重性是一,因此模拟的添加菜单应该提供所有三种范围类型或根本不提供。

为此,我想订阅 IObjectInitializedEvent 和 IObjectRemovedEvent 事件;将它们各自的处理程序 initializedHook 和 removedHook 放在 RangeBase 类中。处理程序将请求对象的本地允许类型列表,并相应地删除或添加三个范围。在阅读了 Plone 的“社区开发者文档”之后,我认为 initializedHook 代码可能看起来像这样:

  # Set allowed content types
  from Products.ATContentTypes.lib import constraintypes

  def initializedHook(obj, event):

    # Get this range's parent simulation
    parent = obj.aq_parent

    # Enable constraining
    parent.setConstrainTypesMode(constraintypes.ENABLED)

    # Remove the three ranges 
    allowedTypes = parent.getLocallyAllowedTypes()
    ranges = ('valueRange','vectorRange','uniformRange')
    for range in ranges:
      allowedTypes.remove(range)

    # Tweak the menu
    parent.setLocallyAllowedTypes(allowedTypes)
    parent.setImmediatelyAddableTypes(allowedTypes)

不幸的是,我的模拟类没有这些功能。

是否有一个适配器可以为我的模拟类提供此功能,或者是否有其他完全不同的方法来实现所需的菜单行为?任何建议,将不胜感激。

4

4 回答 4

4

有可能的。

我相信您需要覆盖 getLocallyAllowedType()

http://svn.plone.org/svn/collective/Products.ATContentTypes/trunk/Products/ATContentTypes/lib/constraintypes.py

AT 是在适配器之前编写的,因此 AT 没有使用它。

我建议您也可以更新有关此的文档……这是很常见的用例。

http://web.archive.org/web/20101010142032/http://collective-docs.plone.org/content/creating.html

于 2011-03-04T19:19:31.053 回答
1

在多次尝试调整 _allowedTypes() 失败后,我遵循了http://plone.org/documentation/kb/restrict-addable-types上的最后一个建议并定制了 getNotAddableTypes.py。我的自定义仅列出了三个范围的文件夹内容过滤。如果结果数组不为空,我将三种范围类型添加到列表中:

 # customize this script to filter addable portal types based on
 # context, the current user or other criteria

 ranges = []
 ranges = context.listFolderContents(contentFilter={'portal_type':
                     ('VectorRange','ValueRange','UniformRange')})
 return {True:  ('Favorite', 'VectorRange', 'ValueRange', 'UniformRange'),
         False: ('Favorite')}[len(ranges)]
于 2011-03-11T13:39:01.947 回答
0

有关两种可能性,请参阅此处的最后一篇文章:http: //plone.293351.n2.nabble.com/Folder-constraints-not-applicable-to-custom-content-types-td6073100.html

于 2011-03-05T17:48:05.063 回答
0

方法

foo.getLocallyAllowedTypes()

返回一个元组,您只需将其复制/过滤到另一个元组/列表中,因为它是不可变的。

allowed_types = parent.getLocallyAllowedTypes()
filtered_types = []
for v in allowed_types:
    if not v in ranges:
        filtered_types.append(v)

然后你可以把那个元组给 setter 方法

parent.setLocallyAllowedTypes(filtered_types)

你就完成了。但是,如果您想在对象创建期间访问父文件夹以限制文件夹的内容类型,您可以在其中创建对象,您可以从 BaseObject 中连接 at_post_create_script() 和 manage_beforeDelete()。这对我很有用,将特定内容类型的数量限制在一个文件夹中,并在对象被删除时更正 AllowedTypes。

于 2011-03-13T02:56:52.903 回答