Updated form update code to avoid multiple updates on a same content when using sub-forms
--- a/src/pyams_form/form.py Tue Feb 20 11:17:52 2018 +0100
+++ b/src/pyams_form/form.py Wed Mar 07 09:47:21 2018 +0100
@@ -173,19 +173,19 @@
return result
def update_content(self, content, data):
- if self in data:
- data = data[self]
- changes = applyChanges(self, content, data)
+ changes = applyChanges(self, content, data.get(self, data))
subforms = self.subforms + self.tabforms
[subforms.extend(group.subforms) for group in self.groups]
for subform in subforms:
+ if subform.mode == DISPLAY_MODE:
+ continue
subform_update = ICustomUpdateSubForm(subform, None)
if subform_update is not None:
- updates = subform_update.update_content(content, data)
+ updates = subform_update.update_content(subform.getContent(), data.get(subform, data))
if isinstance(updates, dict):
changes.update(updates)
else:
- changes.update(applyChanges(subform, content, data))
+ changes.update(applyChanges(subform, subform.getContent(), data.get(subform, data)))
return changes
def render(self):
@@ -317,7 +317,7 @@
def createAndAdd(self, data):
registry = self.request.registry
# create object
- object = self.create(data)
+ object = self.create(data.get(self, data))
if object is not None:
registry.notify(ObjectCreatedEvent(object))
# set parent temporarily to avoid NotYet exceptions
@@ -349,7 +349,7 @@
form.updateWidgets()
form.updateActions()
form_data, form_errors = form.extractData()
- data.update(form_data)
+ data[form] = form_data
errors = errors + form_errors
if errors or self.errors:
return self.get_ajax_errors()
@@ -432,8 +432,6 @@
self.actions['submit'].addClass('btn-primary')
def applyChanges(self, data):
- if self in data:
- data = data[self]
content = self.getContent()
changes = self.update_content(content, data)
if changes:
@@ -462,11 +460,7 @@
return self.get_ajax_errors()
# update form content
try:
- changes = {}
- for form in self.forms:
- if form.mode == DISPLAY_MODE:
- continue
- changes.update(form.applyChanges(data) or {})
+ changes = self.applyChanges(data) or {}
except ValidationError as error:
# This error can occur with file-type inputs
registry = self.request.registry