changeset 2482:ac6097a66944

tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
author Goffi <goffi@goffi.org>
date Wed, 24 Jan 2018 09:12:12 +0100
parents 11ac6157fc73
children 0046283a285d
files src/tools/common/data_objects.py
diffstat 1 files changed, 27 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/common/data_objects.py	Wed Jan 24 09:10:12 2018 +0100
+++ b/src/tools/common/data_objects.py	Wed Jan 24 09:12:12 2018 +0100
@@ -26,6 +26,9 @@
     safe = unicode
 
 from sat.tools.common import uri as xmpp_uri
+import urllib
+
+q = lambda value: urllib.quote(value.encode('utf-8'), safe='@')
 
 
 class BlogItem(object):
@@ -314,15 +317,35 @@
         return jid_str in self.identities
 
 
+class ObjectQuoter(object):
+    """object wrapper which quote attribues (to be used in templates)"""
+
+    def __init__(self, obj):
+        self.obj = obj
+
+    def __unicode__(self):
+        return q(self.obj.__unicode__())
+
+    def __str__(self):
+        return self.__unicode__()
+
+    def __getattr__(self, name):
+        return q(self.obj.__getattr__(name))
+
+    def __getitem__(self, key):
+        return q(self.obj.__getitem__(key))
+
+
 class OnClick(object):
     """Class to handle clickable elements targets"""
 
     def __init__(self, url=None):
         self.url = url
 
-    def formatUrl(self, item):
-        """Use an item to format URL
+    def formatUrl(self, *args, **kwargs):
+        """format URL using Python formatting
 
-        item will be formatted using "item=item" in format()
+        values will be quoted before being used
         """
-        return self.url.format(item=item)
+        return self.url.format(*[q(a) for a in args],
+            **{k: ObjectQuoter(v) for k,v in kwargs.iteritems()})