Question

I created a new content type using the Dexterity manager in Site Setup. I'm able to successfully add content through the Plone user interface, but I've come to a point where I need to create the same object with a python script.

My first attempt was with invokeFactory:

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

This same code works for "Folder" objects, but fails with my Dexterity type:

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)

After doing some more searching, I found several places referring to the following function:

from plone.dexterity.utils import createContentInContainer

Any attempt to import from plone.dexterity.utils in my python scripts ends up with a permissions error:

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

Any help in resolving the errors above or alternative methods would be greatly appreciated. Version details are posted below for reference.

Version Overview

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
Was it helpful?

Solution

You should be able to use "invokeFactory" for this purpose. You call it on the container and pass it the type name as a string.

I'm not sure where your attempt to use the invokeFactory method went awry, but it does work. See http://developer.plone.org/reference_manuals/external/plone.app.dexterity/reference/manipulating-content-objects.html?highlight=invokefactory#adding-an-object-to-a-container for details.

OTHER TIPS

Your guess is right: you have to use createContentInContainer. But you cannot use it in a Python script because only the so called restricted Python is allowed [1]. Put it in a view [2].

As a reference have a look to:

  1. http://developer.plone.org/reference_manuals/active/helloworld/extend/view.html
  2. http://plone.org/documentation/faq/restricted-python-scripts
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top