Migrating Archetype news items to Dexterity content types

*爱你&永不变心* 提交于 2019-12-09 18:25:44

问题


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.


回答1:


  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.



来源:https://stackoverflow.com/questions/15692531/migrating-archetype-news-items-to-dexterity-content-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!