# HG changeset patch # User souliane # Date 1399993781 -7200 # Node ID 63017904c4d43ee42e011e4e85d3d62f36416857 # Parent 88461e9c3e479d4d4a13f591c05ead9ab577748f server_side: static blog retrieve page's title, meta description and keywords from the parameters diff -r 88461e9c3e47 -r 63017904c4d4 libervia_server/blog.py --- a/libervia_server/blog.py Thu May 08 19:13:56 2014 +0200 +++ b/libervia_server/blog.py Tue May 13 17:09:41 2014 +0200 @@ -91,20 +91,27 @@ return server.NOT_DONE_YET def render_html_blog(self, mblog_data, request, profile): - """Retrieve the blog banner or other user's specific stuff before actually rendering the static blog + """Retrieve the user parameters before actually rendering the static blog @param mblog_data: list of microblog data or list of couple (microblog data, list of microblog data) @param request: HTTP request @param profile """ - def check_banner(banner): - """Regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/""" - if re.match(r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$", banner): - style = {'banner': banner} - else: - style = {} - self.__render_html_blog(mblog_data, style, request, profile) + d_list = [] + style = {} + + def getCallback(param_name): + d = defer.Deferred() + d.addCallback(lambda value: style.update({param_name: value})) + d_list.append(d) + return d.callback + eb = lambda failure: self.render_error_blog(failure, request, profile) - self.host.bridge.asyncGetParamA('Blog banner', 'Misc', 'value', C.SERVER_SECURITY_LIMIT, profile, callback=check_banner, errback=eb) + + for param_name in (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION): + self.host.bridge.asyncGetParamA(param_name, C.STATIC_BLOG_KEY, 'value', C.SERVER_SECURITY_LIMIT, profile, callback=getCallback(param_name), errback=eb) + + cb = lambda dummy: self.__render_html_blog(mblog_data, style, request, profile) + defer.DeferredList(d_list).addCallback(cb) def __render_html_blog(self, mblog_data, style, request, profile): """Actually render the static blog. If mblog_data is a list of dict, we are missing @@ -120,23 +127,36 @@ user = sanitizeHtml(profile).encode('utf-8') root_url = '../' * len(request.postpath) base_url = root_url + 'blog/' + user - banner = style['banner'].encode('utf-8') if 'banner' in style else '' - banner_elt = "%(user)s" % {'user': user, 'banner': banner} if banner else user + + def getFromData(key): + return sanitizeHtml(style[key]).encode('utf-8') if key in style else '' + + def getImageFromData(key, alt): + """regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/""" + url = style[key].encode('utf-8') if key in style else '' + regexp = r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$" + return "%(alt)s" % {'alt': alt, 'url': url} if re.match(regexp, url) else alt + request.write(""" + + - %(user)s's microblog + %(title)s
%(banner_elt)s
""" % {'base': base_url, 'root': root_url, 'user': user, - 'banner_elt': banner_elt}) + 'keywords': getFromData(C.STATIC_BLOG_PARAM_KEYWORDS), + 'description': getFromData(C.STATIC_BLOG_PARAM_DESCRIPTION), + 'title': getFromData(C.STATIC_BLOG_PARAM_TITLE) or "%s's microblog" % user, + 'banner_elt': getImageFromData(C.STATIC_BLOG_PARAM_BANNER, user)}) mblog_data = [(entry if isinstance(entry, tuple) else (entry, [])) for entry in mblog_data] mblog_data = sorted(mblog_data, key=lambda entry: (-float(entry[0].get('published', 0)))) for entry in mblog_data: