Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating Archetype news items to Dexterity content types

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:

  1. 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.

  2. How can I convert an image attached to a news item to an image attached to a dexterity content type.

like image 918
Daniel Hernández Avatar asked Mar 28 '13 21:03

Daniel Hernández


1 Answers

  1. 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)
    
  2. 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.

like image 113
Martijn Pieters Avatar answered Nov 09 '22 18:11

Martijn Pieters