[Erp5-report] r20041 - /erp5/trunk/products/ERP5Form/FormBox.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Wed Mar 19 15:07:44 CET 2008
Author: romain
Date: Wed Mar 19 15:07:44 2008
New Revision: 20041
URL: http://svn.erp5.org?rev=20041&view=rev
Log:
Implement a working version of FormBox validator.
Modified:
erp5/trunk/products/ERP5Form/FormBox.py
Modified: erp5/trunk/products/ERP5Form/FormBox.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/FormBox.py?rev=20041&r1=20040&r2=20041&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/FormBox.py (original)
+++ erp5/trunk/products/ERP5Form/FormBox.py Wed Mar 19 15:07:44 2008
@@ -38,10 +38,11 @@
from Products.PythonScripts.Utility import allow_class
from Products.PythonScripts.standard import url_quote_plus
+from Products.Formulator.Errors import FormValidationError, ValidationError
import string
-from zLOG import LOG
+from zLOG import LOG, WARNING, DEBUG, PROBLEM
class FormBoxWidget(Widget.Widget):
"""
@@ -65,11 +66,12 @@
"""
property_names = Widget.Widget.property_names + [
- 'form_id', \
+ 'formbox_target_id', \
]
- form_id = fields.StringField(
- 'form_id',
+ # This name was changed to prevent naming collision with ProxyField
+ formbox_target_id = fields.StringField(
+ 'formbox_target_id',
title='Form ID',
description=(
"ID of the form which must be rendered in this box."),
@@ -84,20 +86,53 @@
default="",
required=0)
-
def render(self, field, key, value, REQUEST):
"""
Render a form in a field
"""
here = REQUEST['here']
- form = getattr(here, field.get_value('form_id'))
- return form()
+ try:
+ form = getattr(here, field.get_value('formbox_target_id'))
+ except AttributeError:
+ LOG('FormBox', WARNING,
+ 'Could not get a form from formbox %s in %s' % \
+ (field.id, field.aq_parent.id))
+ return ''
+ return form(REQUEST=REQUEST)
- def render_view(self, field, value):
- """
- Display a form in a field
+class FormBoxEditor:
+ """
+ A class holding all values required to update the object
+ """
+ def __init__(self, field_id, result):
+ self.field_id = field_id
+ self.result = result
+
+ def view(self):
+ return self.__dict__
+
+ def __call__(self, REQUEST):
+ pass
+
+ def edit(self, context):
+ context.edit(**self.result[0])
+ for encapsulated_editor in self.result[1]:
+ encapsulated_editor.edit(context)
+
+ def as_dict(self):
"""
- return self.render()
+ This method is used to return parameter dict.
+ XXX This API is probably not stable and may change, as some editors are used to
+ edit multiple objects.
+ """
+ result_dict = self.result[0]
+ for encapsulated_editor in self.result[1]:
+ if hasattr(encapsulated_editor, 'as_dict'):
+ result_dict.update(
+ encapsulated_editor.as_dict())
+ return result_dict
+
+allow_class(FormBoxEditor)
class FormBoxValidator(Validator.Validator):
"""
@@ -105,11 +140,30 @@
the result as a single variable.
"""
property_names = Validator.Validator.property_names
+ message_names = Validator.Validator.message_names + \
+ ['form_invalidated',]
+
+ form_invalidated = "Form invalidated."
def validate(self, field, key, REQUEST):
- form = field.aq_parent
- form = getattr(form, field.get_value('form_id'))
- return form.validate(REQUEST)
+ # XXX hardcoded acquisition
+ here = field.aq_parent.aq_parent
+ formbox_target_id = field.get_value('formbox_target_id')
+
+ # Get current error fields
+ current_field_errors = REQUEST.get('field_errors', [])
+
+ # XXX Hardcode script name
+ result, result_type = here.Base_edit(formbox_target_id, silent_mode=1)
+ if result_type == 'edit':
+ return FormBoxEditor(field.id, result)
+ elif result_type == 'form':
+ formbox_field_errors = REQUEST.get('field_errors', [])
+ current_field_errors.extend(formbox_field_errors)
+ REQUEST.set('field_errors', current_field_errors)
+ getattr(here, formbox_target_id).validate_all_to_request(REQUEST)
+ else:
+ raise NotImplementedError, result_type
FormBoxWidgetInstance = FormBoxWidget()
FormBoxValidatorInstance = FormBoxValidator()
More information about the Erp5-report
mailing list