plone.api 與 plone.protect

plone5預設會使用plone.protect 3,而plone.protect在3.0以後,增加了Automatic CSRF Protection機制,有效提高安全性,但是恰巧,卻會使得plone.api.content.create()無法直接新增一個content...

在plone 4.X中,plone.protect使用2.X版,還沒有Automatic CSRF Protection,所以可以在程式中的任何地方使用plone.api.content.create(),直接新增content

但這在plone5將會得到error, 使用以下技巧來解決

from plone import api
import transaction
...
api.content.create(
    type='Document',
    title='document title',
    container=self.context)

transaction.commit()

記得在plone.api.content.create()之後執行transaction.commit(),即可完成操作

 

更新(2016/8/4)

直接把CSRF Protection關掉是更直接的方法,如下

from plone.protect.interfaces import IDisableCSRFProtection
from zope.interface import alsoProvides

alsoProvides(request, IDisableCSRFProtection)