comparison src/server/blog.py @ 827:f1fc7245b910

server (blog): moved _quote and _unquote to module functions, and use them in BlogMessage.url
author Goffi <goffi@goffi.org>
date Fri, 08 Jan 2016 19:53:51 +0100
parents 70939916dc80
children 0c824ebe9d87
comparison
equal deleted inserted replaced
826:70939916dc80 827:f1fc7245b910
41 41
42 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION) 42 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION)
43 43
44 # TODO: chech disco features and use max_items when RSM is not available 44 # TODO: chech disco features and use max_items when RSM is not available
45 45
46
47 def _quote(value):
48 """Quote a value for use in url
49
50 @param value(unicode): value to quote
51 @return (str): quoted value
52 """
53 return urllib.quote(value.encode('utf-8'), '')
54
55
56 def _unquote(quoted_value):
57 """Unquote a value coming from url
58
59 @param unquote_value(str): value to unquote
60 @return (unicode): unquoted value
61 """
62 return urllib.unquote(quoted_value).decode('utf-8')
63
64
46 class TemplateProcessor(object): 65 class TemplateProcessor(object):
47 66
48 THEME = 'default' 67 THEME = 'default'
49 68
50 def __init__(self, host): 69 def __init__(self, host):
76 Resource.__init__(self) 95 Resource.__init__(self)
77 TemplateProcessor.__init__(self, host) 96 TemplateProcessor.__init__(self, host)
78 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedHandler) 97 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedHandler)
79 self.avatars_cache = {} 98 self.avatars_cache = {}
80 self.waiting_deferreds = {} 99 self.waiting_deferreds = {}
81
82 def _quote(self, value):
83 """Quote a value for use in url
84
85 @param value(unicode): value to quote
86 @return (str): quoted value
87 """
88 return urllib.quote(value.encode('utf-8'), '')
89
90 def _unquote(self, quoted_value):
91 """Unquote a value coming from url
92
93 @param unquote_value(str): value to unquote
94 @return (unicode): unquoted value
95 """
96 return urllib.unquote(quoted_value).decode('utf-8')
97 100
98 def entityDataUpdatedHandler(self, entity_s, key, value, dummy): 101 def entityDataUpdatedHandler(self, entity_s, key, value, dummy):
99 """Retrieve the avatar we've been waiting for and fires the callback. 102 """Retrieve the avatar we've been waiting for and fires the callback.
100 103
101 @param entity_s (str): JID of the contact 104 @param entity_s (str): JID of the contact
133 136
134 def render_GET(self, request): 137 def render_GET(self, request):
135 if not request.postpath or len(request.postpath) > 2: 138 if not request.postpath or len(request.postpath) > 2:
136 return self.useTemplate(request, "static_blog_error", {'message': "You must indicate a nickname"}) 139 return self.useTemplate(request, "static_blog_error", {'message': "You must indicate a nickname"})
137 140
138 prof_requested = self._unquote(request.postpath[0]) 141 prof_requested = _unquote(request.postpath[0])
139 142
140 try: 143 try:
141 prof_found = self.host.bridge.getProfileName(prof_requested) 144 prof_found = self.host.bridge.getProfileName(prof_requested)
142 except DBusException: 145 except DBusException:
143 prof_found = None 146 prof_found = None
189 if request.postpath[1] == 'atom.xml': # return the atom feed 192 if request.postpath[1] == 'atom.xml': # return the atom feed
190 request.atom = True 193 request.atom = True
191 request.item_id = None 194 request.item_id = None
192 else: 195 else:
193 request.atom = False 196 request.atom = False
194 request.item_id = request.postpath[1] 197 request.item_id = _unquote(request.postpath[1])
195 else: 198 else:
196 request.item_id = None 199 request.item_id = None
197 request.atom = False 200 request.atom = False
198 201
199 self.parseURLParamsRSM(request) 202 self.parseURLParamsRSM(request)
540 543
541 if is_comment: 544 if is_comment:
542 self.author = (_("from {}").format(entry['author'])) 545 self.author = (_("from {}").format(entry['author']))
543 else: 546 else:
544 self.author = '&nbsp;' 547 self.author = '&nbsp;'
545 self.url = "{}/{}".format(base_url, entry['id'].encode('utf-8')) 548 self.url = "{}/{}".format(base_url, _quote(entry['id']))
546 self.title = self.getText(entry, 'title') 549 self.title = self.getText(entry, 'title')
547 self.tags = list(common.dict2iter('tag', entry)) 550 self.tags = list(common.dict2iter('tag', entry))
548 551
549 count_text = lambda count: D_('comments') if count > 1 else D_('comment') 552 count_text = lambda count: D_('comments') if count > 1 else D_('comment')
550 553