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