Commit f3064cd1 authored by Julien Muchembled's avatar Julien Muchembled

Allow use of ParallelListFields in ListBox cells

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31088 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 41a123b8
......@@ -65,27 +65,23 @@
# Initialise result\n
sub_field_list = []\n
\n
# Maximum size of the MultiListField\n
default_sub_field_property_dict.update({\'required\': 0,\'field_type\': \'ListField\',\'size\': 1,\'item_list\': [(\'\', \'\')] + item_list,\'value\': None})\n
title = default_sub_field_property_dict[\'title\']\n
\n
z = 0\n
# Maximum size of the MultiListField\n
default_sub_field_property_dict.update(title=\' \',\n
key=\'default:list\',\n
field_type=\'ListField\',\n
size=1,\n
item_list=[(\'\', \'\')] + item_list,\n
value=None)\n
for value in value_list:\n
new_dict = default_sub_field_property_dict.copy()\n
new_dict[\'value\'] = value\n
new_dict[\'title\'] = \' \'\n
new_dict[\'key\'] = str(z)\n
z += 1\n
sub_field_list.append(new_dict)\n
\n
request = context.REQUEST\n
\n
new_dict = default_sub_field_property_dict.copy()\n
new_dict[\'title\'] = \' \'\n
new_dict[\'key\'] = str(z)\n
sub_field_list.append(new_dict)\n
sub_field_list.append(default_sub_field_property_dict)\n
\n
if len(sub_field_list):\n
sub_field_list[0][\'title\'] = default_sub_field_property_dict[\'title\']\n
sub_field_list[0][\'title\'] = title\n
return sub_field_list\n
......@@ -130,19 +126,14 @@ return sub_field_list\n
<string>default_sub_field_property_dict</string>
<string>is_right_display</string>
<string>sub_field_list</string>
<string>_getitem_</string>
<string>title</string>
<string>_getattr_</string>
<string>None</string>
<string>z</string>
<string>_getiter_</string>
<string>value</string>
<string>new_dict</string>
<string>_write_</string>
<string>str</string>
<string>_inplacevar_</string>
<string>context</string>
<string>request</string>
<string>len</string>
<string>_getitem_</string>
</tuple>
</value>
</item>
......
2009-12-04 jm
* Allow use of ParallelListFields in ListBox cells. Fields using
Base_hashCategoryList requires ERP5Form to be up-to-date.
2009-11-18 yo
* Move localization-related preferences to General from UI.
......
1393
\ No newline at end of file
1394
\ No newline at end of file
......@@ -2317,46 +2317,24 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine):
else:
error_message = u''
if getattr(brain, 'asContext', None) is not None:
# We needed a way to pass the current line object (ie. brain)
# to the field which is being displayed. Since the
# render_view API did not permit this, we pass the line object
# as the REQUEST. But this has side effects since it breaks
# many possibilities. Therefore, the trick is to wrap
# the REQUEST into the brain. In addition, the define a
# cell property on the request itself so that forms may
# use the 'cell' value (refer to get_value method in Form.py)
cell_request = brain.asContext( REQUEST = request
, form = request.form
, cell = brain
)
if editable_field.get_value('enabled', REQUEST=cell_request):
cell_html = editable_field.render( \
value = display_value
, REQUEST = cell_request
, key = key
)
else:
cell_html = ''
else:
# If the brain does not support asContext (eg. it is None), no way
request.cell = self.getObject()
cell_request = brain
if editable_field.get_value('enabled', REQUEST=cell_request):
cell_html = editable_field.render( value = display_value
, REQUEST = cell_request
, key = key
)
else:
cell_html = ''
# We need a way to pass the current line object (ie. brain) to the
# field which is being displayed. Since the render_view API did not
# permit this, we use the 'cell' value to pass the line object.
request.set('cell', brain)
enabled = editable_field.get_value('enabled', REQUEST=request)
if enabled:
cell_html = editable_field.render(value=display_value,
REQUEST=request,
key=key)
if isinstance(cell_html, str):
cell_html = unicode(cell_html, encoding)
else:
cell_html = u''
if url is None:
html = cell_html + error_message
else:
if editable_field.get_value('editable', REQUEST=cell_request):
if enabled:
html = u'%s' % cell_html
else:
html = u'<a href="%s">%s</a>' % (url, cell_html)
......
......@@ -51,7 +51,7 @@ class ParallelListWidget(Widget.MultiListWidget,
Separation of items list is made with a Hash Script, which take
the items list in input, and return a list of dictionnaries.
Each dictionnary describes a (Multi)Listfield.
Each dictionnary describes a (Multi)ListField.
The keys are:
- key:
default: default
......@@ -134,16 +134,15 @@ class ParallelListWidget(Widget.MultiListWidget,
REQUEST.set(KEYWORD % 'default', "")
REQUEST.set(KEYWORD % 'first_item', 0)
REQUEST.set(KEYWORD % 'items', sub_field_property_dict['item_list'])
sub_widget = self.sub_widget[sub_field_property_dict['field_type']]
if sub_field_property_dict.get('editable', 1):
result = self.sub_widget[sub_field_property_dict['field_type']].render(
field,
field.generate_subfield_key(sub_field_property_dict['key'],
key=key),
result = sub_widget.render(field,
field.generate_subfield_key(
sub_field_property_dict['key'], key=key),
sub_field_property_dict['value'],
REQUEST=REQUEST)
else:
result = self.sub_widget[sub_field_property_dict['field_type']].render_view(
field,
result = sub_widget.render_view(field,
sub_field_property_dict['value'],
REQUEST)
for parameter in ('title', 'required', 'size', 'default', 'first_item',
......@@ -157,61 +156,45 @@ class ParallelListValidator(Validator.MultiSelectionValidator):
property_names = Validator.MultiSelectionValidator.property_names
def __init__(self):
"""
Generate some subvalidator used for rendering.
"""
self.sub_validator = {
sub_validator = {
'ListField': Validator.SelectionValidatorInstance,
'MultiListField': Validator.MultiSelectionValidatorInstance,
}
def validate(self, field, key, REQUEST):
result_list = []
hash_list = generateSubForm(field, field.get_value('default'), REQUEST)
is_sub_field_required = 0
hash_list = generateSubForm(field, (), REQUEST)
for sub_field_property_dict in hash_list:
try:
sub_result_list = self.validate_sub_field(
field,
field.generate_subfield_key(
sub_field_property_dict['key'],
validation=1, key=key),
REQUEST,
id = field.generate_subfield_key(sub_field_property_dict['key'],
validation=1, key=key)
sub_result_list = self.validate_sub_field(field, id, REQUEST,
sub_field_property_dict)
if not isinstance(sub_result_list, (list, tuple)):
sub_result_list = [sub_result_list]
else:
sub_result_list = list(sub_result_list)
result_list.extend(sub_result_list)
except ValidationError:
is_sub_field_required = 1
if result_list == []:
if field.get_value('required'):
self.raise_error('required_not_found', field)
else:
if is_sub_field_required:
self.raise_error('required_not_found', field)
return result_list
def validate_sub_field(self, field, id, REQUEST, sub_field_property_dict):
"""
Validates a subfield (as part of field validation).
"""
try:
for parameter in ('title', 'required', 'size'):
REQUEST.set(KEYWORD % parameter, sub_field_property_dict[parameter])
REQUEST.set(KEYWORD % 'default', "")
REQUEST.set(KEYWORD % 'items', sub_field_property_dict['item_list'])
result = self.sub_validator[sub_field_property_dict['field_type']].validate(
field, id, REQUEST)
field_type = sub_field_property_dict['field_type']
if id[-5:] == ':list':
id = id[:-5]
field_type = 'Multi' + field_type
REQUEST.set(id, [x for x in REQUEST.get(id, ()) if x != ''])
return self.sub_validator[field_type].validate(field, id, REQUEST)
finally:
for parameter in ('title', 'required', 'size', 'default', 'first_item',
'items'):
# As it doesn't seem possible to delete value in the REQUEST,
# use a marker
REQUEST.set(KEYWORD % parameter, MARKER)
return result
ParallelListWidgetInstance = ParallelListWidget()
ParallelListFieldValidatorInstance = ParallelListValidator()
......@@ -240,8 +223,8 @@ class ParallelListField(ZMIField):
return result
def generateSubForm(self, value, REQUEST):
item_list = [x for x in self.get_value('items') \
if x not in (('',''), ['',''])]
item_list = [x for x in self.get_value('items', REQUEST)
if x[0] != '' and x[1]]
value_list = value
if not isinstance(value_list, (list, tuple)):
......@@ -249,37 +232,27 @@ def generateSubForm(self, value, REQUEST):
empty_sub_field_property_dict = {
'key': 'default',
'title': self.get_value('title'),
'required': 0,
'field_type': 'MultiListField',
'item_list': [],
'value': [],
'is_right_display': 0,
'size': 5,
'editable' : self.get_value('editable', REQUEST=REQUEST)
}
for property in 'title', 'size', 'required', 'editable':
empty_sub_field_property_dict[property] = self.get_value(property, REQUEST)
hash_list = []
hash_script_id = self.get_value('hash_script_id')
if hash_script_id not in [None, '']:
script = getattr(self, hash_script_id)
script_hash_list = script(
hash_script_id = self.get_value('hash_script_id', REQUEST)
if hash_script_id:
return getattr(self, hash_script_id)(
item_list,
value_list,
default_sub_field_property_dict=empty_sub_field_property_dict,
is_right_display=0)
hash_list.extend(script_hash_list)
else:
# No hash_script founded, generate a little hash_script
# to display only a MultiListField
default_sub_field_property_dict = empty_sub_field_property_dict.copy()
default_sub_field_property_dict.update({
'item_list': item_list,
'value': value_list,
})
hash_list.append(default_sub_field_property_dict)
return hash_list
empty_sub_field_property_dict['item_list'] = item_list
empty_sub_field_property_dict['value'] = value_list
return [empty_sub_field_property_dict]
# Register get_value
from Products.ERP5Form.ProxyField import registerOriginalGetValueClassAndArgument
......
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