可以在 RestrictedPython 中从 PloneFormGen 脚本访问文件上传吗?

发布于 2024-12-26 13:47:38 字数 3723 浏览 1 评论 0原文

目前我正在尝试设置一个表单来创建新闻项目。到目前为止,该脚本运行良好。文件字段出现问题:当我尝试通过 request.form['pub-core-cover'] 访问文件时,我得到一个“Module AccessControl.ZopeGuards,第 67 行,inguarded_getitem”。

有人建议我使用 HelperView 来绕过 RestrictedPython,这似乎是这里的问题。这对我来说相当困难,因为到目前为止我还没有开发出附加产品。不知何故,我也想知道是否还有另一种可能性可以使 PFG 中的文件字段功能在 RestrictedPython 中再次工作。否则文件字段就会变得过时。

CustomScript-Adapter(不要介意德语注释):

form = request.form

# ID des Zielverzeichnisses ist publikationen
target = context.publikationen

# Einmalige ID für das neu zu erstellende Objekt erstellen anhand des Datums + Uhrzeit
from DateTime import DateTime
uid = str(DateTime().millis())

# Titel und ID festlegen und damit News-Objekt erzeugen (Titel + Beschreibung)
title = form['author-prename'] + " " + form['author-surname']
desc = form['pub-core-title'] + " " + form['pub-core-subtitle']

target.invokeFactory("News Item", id = uid, title = title.upper(), description = desc, image = form['pub-core-cover'])

# Objekt aufspüren und ContentType festlegen
obj = target[uid]
obj.setContentType('text/html')

# Inhalt des News-Items setzen
obj.setText("<p>"+ form['pub-core-description'] +"<br /><br />Veröffentlicht: "+ form['pub-tech-year'] +"<br />ISBN: "+ form['pub-tech-isbn'] +"<br />Preis: "+ form['pub-tech-price'] +"<br />" + form['pub-tech-pages'] + " Seiten, " + form['pub-tech-binding'] + "</p>")

# Objekt veröffentlichen ohne den Initial-State im Workflow zu verändern
obj.portal_workflow.doActionFor(obj, 'publish', comment='Dieser Inhalt wurde über den PythonScriptAdapter von PloneFormGen automatisch publiziert.')

# Content reindexieren, um das neue Objekt anzuzeigen
obj.reindexObject()

Traceback:

Traceback (innermost last):
  Module ZPublisher.Publish, line 127, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 47, in call_object
  Module Products.CMFFormController.FSControllerPageTemplate, line 91, in __call__
  Module Products.CMFFormController.BaseControllerPageTemplate, line 26, in _call
  Module Products.CMFFormController.FormController, line 384, in validate
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 47, in call_object
  Module Products.CMFFormController.FSControllerValidator, line 58, in __call__
  Module Products.CMFFormController.Script, line 145, in __call__
  Module Products.CMFCore.FSPythonScript, line 130, in __call__
  Module Shared.DC.Scripts.Bindings, line 324, in __call__
  Module Shared.DC.Scripts.Bindings, line 361, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 20, in fgvalidate_base
   - <FSControllerValidator at /breyer_verlag/fgvalidate_base used for /breyer_verlag/publikationen/publikation-hinzufuegen>
   - Line 20
  Module Products.PloneFormGen.content.form, line 566, in fgvalidate
  Module Products.PloneFormGen.content.form, line 607, in fgProcessActionAdapters
  Module Products.PloneFormGen.content.customScriptAdapter, line 187, in onSuccess
  Module Products.PloneFormGen.content.customScriptAdapter, line 218, in executeCustomScript
  Module Shared.DC.Scripts.Bindings, line 324, in __call__
  Module Shared.DC.Scripts.Bindings, line 361, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 27, in create-publication
   - <PythonScript at /breyer_verlag/publikationen/publikation-hinzufuegen/create-publication/create-publication>
   - Line 27
  Module AccessControl.ZopeGuards, line 67, in guarded_getitem
KeyError: 'pub-core-cover'

我在 Plone3 中使用非常相似的代码没有任何困难。我将不胜感激任何帮助我摆脱痛苦的帮助。

编辑: 顺便一提: 克隆人4.05 PFG 1.72a

Currently I'm trying to set up a Form to create a News-Item from. The Script works fine so far. Problems arise with the File-Field: As soon as I try to access the file via request.form['pub-core-cover'] I get an "Module AccessControl.ZopeGuards, line 67, in guarded_getitem".

I was advised to use a HelperView to bypass RestrictedPython which seems to be the issue here. This is pretty difficult for me due to the fact I have not developed an add-on product so far. Somehow I'm wondering as well if there is another possibility to make the File-Field feature in PFG work again within RestrictedPython. Otherwise the file-field becomes somehow obsolet.

CustomScript-Adapter (don't mind the german comments):

form = request.form

# ID des Zielverzeichnisses ist publikationen
target = context.publikationen

# Einmalige ID für das neu zu erstellende Objekt erstellen anhand des Datums + Uhrzeit
from DateTime import DateTime
uid = str(DateTime().millis())

# Titel und ID festlegen und damit News-Objekt erzeugen (Titel + Beschreibung)
title = form['author-prename'] + " " + form['author-surname']
desc = form['pub-core-title'] + " " + form['pub-core-subtitle']

target.invokeFactory("News Item", id = uid, title = title.upper(), description = desc, image = form['pub-core-cover'])

# Objekt aufspüren und ContentType festlegen
obj = target[uid]
obj.setContentType('text/html')

# Inhalt des News-Items setzen
obj.setText("<p>"+ form['pub-core-description'] +"<br /><br />Veröffentlicht: "+ form['pub-tech-year'] +"<br />ISBN: "+ form['pub-tech-isbn'] +"<br />Preis: "+ form['pub-tech-price'] +"<br />" + form['pub-tech-pages'] + " Seiten, " + form['pub-tech-binding'] + "</p>")

# Objekt veröffentlichen ohne den Initial-State im Workflow zu verändern
obj.portal_workflow.doActionFor(obj, 'publish', comment='Dieser Inhalt wurde über den PythonScriptAdapter von PloneFormGen automatisch publiziert.')

# Content reindexieren, um das neue Objekt anzuzeigen
obj.reindexObject()

Traceback:

Traceback (innermost last):
  Module ZPublisher.Publish, line 127, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 47, in call_object
  Module Products.CMFFormController.FSControllerPageTemplate, line 91, in __call__
  Module Products.CMFFormController.BaseControllerPageTemplate, line 26, in _call
  Module Products.CMFFormController.FormController, line 384, in validate
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 47, in call_object
  Module Products.CMFFormController.FSControllerValidator, line 58, in __call__
  Module Products.CMFFormController.Script, line 145, in __call__
  Module Products.CMFCore.FSPythonScript, line 130, in __call__
  Module Shared.DC.Scripts.Bindings, line 324, in __call__
  Module Shared.DC.Scripts.Bindings, line 361, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 20, in fgvalidate_base
   - <FSControllerValidator at /breyer_verlag/fgvalidate_base used for /breyer_verlag/publikationen/publikation-hinzufuegen>
   - Line 20
  Module Products.PloneFormGen.content.form, line 566, in fgvalidate
  Module Products.PloneFormGen.content.form, line 607, in fgProcessActionAdapters
  Module Products.PloneFormGen.content.customScriptAdapter, line 187, in onSuccess
  Module Products.PloneFormGen.content.customScriptAdapter, line 218, in executeCustomScript
  Module Shared.DC.Scripts.Bindings, line 324, in __call__
  Module Shared.DC.Scripts.Bindings, line 361, in _bindAndExec
  Module Products.PythonScripts.PythonScript, line 344, in _exec
  Module script, line 27, in create-publication
   - <PythonScript at /breyer_verlag/publikationen/publikation-hinzufuegen/create-publication/create-publication>
   - Line 27
  Module AccessControl.ZopeGuards, line 67, in guarded_getitem
KeyError: 'pub-core-cover'

I had no difficulties using an pretty similar code in Plone3. I would appreciate any help that gets me out of my misery.

Edit:
By the way:
Plone 4.05
PFG 1.72a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-01-02 13:47:39

正如 vangheem 所说,这不是一个受限制的 Python 问题,它与 PloneFormGen 如何处理数据字段有关。将 _file 添加到变量名称非常重要。

As vangheem stated it is not a RestrictedPython-Problem put has to do how PloneFormGen handles the data field. It is important to add _file to the variable name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文