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