changeset 968:4d37b23777c3

pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page: instead of giving a full uri, a page is now given when we want to redirect to something else that current page on data post. This make the redirection more simple and intuive A tuple can be used to specify arguments to put in URL.
author Goffi <goffi@goffi.org>
date Thu, 09 Nov 2017 08:02:47 +0100
parents 4d1dcf97740f
children 8943ee1cf3d7
files src/pages/tickets/new/page_meta.py src/server/server.py
diffstat 2 files changed, 25 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/pages/tickets/new/page_meta.py	Sun Nov 05 22:11:23 2017 +0100
+++ b/src/pages/tickets/new/page_meta.py	Thu Nov 09 08:02:47 2017 +0100
@@ -6,7 +6,6 @@
 from sat.tools.common import template_xmlui
 from sat.core.log import getLogger
 log = getLogger('pages/ticket')
-import urllib
 """ticket handling pages"""
 
 access = C.PAGES_ACCESS_PROFILE
@@ -34,7 +33,6 @@
     profile = self.getProfile(request)
     yield self.host.bridge.ticketsSet(service.full(), node, posted_data, u'', u'', {}, profile)
     # we don't want to redirect to creation page on success, but to tickets list
-    data['post_redirect_uri'] = u'{list_url}/{service}/{node}'.format(
-        service = urllib.quote_plus(service.full().encode('utf-8')),
-        node = urllib.quote_plus(node.encode('utf-8')) if node else u'@',
-        list_url = self.getPageByName(u'tickets_list').url)
+    data['post_redirect_page'] = (self.getPageByName(u'tickets_list'),
+                                  service.full(),
+                                  node or u'@')
--- a/src/server/server.py	Sun Nov 05 22:11:23 2017 +0100
+++ b/src/server/server.py	Thu Nov 09 08:02:47 2017 +0100
@@ -1658,11 +1658,13 @@
         self.pageError(request, C.HTTP_INTERNAL_ERROR)
 
     def _on_data_post_redirect(self, ret, request):
-        """called when page's on_data_post has been called successfuly
+        """called when page's on_data_post has been done successfuly
 
-        this method redirect to the same page or to request.data['post_redirect_uri']
-        if it is set, using Post/Redirect/Get pattern.
-        HTTP status code "See Other" (303) is the recommanded code in this case
+        This will do a Post/Redirect/Get pattern.
+        this method redirect to the same page or to request.data['post_redirect_page']
+        post_redirect_page can be either a page or a tuple with page as first item, then a list of unicode arguments to append to the url.
+        if post_redirect_page is not used, initial request.uri (i.e. the same page as where the data have been posted) will be used for redirection.
+        HTTP status code "See Other" (303) is used as it is the recommanded code in this case.
         @param ret(None, unicode, iterable): on_data_post return value
             see LiberviaPage.__init__ on_data_post docstring
         """
@@ -1674,10 +1676,24 @@
             ret = tuple(ret)
             raise NotImplementedError(_(u'iterable in on_data_post return value is not used yet'))
         session_data = self.host.getSessionData(request, session_iface.ISATSession)
+        if 'post_redirect_page' in request.data:
+            redirect_page_data = request.data['post_redirect_page']
+            if isinstance(redirect_page_data, tuple):
+                redirect_page = redirect_page_data[0]
+                redirect_page_args = [urllib.quote_plus(a.encode('utf-8')).replace('%40','@')
+                                      for a in redirect_page_data[1:]]
+            else:
+                redirect_page = redirect_page_data
+                redirect_page_args = []
+            redirect_uri = os.path.join(redirect_page.url, *redirect_page_args)
+        else:
+            redirect_page = self
+            redirect_uri = request.uri
+
         if not C.POST_NO_CONFIRM in ret:
-            session_data.setPageFlag(self, C.FLAG_CONFIRM)
+            session_data.setPageFlag(redirect_page, C.FLAG_CONFIRM)
         request.setResponseCode(C.HTTP_SEE_OTHER)
-        request.setHeader("location", request.data.get('post_redirect_uri', request.uri))
+        request.setHeader("location", redirect_uri)
         request.finish()
         raise failure.Failure(exceptions.CancelError(u'Post/Redirect/Get is used'))