Added 'query' parameter to 'absolute_url' function
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 12:28:09 +0200
changeset 107 ace92f2db5b6
parent 106 54e93dca854d
child 108 1a9d8c43dba0
Added 'query' parameter to 'absolute_url' function
src/pyams_utils/url.py
--- a/src/pyams_utils/url.py	Mon Sep 11 12:25:41 2017 +0200
+++ b/src/pyams_utils/url.py	Mon Sep 11 12:28:09 2017 +0200
@@ -21,16 +21,18 @@
 
 # import packages
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
-from pyramid.url import resource_url
+from pyramid.encode import url_quote, urlencode
+from pyramid.url import resource_url, QUERY_SAFE
 from zope.interface import Interface
 
 
-def absolute_url(context, request, view_name=None):
+def absolute_url(context, request, view_name=None, query=None):
     """Get resource absolute_url
 
     :param object context: the persistent object for which absolute URL is required
     :param request: the request on which URL is based
     :param str view_name: an optional view name to add to URL
+    :param str/dict query: an optional URL arguments string or mapping
 
     This absolute URL function is based on default Pyramid's :py:func:`resource_url` function, but
     add checks to remove some double slashes, and add control on view name when it begins with a '#'
@@ -46,6 +48,13 @@
             result += view_name
         else:
             result += '/' + view_name
+    if query:
+        qs = ''
+        if isinstance(query, str):
+            qs = '?' + url_quote(query, QUERY_SAFE)
+        elif query:
+            qs = '?' + urlencode(query, doseq=True)
+        result += qs
     return result