changeset 388:893451e35686

server_side: display blog banner if it is defined
author souliane <souliane@mailoo.org>
date Tue, 25 Feb 2014 11:31:11 +0100 (2014-02-25)
parents 933bce4cb816
children 2d782349b88a
files libervia_server/blog.py
diffstat 1 files changed, 28 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libervia_server/blog.py	Tue Feb 25 07:56:05 2014 +0100
+++ b/libervia_server/blog.py	Tue Feb 25 11:31:11 2014 +0100
@@ -19,13 +19,13 @@
 
 from sat_frontends.tools.strings import addURLToText
 from libervia_server.html_tools import sanitizeHtml
-from twisted.internet import reactor, defer
+from twisted.internet import defer
 from twisted.web import server
 from twisted.web.resource import Resource
 from twisted.words.protocols.jabber.jid import JID
 from datetime import datetime
-from feed import atom
 from constants import Const
+import re
 
 
 class MicroBlog(Resource):
@@ -65,7 +65,7 @@
                         d2.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, prof_found], None)
                         self.host.bridge.getLastGroupBlogsAtom(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
                     else:
-                        d2.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
+                        d2.addCallbacks(self._render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
                         self.host.bridge.getLastGroupBlogs(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
 
                 d1 = defer.Deferred()
@@ -74,8 +74,30 @@
 
                 return server.NOT_DONE_YET
 
-    def render_html_blog(self, mblog_data, request, profile):
+    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"""
+        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)
+        eb = lambda failure: self.render_error_blog(failure, request, profile)
+        self.host.bridge.asyncGetParamA('Blog banner', 'Misc', 'value', Const.SERVER_SECURITY_LIMIT, profile, callback=check_banner, errback=eb)
+
+    def render_html_blog(self, mblog_data, style, request, profile):
+        """Actually rendering the static blog
+        @param mblog_data: list of microblog data
+        @param style: dict defining the blog's rendering parameters
+        @param request: the HTTP request
+        @profile
+        """
+        if not isinstance(style, dict):
+            style = {}
         user = sanitizeHtml(profile).encode('utf-8')
+        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
         request.write("""
             <html>
             <head>
@@ -85,8 +107,8 @@
                 <title>%(user)s's microblog</title>
             </head>
             <body>
-                <div class='mblog_title'>%(user)s</div>
-            """ % {'user': user})
+            <div class='mblog_title'>%(banner_elt)s</div>
+            """ % {'user': user, 'banner_elt': banner_elt})
         mblog_data = sorted(mblog_data, key=lambda entry: (-float(entry.get('published', 0))))
         for entry in mblog_data:
             timestamp = float(entry.get('published', 0))