plone – 为什么setText()会更改Document的contentType?

当我更改文字时,例如在具有setText()的Document中,Document的内容类型更改为text / plain.

In [1]: app.Plone.invokeFactory('Document', 'doc')
Out[1]: 'doc'

In [2]: app.Plone.doc.getContentType()
Out[2]: 'text/html'

In [3]: app.Plone.doc.setText('xyz'); app.Plone.doc.getContentType()
Out[3]: 'text/plain'

In [4]: app.Plone.doc.setText('<abc>xyz</abc>'); app.Plone.doc.getContentType()
Out[4]: 'text/html'

即使我创建了一个Document并且我将mimetype显式设置为text / plain,它也会将类型更改为text / html.

In [1]: app.Plone.invokeFactory('Document', 'doc', 
                                 text='<abc>xyz</abc>', 
                                 mimetype='text/plain')
Out[1]: 'doc'

In [2]: app.Plone.doc.getContentType()
Out[2]: 'text/html'

该文本由TextField(FileField)类的_process_input()处理,它猜测类型并更改它.

API是否期望程序员知道_process_input()的所有猜测?如果是的话,这是在某处记录的吗?

最佳答案 我想你的doument是一个ATContentTypes文档.

在这种情况下,除非在setText中给出特定的mimetype,否则将使用默认的mimetype:

doc.setText(u“< p> your text< / p>”,mimetype =’text / html’)

https://github.com/plone/Products.ATContentTypes/blob/2.2.0/Products/ATContentTypes/content/document.py#L112

对于上传的文件,mimetype被猜到了
https://github.com/plone/Products.ATContentTypes/blob/2.2.0/Products/ATContentTypes/content/document.py#L137

点赞