comparison src/tools/common/data_objects.py @ 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 e5acc83a9d51
children 0046283a285d
comparison
equal deleted inserted replaced
2481:11ac6157fc73 2482:ac6097a66944
24 from jinja2 import Markup as safe 24 from jinja2 import Markup as safe
25 except ImportError: 25 except ImportError:
26 safe = unicode 26 safe = unicode
27 27
28 from sat.tools.common import uri as xmpp_uri 28 from sat.tools.common import uri as xmpp_uri
29 import urllib
30
31 q = lambda value: urllib.quote(value.encode('utf-8'), safe='@')
29 32
30 33
31 class BlogItem(object): 34 class BlogItem(object):
32 35
33 def __init__(self, mb_data, parent): 36 def __init__(self, mb_data, parent):
312 315
313 def __contains__(self, jid_str): 316 def __contains__(self, jid_str):
314 return jid_str in self.identities 317 return jid_str in self.identities
315 318
316 319
320 class ObjectQuoter(object):
321 """object wrapper which quote attribues (to be used in templates)"""
322
323 def __init__(self, obj):
324 self.obj = obj
325
326 def __unicode__(self):
327 return q(self.obj.__unicode__())
328
329 def __str__(self):
330 return self.__unicode__()
331
332 def __getattr__(self, name):
333 return q(self.obj.__getattr__(name))
334
335 def __getitem__(self, key):
336 return q(self.obj.__getitem__(key))
337
338
317 class OnClick(object): 339 class OnClick(object):
318 """Class to handle clickable elements targets""" 340 """Class to handle clickable elements targets"""
319 341
320 def __init__(self, url=None): 342 def __init__(self, url=None):
321 self.url = url 343 self.url = url
322 344
323 def formatUrl(self, item): 345 def formatUrl(self, *args, **kwargs):
324 """Use an item to format URL 346 """format URL using Python formatting
325 347
326 item will be formatted using "item=item" in format() 348 values will be quoted before being used
327 """ 349 """
328 return self.url.format(item=item) 350 return self.url.format(*[q(a) for a in args],
351 **{k: ObjectQuoter(v) for k,v in kwargs.iteritems()})