comparison src/server/blog.py @ 708:e9a6cbb924e6

server_side: fixes static blog navigation links
author souliane <souliane@mailoo.org>
date Mon, 08 Jun 2015 08:51:41 +0200
parents 531eacb82e9f
children bf562fb9c273
comparison
equal deleted inserted replaced
707:dfe4b0291daa 708:e9a6cbb924e6
58 'images': os.path.join(theme_url, 'images'), 58 'images': os.path.join(theme_url, 'images'),
59 'styles': os.path.join(theme_url, 'styles'), 59 'styles': os.path.join(theme_url, 'styles'),
60 } 60 }
61 if data: 61 if data:
62 data_.update(data) 62 data_.update(data)
63 return (getattr(theme, tpl.upper()).format(**data_)).encode('utf-8') 63 return getattr(theme, tpl.upper()).encode('utf-8').format(**data_)
64 64
65 65
66 class MicroBlog(Resource, TemplateProcessor): 66 class MicroBlog(Resource, TemplateProcessor):
67 isLeaf = True 67 isLeaf = True
68 68
147 return server.NOT_DONE_YET 147 return server.NOT_DONE_YET
148 148
149 def gotJID(self, pub_jid_s, request, profile): 149 def gotJID(self, pub_jid_s, request, profile):
150 pub_jid = JID(pub_jid_s) 150 pub_jid = JID(pub_jid_s)
151 d = defer.Deferred() 151 d = defer.Deferred()
152 item_id = None 152
153 atom = None 153 self.parseURLParams(request)
154 154 item_id, rsm_ = request.item_id, request.rsm_data
155 if len(request.postpath) > 1:
156 if request.postpath[1] == 'atom.xml': # return the atom feed
157 atom = True
158 else:
159 try: # check if the given path is a valid UUID
160 uuid.UUID(request.postpath[1])
161 item_id = request.postpath[1]
162 except ValueError:
163 pass
164
165 rsm_ = self.parseURLParams(request, item_id)
166 max_items = int(rsm_['max_']) 155 max_items = int(rsm_['max_'])
167 156
168 if atom is not None: 157 if request.atom:
169 d.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, profile], None) 158 d.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, profile], None)
170 self.host.bridge.getGroupBlogsAtom(pub_jid.userhost(), rsm_, C.SERVICE_PROFILE, d.callback, d.errback) 159 self.host.bridge.getGroupBlogsAtom(pub_jid.userhost(), rsm_, C.SERVICE_PROFILE, d.callback, d.errback)
171 return 160 return
172 161
173 d.addCallbacks(self.render_html_blog, self.render_error_blog, [request, profile], None, [request, profile], None) 162 d.addCallbacks(self.render_html_blog, self.render_error_blog, [request, profile], None, [request, profile], None)
180 if max_items == 1: # display one message and its comments 169 if max_items == 1: # display one message and its comments
181 self.host.bridge.getGroupBlogsWithComments(pub_jid.userhost(), [], rsm_, C.RSM_MAX_COMMENTS, C.SERVICE_PROFILE, d.callback, d.errback) 170 self.host.bridge.getGroupBlogsWithComments(pub_jid.userhost(), [], rsm_, C.RSM_MAX_COMMENTS, C.SERVICE_PROFILE, d.callback, d.errback)
182 else: # display the last messages, count their comments 171 else: # display the last messages, count their comments
183 self.host.bridge.getGroupBlogs(pub_jid.userhost(), [], rsm_, True, C.SERVICE_PROFILE, d.callback, d.errback) 172 self.host.bridge.getGroupBlogs(pub_jid.userhost(), [], rsm_, True, C.SERVICE_PROFILE, d.callback, d.errback)
184 173
185 def parseURLParams(self, request, item_id): 174 def parseURLParams(self, request):
186 # retrieve RSM request data from URL parameters 175 """Parse the request URL parameters.
176
177 @param request: HTTP request
178 """
179 request.item_id = None
180 request.atom = False
181
182 if len(request.postpath) > 1:
183 if request.postpath[1] == 'atom.xml': # return the atom feed
184 request.atom = True
185 else:
186 try: # check if the given path is a valid UUID
187 uuid.UUID(request.postpath[1])
188 request.item_id = request.postpath[1]
189 except ValueError:
190 pass
191
192 self.parseURLParamsRSM(request)
193 request.display_single = (request.item_id is not None) or int(request.rsm_data['max_']) == 1
194
195 def parseURLParamsRSM(self, request):
196 """Parse RSM request data from the URL parameters.
197
198 @param request: HTTP request
199 """
187 rsm_ = {} 200 rsm_ = {}
188 try: 201 try:
189 rsm_['max_'] = request.args['max'][0] 202 rsm_['max_'] = request.args['max'][0]
190 except (ValueError, KeyError): 203 except (ValueError, KeyError):
191 rsm_['max_'] = unicode(C.RSM_MAX_ITEMS if item_id else C.RSM_MAX_COMMENTS) 204 rsm_['max_'] = unicode(C.RSM_MAX_ITEMS if request.item_id else C.RSM_MAX_COMMENTS)
192 try: 205 try:
193 rsm_['index'] = request.args['index'][0] 206 rsm_['index'] = request.args['index'][0]
194 except (ValueError, KeyError): 207 except (ValueError, KeyError):
195 try: 208 try:
196 rsm_['before'] = request.args['before'][0] 209 rsm_['before'] = request.args['before'][0]
197 except KeyError: 210 except KeyError:
198 try: 211 try:
199 rsm_['after'] = request.args['after'][0] 212 rsm_['after'] = request.args['after'][0]
200 except KeyError: 213 except KeyError:
201 pass 214 pass
202 return rsm_ 215 request.rsm_data = rsm_
203 216
204 def render_html_blog(self, mblog_data, request, profile): 217 def render_html_blog(self, mblog_data, request, profile):
205 """Retrieve the user parameters before actually rendering the static blog 218 """Retrieve the user parameters before actually rendering the static blog
206 219
207 @param mblog_data (list): couple (list, dict) with: 220 @param mblog_data (list): couple (list, dict) with:
298 @return: dict 311 @return: dict
299 """ 312 """
300 data = {} 313 data = {}
301 for key in ('later_message', 'later_messages', 'older_message', 'older_messages'): 314 for key in ('later_message', 'later_messages', 'older_message', 'older_messages'):
302 count = int(rsm_data.get('count', 0)) 315 count = int(rsm_data.get('count', 0))
303 display_single = len(mblog_data) == 1
304 data[key] = '' # key must exist when using the template 316 data[key] = '' # key must exist when using the template
305 if count <= 0 or (display_single == key.endswith('s')): 317 if count <= 0 or (request.display_single == key.endswith('s')):
306 continue 318 continue
307 319
308 index = int(rsm_data['index']) 320 index = int(rsm_data['index'])
309 321
310 link_data = {'base_url': base_url, 'suffix': ''} 322 link_data = {'base_url': base_url, 'suffix': ''}
318 if index + len(mblog_data) >= count: 330 if index + len(mblog_data) >= count:
319 continue 331 continue
320 link_data['item_id'] = rsm_data['last'] 332 link_data['item_id'] = rsm_data['last']
321 link_data['post_arg'] = 'after' 333 link_data['post_arg'] = 'after'
322 334
323 if display_single: 335 if request.display_single:
324 link_data['suffix'] = '&max=1' 336 link_data['suffix'] = '&max=1'
325 337
326 link = "%(base_url)s?%(post_arg)s=%(item_id)s%(suffix)s" % link_data 338 link = "%(base_url)s?%(post_arg)s=%(item_id)s%(suffix)s" % link_data
327 339
328 link_data = {'link': link, 'class': key, 'text': key.replace('_', ' ')} 340 link_data = {'link': link, 'class': key, 'text': key.replace('_', ' ')}
377 389
378 if is_comment: 390 if is_comment:
379 author = (_("from %s") % entry['author']).encode('utf-8') 391 author = (_("from %s") % entry['author']).encode('utf-8')
380 else: 392 else:
381 author = '&nbsp;' 393 author = '&nbsp;'
382 message_link = ("%s/%s" % (self.base_url, entry['id'])).encode('utf-8') 394 message_link = (u"%s/%s" % (self.base_url, entry['id'])).encode('utf-8')
383 395
384 count_text = lambda count: D_('comments') if count > 1 else D_('comment') 396 count_text = lambda count: D_('comments') if count > 1 else D_('comment')
385 397
386 comments_count = int(entry['comments_count']) 398 comments_count = int(entry['comments_count'])
387 delta = comments_count - len(comments) 399 delta = comments_count - len(comments)
388 if len(self.mblog_data) == 1 and delta > 0: 400 if self.request.display_single and delta > 0:
389 data['comments_link'] = ("%s?max=%s" % (message_link, entry['comments_count'])) 401 data['comments_link'] = ("%s?max=%s" % (message_link, entry['comments_count']))
390 data['previous_comments'] = D_("Show %(count)d previous %(comments)s") % \ 402 data['previous_comments'] = D_("Show %(count)d previous %(comments)s") % \
391 {'count': delta, 'comments': count_text(delta)} 403 {'count': delta, 'comments': count_text(delta)}
392 404
393 data.update({'comments_count': comments_count, 405 data.update({'comments_count': comments_count,