comparison src/server/blog.py @ 854:0dfb76b13115

server (blog): fixes: - fixed BlogMessage comments arguments when comments is None - added a couple of forgotten "u" for unicode strings - added a workaround for an issue with empty <div /> element. This workaround doesn't cover all cases
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:22:30 +0100
parents 330db23d4a44
children de17f7313cbe
comparison
equal deleted inserted replaced
853:fe43cf3ed7d8 854:0dfb76b13115
36 import urllib 36 import urllib
37 37
38 from libervia.server.html_tools import sanitizeHtml, convertNewLinesToXHTML 38 from libervia.server.html_tools import sanitizeHtml, convertNewLinesToXHTML
39 from libervia.server.constants import Const as C 39 from libervia.server.constants import Const as C
40 40
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) 41 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION)
42 re_strip_empty_div = re.compile(r"<div ?/>|<div> *?</div>")
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): 47 def getDefaultQueryData(request):
71 """Unquote a value coming from url 71 """Unquote a value coming from url
72 72
73 @param unquote_value(str): value to unquote 73 @param unquote_value(str): value to unquote
74 @return (unicode): unquoted value 74 @return (unicode): unquoted value
75 """ 75 """
76 assert not isinstance(quoted_value, unicode)
76 return urllib.unquote(quoted_value).decode('utf-8') 77 return urllib.unquote(quoted_value).decode('utf-8')
77 78
78 79
79 class TemplateProcessor(object): 80 class TemplateProcessor(object):
80 81
570 @param base_url (unicode): the base URL 571 @param base_url (unicode): the base URL
571 @param entry(dict): item microblog data 572 @param entry(dict): item microblog data
572 @param comments(list[dict]): list of microblog data 573 @param comments(list[dict]): list of microblog data
573 @param comments_count (int): total number of comments 574 @param comments_count (int): total number of comments
574 """ 575 """
576 if comments is None:
577 comments = []
575 timestamp = float(entry.get('published', 0)) 578 timestamp = float(entry.get('published', 0))
576 579
577 # FIXME: for now we assume that the comments' depth is only 1 580 # FIXME: for now we assume that the comments' depth is only 1
578 is_comment = not entry.get('comments', False) 581 is_comment = not entry.get('comments', False)
579 582
581 self.type = "comment" if is_comment else "main_item" 584 self.type = "comment" if is_comment else "main_item"
582 self.style = 'mblog_comment' if is_comment else '' 585 self.style = 'mblog_comment' if is_comment else ''
583 self.content = self.getText(entry, 'content') 586 self.content = self.getText(entry, 'content')
584 587
585 if is_comment: 588 if is_comment:
586 self.author = (_("from {}").format(entry['author'])) 589 self.author = (_(u"from {}").format(entry['author']))
587 else: 590 else:
588 self.author = '&nbsp;' 591 self.author = '&nbsp;'
589 self.url = "{}/{}".format(base_url, _quote(entry['id'])) 592 self.url = "{}/{}".format(base_url, _quote(entry['id']))
590 query_data = getDefaultQueryData(request) 593 query_data = getDefaultQueryData(request)
591 if query_data: 594 if query_data:
592 self.url += '?{}'.format(urllib.urlencode(query_data)) 595 self.url += '?{}'.format(urllib.urlencode(query_data))
593 self.title = self.getText(entry, 'title') 596 self.title = self.getText(entry, 'title')
594 self.tags = [sanitizeHtml(tag) for tag in common.dict2iter('tag', entry)] 597 self.tags = [sanitizeHtml(tag) for tag in common.dict2iter('tag', entry)]
595 598
596 count_text = lambda count: D_('comments') if count > 1 else D_('comment') 599 count_text = lambda count: D_(u'comments') if count > 1 else D_(u'comment')
597 600
598 self.comments_text = u"{} {}".format(comments_count, count_text(comments_count)) 601 self.comments_text = u"{} {}".format(comments_count, count_text(comments_count))
599 602
600 delta = comments_count - len(comments) 603 delta = comments_count - len(comments)
601 if request.display_single and delta > 0: 604 if request.display_single and delta > 0:
602 prev_url = "{}?{}".format(self.url, urllib.urlencode({'comments_max': comments_count})) 605 prev_url = "{}?{}".format(self.url, urllib.urlencode({'comments_max': comments_count}))
603 prev_text = D_("show {count} previous {comments}").format( 606 prev_text = D_(u"show {count} previous {comments}").format(
604 count = delta, comments = count_text(delta)) 607 count = delta, comments = count_text(delta))
605 self.all_comments_link = BlogLink(prev_url, "comments_link", prev_text) 608 self.all_comments_link = BlogLink(prev_url, "comments_link", prev_text)
606 609
607 if comments: 610 if comments:
608 self.comments = [BlogMessage(request, base_url, comment) for comment in comments] 611 self.comments = [BlogMessage(request, base_url, comment) for comment in comments]
609 612
610 def getText(self, entry, key): 613 def getText(self, entry, key):
611 try: 614 try:
612 return fixXHTMLLinks(entry['{}_xhtml'.format(key)]) 615 xhtml = entry['{}_xhtml'.format(key)]
613 except KeyError: 616 except KeyError:
614 try: 617 try:
615 processor = addURLToText if key.startswith('content') else sanitizeHtml 618 processor = addURLToText if key.startswith('content') else sanitizeHtml
616 return convertNewLinesToXHTML(processor(entry[key])) 619 return convertNewLinesToXHTML(processor(entry[key]))
617 except KeyError: 620 except KeyError:
618 return None 621 return None
622 else:
623 # FIXME: empty <div /> elements provoke rendering issue
624 # this regex is a temporary workadound, need more investigation
625 xhtml = re_strip_empty_div.sub("", xhtml)
626 return fixXHTMLLinks(xhtml)