Commit 7140dbc0 by Tomáš Peterka Committed by Eteri

[erp5] Form/FormBox does not hold context reference but only method_getter name

Fixed bug reported by nexedi/erp5@a5a2f1cd (comment 38674)

/reviewed-on nexedi/erp5!363
parent fbbf4c1e
......@@ -42,7 +42,6 @@ from Products.PythonScripts.standard import url_quote_plus
from Products.Formulator.Errors import FormValidationError, ValidationError
import string
import copy
class FormBoxWidget(Widget.Widget):
"""
......@@ -121,18 +120,18 @@ class FormBoxWidget(Widget.Widget):
return ''
class FormBoxEditor:
"""
A class holding all values required to update the object
"""
def __init__(self, field_id, result, context=None):
"""Initialize with all necessary information for editing.
"""An editor returned from FormBox validation able to `edit` document."""
Keep a reference to the correct context and don't expect the caller to provide it
during the edit phase because they don't have access to the widget anymore.
def __init__(self, result, context_method_id=None):
"""Initialize with all necessary information for editing a document.
:result: tuple of attributes, editors intended as parameters for edit function
:context_method_id: editor needs to operate on the correct context (Document)
but it cannot hold reference because then weird failures
appear; thus we keep name of the context-obtaining method
"""
self.field_id = field_id
self.attr_dict, self.editor_list = result
self.context = context
self.context_method_id = context_method_id
def view(self):
return self.__dict__
......@@ -141,10 +140,8 @@ class FormBoxEditor:
pass
def edit(self, context):
"""Edit inside correct context."""
if self.context is not None:
context = self.context
if self.context_method_id:
context = getattr(context, self.context_method_id)
context.edit(**self.attr_dict)
for encapsulated_editor in self.editor_list:
encapsulated_editor.edit(context)
......@@ -155,7 +152,7 @@ class FormBoxEditor:
XXX This API is probably not stable and may change, as some editors are used to
edit multiple objects.
"""
result_dict = copy.copy(self.attr_dict)
result_dict = self.attr_dict.copy() # avoid modifying own attribute
for encapsulated_editor in self.editor_list:
if hasattr(encapsulated_editor, 'as_dict'):
result_dict.update(
......@@ -192,7 +189,7 @@ class FormBoxValidator(Validator.Validator):
# XXX Hardcode script name
result, result_type = here.Base_edit(formbox_target_id, silent_mode=1, key_prefix=key)
if result_type == 'edit':
return FormBoxEditor(field.id, result, context=here)
return FormBoxEditor(result, context_method_id)
elif result_type == 'form':
formbox_field_errors = REQUEST.get('field_errors', [])
current_field_errors.extend(formbox_field_errors)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment