Commit 566c0c5f authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

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

Fixed bug reported by a5a2f1cd (comment 38674)

/reviewed-on !363
parent e1c1fda0
...@@ -42,7 +42,6 @@ from Products.PythonScripts.standard import url_quote_plus ...@@ -42,7 +42,6 @@ from Products.PythonScripts.standard import url_quote_plus
from Products.Formulator.Errors import FormValidationError, ValidationError from Products.Formulator.Errors import FormValidationError, ValidationError
import string import string
import copy
class FormBoxWidget(Widget.Widget): class FormBoxWidget(Widget.Widget):
""" """
...@@ -121,18 +120,18 @@ class FormBoxWidget(Widget.Widget): ...@@ -121,18 +120,18 @@ class FormBoxWidget(Widget.Widget):
return '' return ''
class FormBoxEditor: class FormBoxEditor:
""" """An editor returned from FormBox validation able to `edit` document."""
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.
Keep a reference to the correct context and don't expect the caller to provide it def __init__(self, result, context_method_id=None):
during the edit phase because they don't have access to the widget anymore. """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.attr_dict, self.editor_list = result
self.context = context self.context_method_id = context_method_id
def view(self): def view(self):
return self.__dict__ return self.__dict__
...@@ -141,10 +140,8 @@ class FormBoxEditor: ...@@ -141,10 +140,8 @@ class FormBoxEditor:
pass pass
def edit(self, context): def edit(self, context):
"""Edit inside correct context.""" if self.context_method_id:
if self.context is not None: context = getattr(context, self.context_method_id)
context = self.context
context.edit(**self.attr_dict) context.edit(**self.attr_dict)
for encapsulated_editor in self.editor_list: for encapsulated_editor in self.editor_list:
encapsulated_editor.edit(context) encapsulated_editor.edit(context)
...@@ -155,7 +152,7 @@ class FormBoxEditor: ...@@ -155,7 +152,7 @@ class FormBoxEditor:
XXX This API is probably not stable and may change, as some editors are used to XXX This API is probably not stable and may change, as some editors are used to
edit multiple objects. 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: for encapsulated_editor in self.editor_list:
if hasattr(encapsulated_editor, 'as_dict'): if hasattr(encapsulated_editor, 'as_dict'):
result_dict.update( result_dict.update(
...@@ -192,7 +189,7 @@ class FormBoxValidator(Validator.Validator): ...@@ -192,7 +189,7 @@ class FormBoxValidator(Validator.Validator):
# XXX Hardcode script name # XXX Hardcode script name
result, result_type = here.Base_edit(formbox_target_id, silent_mode=1, key_prefix=key) result, result_type = here.Base_edit(formbox_target_id, silent_mode=1, key_prefix=key)
if result_type == 'edit': if result_type == 'edit':
return FormBoxEditor(field.id, result, context=here) return FormBoxEditor(result, context_method_id)
elif result_type == 'form': elif result_type == 'form':
formbox_field_errors = REQUEST.get('field_errors', []) formbox_field_errors = REQUEST.get('field_errors', [])
current_field_errors.extend(formbox_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 to comment