comparison libervia/server/pages.py @ 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 aee3d8fa679f
comparison
equal deleted inserted replaced
1187:dab7a2b151ea 1188:263fed3ce354
147 This method is mutually exclusive with template and must return a unicode 147 This method is mutually exclusive with template and must return a unicode
148 string. 148 string.
149 @param template(unicode, None): path to the template to render. 149 @param template(unicode, None): path to the template to render.
150 This method is mutually exclusive with render 150 This method is mutually exclusive with render
151 @param on_data_post(callable, None): method to call when data is posted 151 @param on_data_post(callable, None): method to call when data is posted
152 None if not post is handled 152 None if data post is not handled
153 on_data_post can return a string with following value: 153 on_data_post can return a string with following value:
154 - C.POST_NO_CONFIRM: confirm flag will not be set 154 - C.POST_NO_CONFIRM: confirm flag will not be set
155 on_data_post can raise following exceptions:
156 - exceptions.DataError: value is incorrect, message will be displayed
157 as a notification
155 @param on_data(callable, None): method to call when dynamic data is sent 158 @param on_data(callable, None): method to call when dynamic data is sent
156 this method is used with Libervia's websocket mechanism 159 this method is used with Libervia's websocket mechanism
157 @param on_signal(callable, None): method to call when a registered signal is 160 @param on_signal(callable, None): method to call when a registered signal is
158 received. This method is used with Libervia's websocket mechanism 161 received. This method is used with Libervia's websocket mechanism
159 @param url_cache(boolean): if set, result of parse_url is cached (per profile). 162 @param url_cache(boolean): if set, result of parse_url is cached (per profile).
1248 self.pageError(request, C.HTTP_FORBIDDEN) 1251 self.pageError(request, C.HTTP_FORBIDDEN)
1249 log.error(_(u"Uncatched error for HTTP request on {url}: {msg}") 1252 log.error(_(u"Uncatched error for HTTP request on {url}: {msg}")
1250 .format( url=request.URLPath(), msg=failure_)) 1253 .format( url=request.URLPath(), msg=failure_))
1251 self.pageError(request, C.HTTP_INTERNAL_ERROR) 1254 self.pageError(request, C.HTTP_INTERNAL_ERROR)
1252 1255
1256 def _on_data_post_error(self, failure_, request):
1257 failure_.trap(exceptions.DataError)
1258 # something is wrong with the posted data, we re-display the page with a
1259 # warning notification
1260 session_data = self.host.getSessionData(request, session_iface.ISATSession)
1261 session_data.setPageNotification(self, failure_.value.message, C.LVL_WARNING)
1262 request.setResponseCode(C.HTTP_SEE_OTHER)
1263 request.setHeader("location", request.uri)
1264 request.finish()
1265 raise failure.Failure(exceptions.CancelError(u"Post/Redirect/Get is used"))
1266
1253 def _on_data_post_redirect(self, ret, request): 1267 def _on_data_post_redirect(self, ret, request):
1254 """called when page's on_data_post has been done successfuly 1268 """called when page's on_data_post has been done successfuly
1255 1269
1256 This will do a Post/Redirect/Get pattern. 1270 This will do a Post/Redirect/Get pattern.
1257 this method redirect to the same page or to request.data['post_redirect_page'] 1271 this method redirect to the same page or to request.data['post_redirect_page']
1310 ) 1324 )
1311 ) 1325 )
1312 self.pageError(request, C.HTTP_FORBIDDEN) 1326 self.pageError(request, C.HTTP_FORBIDDEN)
1313 d = defer.maybeDeferred(self.on_data_post, self, request) 1327 d = defer.maybeDeferred(self.on_data_post, self, request)
1314 d.addCallback(self._on_data_post_redirect, request) 1328 d.addCallback(self._on_data_post_redirect, request)
1329 d.addErrback(self._on_data_post_error, request)
1315 return d 1330 return d
1316 1331
1317 def getPostedData(self, request, keys, multiple=False, raise_on_missing=True): 1332 def getPostedData(self, request, keys, multiple=False, raise_on_missing=True):
1318 """Get data from a POST request or from URL's query part and decode it 1333 """Get data from a POST request or from URL's query part and decode it
1319 1334