【问题标题】:Can't create Dexterity content type programatically in Plone 4.3.1无法在 Plone 4.3.1 中以编程方式创建敏捷内容类型
【发布时间】:2013-08-05 18:41:39
【问题描述】:

我使用站点设置中的敏捷管理器创建了一个新的内容类型。我能够通过 Plone 用户界面成功添加内容,但我已经到了需要使用 python 脚本创建相同对象的地步。

我的第一次尝试是使用 invokeFactory:

context.invokeFactory("mycontenttype", id="test", Title="Test")

同样的代码适用于“文件夹”对象,但我的敏捷类型失败:

Traceback (innermost last):
  Module ZPublisher.Publish, line 60, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 46, in call_object
  Module Shared.DC.Scripts.Bindings, line 322, in __call__
  Module Products.PloneHotfix20130618.spamProtect, line 35, in _patched_bindAndExec
  Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 7, in test
   - <PythonScript at /three-year-plan/test>
   - Line 7
TypeError: invokeFactory() takes at least 3 arguments (2 given)

经过一番搜索,我找到了几个地方引用了以下函数:

from plone.dexterity.utils import createContentInContainer

任何尝试在我的 python 脚本中从 plone.dexterity.utils 导入都会导致权限错误:

Traceback (innermost last):

  Module ZPublisher.Publish, line 60, in publish
  Module ZPublisher.mapply, line 77, in mapply
  zModule ZPublisher.Publish, line 46, in call_object
  Module Shared.DC.Scripts.Bindings, line 322, in __call__
  Module Products.PloneHotfix20130618.spamProtect, line 35, in _patched_bindAndExec
  Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 1, in test
    - <PythonScript at /my-site/test>
    - Line 1
  Module AccessControl.ZopeGuards, line 305, in guarded_import
  Unauthorized: import of 'plone.dexterity.utils' is unauthorized

对于解决上述错误或替代方法的任何帮助将不胜感激。版本详细信息发布在下面以供参考。

版本概览

Plone 4.3.1 (4306)
CMF 2.2.7
Zope 2.13.19
Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3]
PIL 1.7.8 (Pillow)
Dexterity Content Types 2.0.8

【问题讨论】:

    标签: plone zope dexterity


    【解决方案1】:

    您应该能够为此目的使用“invokeFactory”。您在容器上调用它并将类型名称作为字符串传递给它。

    我不确定您尝试使用 invokeFactory 方法的地方出了什么问题,但它确实有效。详情请见http://developer.plone.org/reference_manuals/external/plone.app.dexterity/reference/manipulating-content-objects.html?highlight=invokefactory#adding-an-object-to-a-container

    【讨论】:

      【解决方案2】:

      您的猜测是正确的:您必须使用 createContentInContainer。 但是你不能在 Python 脚本中使用它,因为只允许所谓的受限 Python [1]。将其放在视图中 [2]。

      作为参考看看:

      1. http://developer.plone.org/reference_manuals/active/helloworld/extend/view.html
      2. http://plone.org/documentation/faq/restricted-python-scripts

      【讨论】:

      • 我们应该考虑一下是否应该将 Dexterity 便利函数标记为安全的脚本。在 dev.plone.org 提交功能请求票。如果您想自己尝试这样做,github.com/collective/collective.localfunctions 可能会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 2015-12-27
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多