tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
This commit is contained in:
parent
f857a86740
commit
4d7bc8aaa1
1 changed files with 27 additions and 4 deletions
|
|
@ -26,6 +26,9 @@ except ImportError:
|
|||
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 @@ class Identities(object):
|
|||
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()})
|
||||
|
|
|
|||
Loading…
Reference in a new issue