# HG changeset patch # User souliane # Date 1393324271 -3600 # Node ID 893451e35686e3906c4e36f5a7635b384a1d85f0 # Parent 933bce4cb816b1bc9f23b0b79d84e26dbdc3d42f server_side: display blog banner if it is defined diff -r 933bce4cb816 -r 893451e35686 libervia_server/blog.py --- 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 = "%(user)s" % {'user': user, 'banner': banner} if banner else user request.write(""" @@ -85,8 +107,8 @@ %(user)s's microblog -
%(user)s
- """ % {'user': user}) +
%(banner_elt)s
+ """ % {'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))