comparison src/server/blog.py @ 726:e949b7c7ed9c

server side (blog): fixes unicode error
author souliane <souliane@mailoo.org>
date Mon, 21 Sep 2015 12:01:34 +0200
parents c1abaa91a121
children 3bd097380da7
comparison
equal deleted inserted replaced
725:c1abaa91a121 726:e949b7c7ed9c
349 @param request: the HTTP request 349 @param request: the HTTP request
350 @param profile 350 @param profile
351 """ 351 """
352 if not isinstance(options, dict): 352 if not isinstance(options, dict):
353 options = {} 353 options = {}
354 user = sanitizeHtml(profile).encode('utf-8') 354 user = sanitizeHtml(profile)
355 root_url = '../' * len(request.postpath) 355 root_url = '../' * len(request.postpath)
356 base_url = root_url + 'blog/' + user 356 base_url = root_url + 'blog/' + user
357 357
358 def getOption(key): 358 def getOption(key):
359 return sanitizeHtml(options[key]).encode('utf-8') if key in options else '' 359 return sanitizeHtml(options[key]) if key in options else ''
360 360
361 def getImageParams(key, default, alt): 361 def getImageParams(key, default, alt):
362 """regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/""" 362 """regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/"""
363 url = options[key].encode('utf-8') if key in options else '' 363 url = options[key] if key in options else ''
364 regexp = r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$" 364 regexp = r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$"
365 if re.match(regexp, url): 365 if re.match(regexp, url):
366 url = url 366 url = url
367 else: 367 else:
368 url = default 368 url = default
479 self.type = "comment" if is_comment else "main_item" 479 self.type = "comment" if is_comment else "main_item"
480 self.style = 'mblog_comment' if is_comment else '' 480 self.style = 'mblog_comment' if is_comment else ''
481 self.content = self.getText(entry, 'content') 481 self.content = self.getText(entry, 'content')
482 482
483 if is_comment: 483 if is_comment:
484 self.author = (_("from %s") % entry['author']).encode('utf-8') 484 self.author = (_("from %s") % entry['author'])
485 else: 485 else:
486 self.author = '&nbsp;' 486 self.author = '&nbsp;'
487 self.url = (u"%s/%s" % (base_url, entry['id'])).encode('utf-8') 487 self.url = (u"%s/%s" % (base_url, entry['id']))
488 self.title = self.getText(entry, 'title') 488 self.title = self.getText(entry, 'title')
489 489
490 count_text = lambda count: D_('comments') if count > 1 else D_('comment') 490 count_text = lambda count: D_('comments') if count > 1 else D_('comment')
491 491
492 self.comments_text = "%s %s" % (comments_count, count_text(comments_count)) 492 self.comments_text = "%s %s" % (comments_count, count_text(comments_count))
502 comments.sort(key=lambda comment: float(comment.get('published', 0))) 502 comments.sort(key=lambda comment: float(comment.get('published', 0)))
503 self.comments = [BlogMessage(request, base_url, comment) for comment in comments] 503 self.comments = [BlogMessage(request, base_url, comment) for comment in comments]
504 504
505 def getText(self, entry, key): 505 def getText(self, entry, key):
506 if ('%s_xhtml' % key) in entry: 506 if ('%s_xhtml' % key) in entry:
507 return entry['%s_xhtml' % key].encode('utf-8') 507 return entry['%s_xhtml' % key]
508 elif key in entry: 508 elif key in entry:
509 processor = addURLToText if key.startswith('content') else sanitizeHtml 509 processor = addURLToText if key.startswith('content') else sanitizeHtml
510 return convertNewLinesToXHTML(processor(entry[key])).encode('utf-8') 510 return convertNewLinesToXHTML(processor(entry[key]))
511 return None 511 return None