I'm trying to copy the content in news items to other content type that I wrote. In my script I have the news
item and the project
item. The second, project
, is a content type defined using Dexterity. It would be wonderful if I could to copy the image and the body text from news
to project
in the next way.
project.text = news.text
project.image = news.image
Where text and image are defined in the project
schema as RichText and NamedBlobImage. I don't know how the attributes are in the news item. I only know that I can get the image in the news item using the method getImage()
but assign it to the project generates an error when rendering the project.
So I need some pointers to solve my basic questions:
How can I know the attribute names for Archetype content types. For example, in this case I need to know the name of the attribute for the body text of news item.
How can I convert an image attached to a news item to an image attached to a dexterity content type.
You use the field from the Archetypes schema to retrieve values, preferably the raw values in this case. You pass in the object then calling either .get()
or .getRaw()
:
schema = news.Schema()
news = schema.getField('text').getRaw(news)
imageField = schema.getField('image')
image = imageField.getRaw(news)
content_type = imageField.getContentType(news)
filename = imageField.getFilename(news)
The object returned by the ImageField.getRaw()
call is basically a OFS.Image
instance. You can call str()
on it to get the raw image data.
To set the image object, you really want to get the image field from the schema and use it's ._type
attribute as a factory:
project.image = IProjectInterface.image._type(str(image),
contentType=content_type, filename=filename)
The content type here is optional; the NamedImage
and NamedBlobImage
types sniff out the content type automatically too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With