changeset 441:63017904c4d4

server_side: static blog retrieve page's title, meta description and keywords from the parameters
author souliane <souliane@mailoo.org>
date Tue, 13 May 2014 17:09:41 +0200
parents 88461e9c3e47
children 17259c2ff96f
files libervia_server/blog.py
diffstat 1 files changed, 33 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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 = "<img src='%(banner)s' alt='%(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 "<img src='%(url)s' alt='%(alt)s'/>" % {'alt': alt, 'url': url} if re.match(regexp, url) else alt
+
         request.write("""
             <html>
             <head>
                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+                <meta name="keywords" content="%(keywords)s">
+                <meta name="description" content="%(description)s">
                 <link rel="alternate" type="application/atom+xml" href="%(base)s/atom.xml"/>
                 <link rel="stylesheet" type="text/css" href="%(root)scss/blog.css" />
                 <link rel="icon" type="image/png" href="%(root)ssat_logo_16.png">
-                <title>%(user)s's microblog</title>
+                <title>%(title)s</title>
             </head>
             <body>
             <div class="mblog_title"><a href="%(base)s">%(banner_elt)s</a></div>
             """ % {'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: