4

I have made some new content type using Dexterity. I now wish to create the content from a python script. All is well with the line below, and the item is generated in the target folder with the correct id and date. But how do you pass the file data to a file field, the image data to the image field and the richt_text data to the rich_text field?

target.invokeFactory(type_name="content_type_name", id=id, date=date, file=file, image=image, rich_text=rich_text)

The date I could figure out; Dexterity wants the Python datetime format:

datetime.datetime(2011,1,1)

Thank you very much for your help - I am sure I am missing something quite elementary here, but haven't found it - probably because I am looking in the wrong place.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Horst
  • 41
  • 1

1 Answers1

7

For file use plone.namedfile.NamedFile and for image use plone.namedfile.NamedImage and for rich text use plone.app.textfield.value.RichTextValue

e.g.

from plone.namedfile import NamedFile
from plone.namedfile import NamedImage
from plone.app.textfield.value import RichTextValue

file = NamedFile("<html></html>", "text/html", u"text.html")
logo = ... some binary data in a byte string ...
image = NamedImage(logo, filename="logo.gif")
rich_text = RichTextValue(u"<p>A paragraph</p>", 'text/html', 
        'text/x-html-safe', 'utf-8')
target.invokeFactory(type_name="content_type_name", id=id, date=date, file=file,
        image=image, rich_text=rich_text)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
scarba05
  • 2,943
  • 1
  • 27
  • 29