comparison src/server/blog.py @ 828:0c824ebe9d87

server (blog): implemented tag/category filtering: - the tag=[quoted tag] query is used to use the filtering - MAM data use request.mam_extra instead of request.extra_dict to avoid using it with item_ids - getDefaultQueryData function is used to keep query data (link MAM category filtering) between links - filtering is help when displaying a blog post, this way it's easy to navigate between thematic posts - filtering can be removed by clicking on user avatar/nick (with default theme)
author Goffi <goffi@goffi.org>
date Fri, 08 Jan 2016 19:58:08 +0100
parents f1fc7245b910
children c1000ea40e6c
comparison
equal deleted inserted replaced
827:f1fc7245b910 828:0c824ebe9d87
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 46
47 def getDefaultQueryData(request):
48 """Return query data which must be present in all links
49
50 @param request(twisted.web.http.Request): request instance comming from render
51 @return (dict): a dict with values as expected by urllib.urlencode
52 """
53 default_query_data = {}
54 try:
55 default_query_data['tag'] = request.extra_dict['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)].encode('utf-8')
56 except KeyError:
57 pass
58 return default_query_data
59
60
47 def _quote(value): 61 def _quote(value):
48 """Quote a value for use in url 62 """Quote a value for use in url
49 63
50 @param value(unicode): value to quote 64 @param value(unicode): value to quote
51 @return (str): quoted value 65 @return (str): quoted value
168 # max_items = int(request.extra_dict['rsm_max']) # FIXME 182 # max_items = int(request.extra_dict['rsm_max']) # FIXME
169 max_items = 0 183 max_items = 0
170 # TODO: use max_items only when RSM is not available 184 # TODO: use max_items only when RSM is not available
171 185
172 if request.atom: 186 if request.atom:
187 request.extra_dict.update(request.mam_extra)
173 self.host.bridge.mbGetAtom(pub_jid.userhost(), '', max_items, item_ids, 188 self.host.bridge.mbGetAtom(pub_jid.userhost(), '', max_items, item_ids,
174 request.extra_dict, C.SERVICE_PROFILE, 189 request.extra_dict, C.SERVICE_PROFILE,
175 lambda feed: self.renderAtomFeed(feed, request), 190 lambda feed: self.renderAtomFeed(feed, request),
176 lambda failure: self.renderError(failure, request, pub_jid)) 191 lambda failure: self.renderError(failure, request, pub_jid))
177 elif request.item_id: 192 elif request.item_id:
193 # we can't merge mam_extra now because we'll use item_ids
178 self.getItemById(pub_jid, request.item_id, request.extra_dict, 194 self.getItemById(pub_jid, request.item_id, request.extra_dict,
179 request.extra_comments_dict, request, profile) 195 request.extra_comments_dict, request, profile)
180 else: 196 else:
197 request.extra_dict.update(request.mam_extra)
181 self.getItems(pub_jid, max_items, request.extra_dict, 198 self.getItems(pub_jid, max_items, request.extra_dict,
182 request.extra_comments_dict, request, profile) 199 request.extra_comments_dict, request, profile)
183 200
184 ## URL parsing 201 ## URL parsing
185 202
201 218
202 self.parseURLParamsRSM(request) 219 self.parseURLParamsRSM(request)
203 # XXX: request.display_single is True when only one blog post is visible 220 # XXX: request.display_single is True when only one blog post is visible
204 request.display_single = (request.item_id is not None) or int(request.extra_dict['rsm_max']) == 1 221 request.display_single = (request.item_id is not None) or int(request.extra_dict['rsm_max']) == 1
205 self.parseURLParamsCommentsRSM(request) 222 self.parseURLParamsCommentsRSM(request)
223 self.parseURLParamsMAM(request)
206 224
207 def parseURLParamsRSM(self, request): 225 def parseURLParamsRSM(self, request):
208 """Parse RSM request data from the URL parameters for main items 226 """Parse RSM request data from the URL parameters for main items
209 227
210 fill request.extra_dict accordingly 228 fill request.extra_dict accordingly
248 except (ValueError, KeyError): 266 except (ValueError, KeyError):
249 request.extra_comments_dict['rsm_max'] = unicode(C.STATIC_RSM_MAX_COMMENTS_DEFAULT) 267 request.extra_comments_dict['rsm_max'] = unicode(C.STATIC_RSM_MAX_COMMENTS_DEFAULT)
250 else: 268 else:
251 request.extra_comments_dict['rsm_max'] = "0" 269 request.extra_comments_dict['rsm_max'] = "0"
252 270
271 def parseURLParamsMAM(self, request):
272 """Parse MAM request data from the URL parameters for main items
273
274 fill request.extra_dict accordingly
275 @param request: HTTP request
276 """
277 # XXX: we use a separate dict for MAM as the filters are not used
278 # when display_single is set (because it then use item_ids which
279 # can't be used with MAM), but it is still used in this case
280 # for navigation links.
281 request.mam_extra = {}
282 try:
283 request.mam_extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = request.args['tag'][0]
284 except KeyError:
285 pass
286
253 ## Items retrieval 287 ## Items retrieval
254 288
255 def getItemById(self, pub_jid, item_id, extra_dict, extra_comments_dict, request, profile): 289 def getItemById(self, pub_jid, item_id, extra_dict, extra_comments_dict, request, profile):
256 """ 290 """
257 291
279 pass 313 pass
280 314
281 metadata['rsm_first'] = metadata['rsm_last'] = item["id"] 315 metadata['rsm_first'] = metadata['rsm_last'] = item["id"]
282 316
283 def gotComments(comments): 317 def gotComments(comments):
318 # at this point we can merge mam dict
319 request.extra_dict.update(request.mam_extra)
284 # build the items as self.getItems would do it (and as self.renderHTML expects them to be) 320 # build the items as self.getItems would do it (and as self.renderHTML expects them to be)
285 comments = [(item['comments_service'], item['comments_node'], "", comments[0], comments[1])] 321 comments = [(item['comments_service'], item['comments_node'], "", comments[0], comments[1])]
286 self.renderHTML([(item, comments)], metadata, request, pub_jid, profile) 322 self.renderHTML([(item, comments)], metadata, request, pub_jid, profile)
287 323
288 # get the comments 324 # get the comments
452 @param items (list): list of items 488 @param items (list): list of items
453 @param rsm_data (dict): rsm data 489 @param rsm_data (dict): rsm data
454 @param base_url (unicode): the base URL for this user's blog 490 @param base_url (unicode): the base URL for this user's blog
455 @return: dict 491 @return: dict
456 """ 492 """
493 # query data which must be present in all links
494 default_query_data = getDefaultQueryData(request)
495
496 # which links we need to display
457 if request.display_single: 497 if request.display_single:
458 links = ('later_message', 'older_message') 498 links = ('later_message', 'older_message')
459 # key must exist when using the template 499 # key must exist when using the template
460 self.later_messages = self.older_messages = '' 500 self.later_messages = self.older_messages = ''
461 else: 501 else:
462 links = ('later_messages', 'older_messages') 502 links = ('later_messages', 'older_messages')
463 self.later_message = self.older_message = '' 503 self.later_message = self.older_message = ''
464 504
505 # now we set the links according to RSM
465 for key in links: 506 for key in links:
466 query_data = {} 507 query_data = default_query_data.copy()
467 508
468 if key.startswith('later_message'): 509 if key.startswith('later_message'):
469 try: 510 try:
470 index = int(rsm_data['rsm_index']) 511 index = int(rsm_data['rsm_index'])
471 except (KeyError, ValueError): 512 except (KeyError, ValueError):
544 if is_comment: 585 if is_comment:
545 self.author = (_("from {}").format(entry['author'])) 586 self.author = (_("from {}").format(entry['author']))
546 else: 587 else:
547 self.author = '&nbsp;' 588 self.author = '&nbsp;'
548 self.url = "{}/{}".format(base_url, _quote(entry['id'])) 589 self.url = "{}/{}".format(base_url, _quote(entry['id']))
590 query_data = getDefaultQueryData(request)
591 if query_data:
592 self.url += '?{}'.format(urllib.urlencode(query_data))
549 self.title = self.getText(entry, 'title') 593 self.title = self.getText(entry, 'title')
550 self.tags = list(common.dict2iter('tag', entry)) 594 self.tags = list(common.dict2iter('tag', entry))
551 595
552 count_text = lambda count: D_('comments') if count > 1 else D_('comment') 596 count_text = lambda count: D_('comments') if count > 1 else D_('comment')
553 597