Commit 96dda890 authored by Romain Courteaud's avatar Romain Courteaud

Add new methods to Formulator field.

render_html is a alias to Formulator render method.
render_htmlgrid returns a list of tuples (title, html), which can be used in
the page template to display multiples titles for the same field.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4405 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 8dce8c47
...@@ -87,7 +87,6 @@ def Field_render_helper(self, key, value, REQUEST): ...@@ -87,7 +87,6 @@ def Field_render_helper(self, key, value, REQUEST):
else: else:
return self.widget.render(self, key, value, REQUEST) return self.widget.render(self, key, value, REQUEST)
Field.generate_field_key = Field_generate_field_key Field.generate_field_key = Field_generate_field_key
Field.render = Field_render Field.render = Field_render
Field.render_sub_field = Field_render_sub_field Field.render_sub_field = Field_render_sub_field
...@@ -835,7 +834,72 @@ class FloatWidget(TextWidget): ...@@ -835,7 +834,72 @@ class FloatWidget(TextWidget):
return TextWidgetInstance.render_view(field, value) return TextWidgetInstance.render_view(field, value)
FloatWidgetInstance = FloatWidget() FloatWidgetInstance = FloatWidget()
from Products.Formulator.StandardFields import FloatField from Products.Formulator.StandardFields import FloatField
FloatField.widget = FloatWidgetInstance FloatField.widget = FloatWidgetInstance
###################################################################
# New formulator API
#
# render method on Field must change, and have a new parameter:
# render_format
# which is call others methods ('html' call render_html)
###################################################################
# XXX Patching all Fields is not easy, as ERP5 defines his own fields.
# def Widget_render(self, field, key, value, REQUEST, render_format='html')
# # Test if method defined on class
# method_id = 'render_%' % render_format
# if hasattr(aq_self(self), method_id):
# # Try to return built-in renderer
# return getattr(self, method_id )(self, field, key, value, REQUEST)
# raise KeyError, "Rendering not defined"
# Monkey Patch
#
# Lookup all registered widgets and create render_html
# XXX This method is not a good way of patching,
# because it breaks inheritance
# XXX It's difficult to get all possible widgets, as ERP5 defines
# also his owns.
# for f in Formulator.widgets():
# if not hasattr(f, '__erp5_patched'):
# f.render_html = f.render
def Widget_render_html(self, *args, **kw):
return self.render(*args, **kw)
Widget.render_html = Widget_render_html
def Field_render_html(self, *args, **kw):
"""
render_html is used to as definition of render method in Formulator.
"""
return self.render(*args, **kw)
Field.render_html = Field_render_html
def Field_render_htmlgrid(self, value=None, REQUEST=None, key=None):
"""
render_htmlgrid returns a list of tuple (title, html render)
"""
# What about CSS ? What about description ? What about error ?
return ((self.get_value('title'),
self.render_html(value=value, REQUEST=REQUEST, key=key)),)
Field.render_htmlgrid = Field_render_htmlgrid
# Generic possible renderers
# def render_ext(self, field, key, value, REQUEST):
# return getattr(self, '%s_render' % self.__class__.__name__)
#
# def render_pt(self, field, key, value, REQUEST):
# """
# Call a page template which contains 1 macro per field
# """
# return self.field_master(self.__class__.__name__)
#
# def render_grid(self, field, key, value, REQUEST):
# return ((self.get_value('title'), self.get_value('value'),)
# # What about CSS ? What about description ? What about error ?
# # What about rendering a listbox ?
# # Grid is only valid if stucture of grid has some meaning and is
# # implemeted by listbox (ex. spreadsheet = grid)
#
# def render_pdf(self, field, key, value, REQUEST):
# return 'whatever for reportlab'
...@@ -78,8 +78,7 @@ class ParallelListWidget(Widget.MultiListWidget): ...@@ -78,8 +78,7 @@ class ParallelListWidget(Widget.MultiListWidget):
"The method to call to hash items list."), "The method to call to hash items list."),
required=0) required=0)
def render(self, field, key, value, REQUEST): def _generateSubForm(self, field, key, value, REQUEST):
item_list = filter(lambda x: x not in [ ('',''), ['',''] ], item_list = filter(lambda x: x not in [ ('',''), ['',''] ],
field.get_value('items')) field.get_value('items'))
...@@ -89,6 +88,7 @@ class ParallelListWidget(Widget.MultiListWidget): ...@@ -89,6 +88,7 @@ class ParallelListWidget(Widget.MultiListWidget):
empty_sub_field_property_dict = { empty_sub_field_property_dict = {
'key': 'default', 'key': 'default',
'title': '',
'required': 0, 'required': 0,
'field_type': 'MultiListField', 'field_type': 'MultiListField',
'item_list': [], 'item_list': [],
...@@ -114,22 +114,39 @@ class ParallelListWidget(Widget.MultiListWidget): ...@@ -114,22 +114,39 @@ class ParallelListWidget(Widget.MultiListWidget):
default_sub_field_property_dict = empty_sub_field_property_dict.copy() default_sub_field_property_dict = empty_sub_field_property_dict.copy()
default_sub_field_property_dict['item_list'] = item_list default_sub_field_property_dict['item_list'] = item_list
default_sub_field_property_dict['value'] = value_list default_sub_field_property_dict['value'] = value_list
default_sub_field_property_dict['title'] = self.title
hash_list.append(default_sub_field_property_dict) hash_list.append(default_sub_field_property_dict)
# XXX Regenerate fields each time.... # XXX Regenerate fields each time....
field.sub_form = create_aggregated_list_sub_form(hash_list) field.sub_form = create_aggregated_list_sub_form(hash_list)
return hash_list
def render(self, field, key, value, REQUEST):
hash_list = self._generateSubForm(field, key, value, REQUEST)
# Call render on each sub field
sub_field_render_list = [] sub_field_render_list = []
for sub_field_property_dict in hash_list: for sub_field_property_dict in hash_list:
sub_field_render_list.append(field.render_sub_field( sub_field_render_list.append(field.render_sub_field(
sub_field_property_dict['key'], sub_field_property_dict['key'],
sub_field_property_dict['value'], REQUEST, sub_field_property_dict['value'], REQUEST,
key=key)) key=key))
# Aggregate all renders
html_string = string.join(sub_field_render_list, html_string = string.join(sub_field_render_list,
field.get_value('view_separator')) field.get_value('view_separator'))
return html_string return html_string
def render_htmlgrid(self, field, key, value, REQUEST):
hash_list = self._generateSubForm(field, key, value, REQUEST)
# Call render on each sub field
sub_field_render_list = []
for sub_field_property_dict in hash_list:
sub_field_render_list.append((sub_field_property_dict['title'],
field.render_sub_field(
sub_field_property_dict['key'],
sub_field_property_dict['value'], REQUEST,
key=key)))
return sub_field_render_list
class ParallelListValidator(Validator.MultiSelectionValidator): class ParallelListValidator(Validator.MultiSelectionValidator):
property_names = Validator.MultiSelectionValidator.property_names property_names = Validator.MultiSelectionValidator.property_names
...@@ -169,6 +186,16 @@ class ParallelListField(ZMIField): ...@@ -169,6 +186,16 @@ class ParallelListField(ZMIField):
widget = ParallelListWidgetInstance widget = ParallelListWidgetInstance
validator = ParallelListFieldValidatorInstance validator = ParallelListFieldValidatorInstance
def render_htmlgrid(self, value=None, REQUEST=None, key=None):
"""
render_htmlgrid returns a list of tuple (title, html render)
We will use title generated by the widget.
"""
key = self.generate_field_key(key=key)
value = self._get_default(key, value, REQUEST)
html = self.widget.render_htmlgrid(self, key, value, REQUEST)
return html
def create_aggregated_list_sub_form(hash_list): def create_aggregated_list_sub_form(hash_list):
""" """
Generate ParallelListField sub field. Generate ParallelListField sub field.
...@@ -191,4 +218,3 @@ def create_aggregated_list_sub_form(hash_list): ...@@ -191,4 +218,3 @@ def create_aggregated_list_sub_form(hash_list):
sub_form.add_fields(sub_list_field_list, "sub_list") sub_form.add_fields(sub_list_field_list, "sub_list")
return sub_form return sub_form
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