Mercurial > libervia-web
diff src/server/server.py @ 972:c4e58c4dba75
server: getURL + minor improvments:
- new getURL method allow to retrieve URL (same as "url" property) with possibility to specify arguments which will be quoted and added to the path
- added quote function which encode unicode value and do a urllib.quote_plus
- use of new getURL in _on_data_post_redirect, this simplify a bit the code
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 10 Nov 2017 11:10:15 +0100 |
parents | 4d37b23777c3 |
children | 4aa38c49bff7 |
line wrap: on
line diff
--- a/src/server/server.py Thu Nov 09 08:02:49 2017 +0100 +++ b/src/server/server.py Fri Nov 10 11:10:15 2017 +0100 @@ -68,6 +68,9 @@ DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None +def quote(value): + """shortcut to quote an unicode value for URL""" + return urllib.quote_plus(value.encode('utf-8')).replace('%40','@') class LiberviaSession(server.Session): @@ -158,7 +161,7 @@ except (IndexError, KeyError): raise NotImplementedError(u"only item for PubSub URI is handled for the moment for url_redirections_dict") location = "/blog/{profile}/{item}".format( - profile=urllib.quote(options['url_redirections_profile'].encode('utf-8')), + profile=quote(options['url_redirections_profile']), item = urllib.quote_plus(item), ).decode('utf-8') request_data = self._getRequestData(location) @@ -1499,6 +1502,14 @@ root_url = self.getPageByName(page_name).url, redirect_url=urllib.quote_plus(request.uri) if url is None else url.encode('utf-8')) + def getURL(self, *args): + """retrieve URL of the page set arguments + + *args(list[unicode]): argument to add to the URL as path elements + """ + url_args = [quote(a) for a in args] + return os.path.join(self.url, *url_args) + def getChildWithDefault(self, path, request): # we handle children ourselves raise exceptions.InternalError(u"this method should not be used with LiberviaPage") @@ -1680,12 +1691,11 @@ 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:]] + redirect_page_args = redirect_page_data[1:] + redirect_uri = redirect_page.getURL(*redirect_page_args) else: redirect_page = redirect_page_data - redirect_page_args = [] - redirect_uri = os.path.join(redirect_page.url, *redirect_page_args) + redirect_uri = redirect_page.url else: redirect_page = self redirect_uri = request.uri