changeset 1188:263fed3ce354

server (pages): on_data_post can now raise an exceptions.DataError to reload the page with a warning message (without validating data posted)
author Goffi <goffi@goffi.org>
date Sun, 26 May 2019 22:19:42 +0200
parents dab7a2b151ea
children 170802865156
files libervia/server/pages.py
diffstat 1 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/pages.py	Sun May 26 22:18:04 2019 +0200
+++ b/libervia/server/pages.py	Sun May 26 22:19:42 2019 +0200
@@ -149,9 +149,12 @@
         @param template(unicode, None): path to the template to render.
             This method is mutually exclusive with render
         @param on_data_post(callable, None): method to call when data is posted
-            None if not post is handled
+            None if data post is not handled
             on_data_post can return a string with following value:
                 - C.POST_NO_CONFIRM: confirm flag will not be set
+            on_data_post can raise following exceptions:
+                - exceptions.DataError: value is incorrect, message will be displayed
+                    as a notification
         @param on_data(callable, None): method to call when dynamic data is sent
             this method is used with Libervia's websocket mechanism
         @param on_signal(callable, None): method to call when a registered signal is
@@ -1250,6 +1253,17 @@
             .format( url=request.URLPath(), msg=failure_))
         self.pageError(request, C.HTTP_INTERNAL_ERROR)
 
+    def _on_data_post_error(self, failure_, request):
+        failure_.trap(exceptions.DataError)
+        # something is wrong with the posted data, we re-display the page with a
+        # warning notification
+        session_data = self.host.getSessionData(request, session_iface.ISATSession)
+        session_data.setPageNotification(self, failure_.value.message, C.LVL_WARNING)
+        request.setResponseCode(C.HTTP_SEE_OTHER)
+        request.setHeader("location", request.uri)
+        request.finish()
+        raise failure.Failure(exceptions.CancelError(u"Post/Redirect/Get is used"))
+
     def _on_data_post_redirect(self, ret, request):
         """called when page's on_data_post has been done successfuly
 
@@ -1312,6 +1326,7 @@
             self.pageError(request, C.HTTP_FORBIDDEN)
         d = defer.maybeDeferred(self.on_data_post, self, request)
         d.addCallback(self._on_data_post_redirect, request)
+        d.addErrback(self._on_data_post_error, request)
         return d
 
     def getPostedData(self, request, keys, multiple=False, raise_on_missing=True):