Updated JSOn responses management in AJAX forms
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 13:34:18 +0200
changeset 57 cb847dfb3e11
parent 56 93413c5a0910
child 58 6302819f15be
Updated JSOn responses management in AJAX forms
src/pyams_form/form.py
--- a/src/pyams_form/form.py	Wed Jul 12 14:17:54 2017 +0200
+++ b/src/pyams_form/form.py	Mon Sep 11 13:34:18 2017 +0200
@@ -172,6 +172,8 @@
         return result
 
     def update_content(self, content, data):
+        if self in data:
+            data = data[self]
         changes = applyChanges(self, content, data)
         subforms = self.subforms + self.tabforms
         [subforms.extend(group.subforms) for group in self.groups]
@@ -261,11 +263,11 @@
                     if hasattr(inner_error, 'widget'):
                         widget = inner_error.widget
                         if widget is not None:
-                            errors.setdefault('widgets', []).append({ 'name': widget.name,
-                                                                      'label': translate(widget.label),
-                                                                      'message': translate(inner_error.message) })
+                            errors.setdefault('widgets', []).append({'name': widget.name,
+                                                                     'label': translate(widget.label),
+                                                                     'message': translate(inner_error.message)})
                         else:
-                            errors.setdefault('messages', []).append({ 'message': translate(inner_error.message) })
+                            errors.setdefault('messages', []).append({'message': translate(inner_error.message)})
                     else:
                         errors.setdefault('messages', []).append(translate(inner_error.message))
             else:
@@ -283,12 +285,15 @@
 
     def get_ajax_output(self, changes):
         """Extract AJAX POST output"""
+        output = {}
         for form in self.subforms + self.tabforms:
             try:
-                return form.get_ajax_output(changes)
+                form_output = form.get_ajax_output(changes)
+                if form_output:
+                    output.update(form_output)
             except NotImplementedError:
                 pass
-        raise NotImplementedError
+        return output
 
 
 #
@@ -351,15 +356,19 @@
         return self.get_ajax_output(result)
 
     def get_ajax_output(self, changes):
+        output = {}
         for form in self.subforms + self.tabforms:
             try:
-                output = form.get_ajax_output(changes)
-                if output is not None:
-                    return output
+                form_output = form.get_ajax_output(changes)
+                if form_output:
+                    output.update(form_output)
             except NotImplementedError:
                 pass
-        return {'status': 'reload',
-                'location': self.nextURL()}
+        if output:
+            return output
+        else:
+            return {'status': 'reload',
+                    'location': self.nextURL()}
 
 
 @implementer(IDialog)
@@ -410,6 +419,8 @@
             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:
@@ -424,6 +435,7 @@
     """AJAX edit form"""
 
     def __call__(self):
+        # call form elements
         self.request.registry.notify(PageletCreatedEvent(self))
         data, errors = {}, ()
         for form in self.forms:
@@ -431,30 +443,46 @@
             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()
-        changes = self.applyChanges(data)
-        return self.get_ajax_output(changes)
+        # update form content
+        changes = {}
+        for form in self.forms:
+            if form.mode == DISPLAY_MODE:
+                continue
+            changes.update(form.applyChanges(data) or {})
+        # check JSON output
+        output = self.get_ajax_output(changes)
+        translate = self.request.localizer.translate
+        status = output.get('status')
+        message = output.get('message')
+        if not changes:
+            if not status:
+                output['status'] = 'info'
+            if not message:
+                output['message'] = translate(self.noChangesMessage)
+        else:
+            if not status:
+                output['status'] = status = 'success'
+            if not message:
+                if status == 'success':
+                    output['message'] = translate(self.successMessage)
+        return output
 
     def get_ajax_output(self, changes):
+        output = {}
         for form in self.subforms + self.tabforms:
+            if form.mode == DISPLAY_MODE:
+                continue
             try:
-                output = form.get_ajax_output(changes)
-                if output is not None:
-                    return output
+                form_output = form.get_ajax_output(changes)
+                if form_output:
+                    output.update(form_output)
             except NotImplementedError:
                 pass
-        if changes:
-            status = 'success'
-            message = self.successMessage
-        else:
-            status = 'info'
-            message = self.noChangesMessage
-        translate = self.request.localizer.translate
-        return {'status': status,
-                'message': translate(message)}
+        return output
 
 
 @implementer(IDialog)