10
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 Libervia: a Salut à Toi frontend |
|
6 Copyright (C) 2011 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 server_side.html_tools import sanitizeHtml |
|
23 from twisted.internet import reactor, defer |
|
24 from twisted.web import server |
|
25 from twisted.web.resource import Resource |
|
26 from twisted.words.protocols.jabber.jid import JID |
|
27 from datetime import datetime |
|
28 |
|
29 class MicroBlog(Resource): |
|
30 isLeaf = True |
|
31 |
|
32 ERROR_TEMPLATE = """ |
|
33 <html> |
|
34 <head> |
|
35 <title>MICROBLOG ERROR</title> |
|
36 </head> |
|
37 <body> |
|
38 <h1 style='text-align: center; color: red;'>%s</h1> |
|
39 </body> |
|
40 </html> |
|
41 """ |
|
42 |
|
43 def __init__(self,host): |
|
44 self.host = host |
|
45 Resource.__init__(self) |
|
46 if not host.bridge.isConnected("libervia"): #FIXME: hard coded value for test |
|
47 host.bridge.connect("libervia") |
|
48 |
|
49 def render_GET(self, request): |
|
50 if not request.postpath: |
|
51 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname" |
|
52 else: |
|
53 prof_requested = request.postpath[0] |
|
54 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here |
|
55 prof_found = self.host.bridge.getProfileName(prof_requested) |
|
56 if not prof_found or prof_found=='libervia': |
|
57 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname" |
|
58 else: |
|
59 pub_jid=JID(self.host.bridge.getParamA('JabberID','Connection','value',prof_found)) |
|
60 d = defer.Deferred() |
|
61 d.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None) |
|
62 self.host.bridge.getLastMicroblogs(pub_jid.userhost(), 10, 'libervia', d.callback, d.errback) |
|
63 |
|
64 return server.NOT_DONE_YET |
|
65 |
|
66 def render_html_blog(self, mblog_data, request, profile): |
|
67 user = sanitizeHtml(profile).encode('utf-8') |
|
68 request.write(""" |
|
69 <html> |
|
70 <head> |
|
71 <link rel="stylesheet" type="text/css" href="../css/blog.css" /> |
|
72 <title>%(user)s's microblog</title> |
|
73 </head> |
|
74 <body> |
|
75 <div class='mblog_title'>%(user)s</div> |
|
76 """ % {'user':user}) |
|
77 for entry in mblog_data: |
|
78 timestamp = float(entry.get('timestamp',0)) |
|
79 _datetime = datetime.fromtimestamp(timestamp) |
|
80 request.write("<div class='mblog_content'><span class='mblog_timestamp'>%(date)s</span>%(content)s</div>" % { |
|
81 'date':_datetime, |
|
82 'content':sanitizeHtml(entry['content']).encode('utf-8')}) |
|
83 request.write('</body></html>') |
|
84 request.finish() |
|
85 |
|
86 def render_error_blog(self, error, request): |
|
87 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data") |
|
88 request.finish() |