# HG changeset patch # User Goffi # Date 1516781532 -3600 # Node ID ac6097a66944f099d74c70ea636f7bf162eed5a2 # Parent 11ac6157fc73b629aed671b53aeabbe183dfde64 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl diff -r 11ac6157fc73 -r ac6097a66944 src/tools/common/data_objects.py --- 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()})