comparison libervia_server/blog.py @ 331:06a48d805547

server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port. ==> NOTE from Goffi: it's a fixed version of Link Mauve's patch c144b603fb93 Fixes bug 16.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 04 Feb 2014 17:09:00 +0100
parents server_side/blog.py@bbadd490e63c
children 2067d6241927
comparison
equal deleted inserted replaced
330:e43a1a0b4f23 331:06a48d805547
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Libervia: a Salut à Toi frontend
6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from sat_frontends.tools.strings import addURLToText
23 from libervia_server.html_tools import sanitizeHtml
24 from twisted.internet import reactor, defer
25 from twisted.web import server
26 from twisted.web.resource import Resource
27 from twisted.words.protocols.jabber.jid import JID
28 from datetime import datetime
29 from constants import Const
30
31
32 class MicroBlog(Resource):
33 isLeaf = True
34
35 ERROR_TEMPLATE = """
36 <html>
37 <head>
38 <title>MICROBLOG ERROR</title>
39 </head>
40 <body>
41 <h1 style='text-align: center; color: red;'>%s</h1>
42 </body>
43 </html>
44 """
45
46 def __init__(self, host):
47 self.host = host
48 Resource.__init__(self)
49 if not host.bridge.isConnected("libervia"): # FIXME: hard coded value for test
50 host.bridge.connect("libervia")
51
52 def render_GET(self, request):
53 if not request.postpath:
54 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname"
55 else:
56 prof_requested = request.postpath[0]
57 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here
58 prof_found = self.host.bridge.getProfileName(prof_requested)
59 if not prof_found or prof_found == 'libervia':
60 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname"
61 else:
62 def got_jid(pub_jid_s):
63 pub_jid = JID(pub_jid_s)
64 d2 = defer.Deferred()
65 d2.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
66 self.host.bridge.getLastGroupBlogs(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
67
68 d1 = defer.Deferred()
69 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', Const.SERVER_SECURITY_LIMIT, prof_found, callback=d1.callback, errback=d1.errback))
70 d1.addCallbacks(got_jid)
71
72 return server.NOT_DONE_YET
73
74 def render_html_blog(self, mblog_data, request, profile):
75 user = sanitizeHtml(profile).encode('utf-8')
76 request.write("""
77 <html>
78 <head>
79 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
80 <link rel="stylesheet" type="text/css" href="../css/blog.css" />
81 <title>%(user)s's microblog</title>
82 </head>
83 <body>
84 <div class='mblog_title'>%(user)s</div>
85 """ % {'user': user})
86 #mblog_data.reverse()
87 for entry in mblog_data:
88 timestamp = float(entry.get('timestamp', 0))
89 _datetime = datetime.fromtimestamp(timestamp)
90 body = addURLToText(sanitizeHtml(entry['content'])).encode('utf-8') if 'xhtml' not in entry else entry['xhtml'].encode()
91 request.write("""<div class='mblog_entry'><span class='mblog_timestamp'>%(date)s</span>
92 <span class='mblog_content'>%(content)s</span></div>""" % {
93 'date': _datetime,
94 'content': body})
95 request.write('</body></html>')
96 request.finish()
97
98 def render_error_blog(self, error, request, profile):
99 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data")
100 request.finish()