Commit 84029c01 authored by Jérome Perrin's avatar Jérome Perrin

Listbox: calculate a mapping uid -> object once

Instead of looping through the list each time.
Also check that the list method does not return duplicate uids.
parent 386b0e6b
...@@ -2771,7 +2771,7 @@ class ListBoxValidator(Validator.Validator): ...@@ -2771,7 +2771,7 @@ class ListBoxValidator(Validator.Validator):
selected_uid_set = set(REQUEST.get('uids', ())) selected_uid_set = set(REQUEST.get('uids', ()))
#LOG('ListBox.validate: REQUEST',0,REQUEST) #LOG('ListBox.validate: REQUEST',0,REQUEST)
errors = [] errors = []
object_list = None object_dict = None
# We have two things to do in the case of temp objects, # We have two things to do in the case of temp objects,
# the first thing is to create a list with new temp objects # the first thing is to create a list with new temp objects
# then try to validate some data, and then create again # then try to validate some data, and then create again
...@@ -2780,19 +2780,27 @@ class ListBoxValidator(Validator.Validator): ...@@ -2780,19 +2780,27 @@ class ListBoxValidator(Validator.Validator):
listbox = {} listbox = {}
for uid in listbox_uids: for uid in listbox_uids:
if uid[:4] == 'new_': if uid[:4] == 'new_':
if object_list is None: if object_dict is None:
list_method = field.get_value('list_method') list_method = field.get_value('list_method')
list_method = getattr(here, list_method.method_name) list_method = getattr(here, list_method.method_name)
#LOG('ListBoxValidator', 0, 'call %s' % repr(list_method)) #LOG('ListBoxValidator', 0, 'call %s' % repr(list_method))
object_list = list_method(REQUEST=REQUEST, **params) object_list = list_method(REQUEST=REQUEST, **params)
object_dict = dict()
for obj in object_list:
o_uid = obj.getUid()
assert o_uid not in object_dict,\
"List method returned duplicate uid %s %s" % (
o_uid, object_dict)
object_dict[o_uid] = obj
row_key = uid[4:] row_key = uid[4:]
for o in object_list: try:
if o.getUid() == uid: o = object_dict[uid]
break except KeyError:
else:
# First case: dialog input to create new objects # First case: dialog input to create new objects
o = newTempBase(portal, row_key) # Arghhh - XXX acquisition problem - use portal root o = newTempBase(portal, row_key) # Arghhh - XXX acquisition problem - use portal root
o.uid = uid o.uid = uid
listbox[row_key] = row_result = {} listbox[row_key] = row_result = {}
# We first try to set a listbox corresponding to all things # We first try to set a listbox corresponding to all things
# we can validate, so that we can use the same list # we can validate, so that we can use the same list
...@@ -2811,24 +2819,34 @@ class ListBoxValidator(Validator.Validator): ...@@ -2811,24 +2819,34 @@ class ListBoxValidator(Validator.Validator):
pass pass
except KeyError: except KeyError:
pass pass
# Here we generate again the object_list with listbox the listbox we # Here we generate again the object_list with listbox the listbox we
# have just created # have just created
# XXX why ? -jerome
if listbox: if listbox:
list_method = field.get_value('list_method') list_method = field.get_value('list_method')
list_method = getattr(here, list_method.method_name) list_method = getattr(here, list_method.method_name)
REQUEST.set(field.id, listbox) REQUEST.set(field.id, listbox)
object_list = list_method(REQUEST=REQUEST, **params) object_list = list_method(REQUEST=REQUEST, **params)
object_dict = dict()
for obj in object_list:
o_uid = obj.getUid()
assert o_uid not in object_dict,\
"List method returned duplicate uid %s" % o_uid
object_dict[o_uid] = obj
for uid in listbox_uids: for uid in listbox_uids:
row_result = {} row_result = {}
if uid[:4] == 'new_': if uid[:4] == 'new_':
# First case: dialog input to create new objects
row_key = uid[4:] row_key = uid[4:]
for o in object_list: try:
if o.getUid() == uid: o = object_dict[uid]
break except KeyError:
else: # First case: dialog input to create new objects
o = newTempBase(portal, row_key) # Arghhh - XXX acquisition problem - use portal root o = newTempBase(portal, row_key) # Arghhh - XXX acquisition problem - use portal root
o.uid = uid o.uid = uid
for sql in editable_column_ids: for sql in editable_column_ids:
editable_field = editable_field_dict.get(sql.replace('.', '_')) editable_field = editable_field_dict.get(sql.replace('.', '_'))
if editable_field is not None: if editable_field is not None:
...@@ -2895,6 +2913,7 @@ class ListBoxValidator(Validator.Validator): ...@@ -2895,6 +2913,7 @@ class ListBoxValidator(Validator.Validator):
#except: #except:
else: else:
LOG("ListBox WARNING",0,"Object uid %s could not be validated" % uid) LOG("ListBox WARNING",0,"Object uid %s could not be validated" % uid)
result[row_key] = row_result result[row_key] = row_result
if select: if select:
row_result['listbox_selected'] = uid in selected_uid_set row_result['listbox_selected'] = uid in selected_uid_set
......
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