4

I fear I may be overlooking something very obvious, but I'd be grateful for any suggestions. I have a plain text file called 'settings' in a Ploneformgen form folder in Plone. The code below successfully alters the text in this file when I put it in a Python script called when the form is viewed using an override in one of the form's fields (e.g. Default Expression in a string field).

obj = context['settings']
obj.setText('Some text:2;More text:2')
obj.reindexObject()

My problem is that I would like to be able to modify the text in 'settings' using either a Custom Script Adapter or a script called using the After Validation Script override of the form. Neither of these work (and overrides in individual fields for validating the field don't seem to allow this either).

Is there some reason why setText() works in some places and not in others (the line obj = context['settings'] doesn't appear to be a problem)? What am I missing?

As far as I can see this isn't a problem of permissions, and I'm a bit baffled that code that works if called when the form is viewed doesn't work if called when the form has been submitted.

I can create a new text file and add text to it using scripts called in these ways no problem: it seems to be a specific problem with calling setText() on an existing file.

SteveM
  • 6,058
  • 1
  • 16
  • 20

1 Answers1

1

The solution is to set the mimetype explicitly when calling setText():

obj.setText("Some text", mimetype='text/plain')

or

obj.setText("Some text", mimetype='text/html')

as appropriate. I don't know why this works, but it does.