annotate server_side/blog.py @ 121:2d40b0f5fb37

server side: asynchronous login
author Goffi <goffi@goffi.org>
date Sat, 10 Dec 2011 10:41:40 +0100
parents 9d8e79ac4c9c
children ddfcc4cb6cee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
3
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
66
9d8e79ac4c9c Login/Register box: integration of Adrien Vigneron's design
Goffi <goffi@goffi.org>
parents: 15
diff changeset
6 Copyright (C) 2011 Jérôme Poisson <goffi@goffi.org>
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
7
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
12
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
17
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
21
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from server_side.html_tools import sanitizeHtml
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import reactor, defer
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.web import server
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.web.resource import Resource
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber.jid import JID
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from datetime import datetime
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
28
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
29 class MicroBlog(Resource):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
30 isLeaf = True
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
31
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
32 ERROR_TEMPLATE = """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
33 <html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
34 <head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
35 <title>MICROBLOG ERROR</title>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
36 </head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
37 <body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
38 <h1 style='text-align: center; color: red;'>%s</h1>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
39 </body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
40 </html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
41 """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
42
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def __init__(self,host):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.host = host
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
45 Resource.__init__(self)
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
46 if not host.bridge.isConnected("libervia"): #FIXME: hard coded value for test
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
47 host.bridge.connect("libervia")
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
48
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def render_GET(self, request):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if not request.postpath:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname"
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
53 prof_requested = request.postpath[0]
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
54 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
55 prof_found = self.host.bridge.getProfileName(prof_requested)
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if not prof_found or prof_found=='libervia':
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
57 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname"
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
58 else:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
59 pub_jid=JID(self.host.bridge.getParamA('JabberID','Connection','value',prof_found))
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
60 d = defer.Deferred()
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
61 d.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.host.bridge.getLastMicroblogs(pub_jid.userhost(), 10, 'libervia', d.callback, d.errback)
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
63
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return server.NOT_DONE_YET
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
65
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def render_html_blog(self, mblog_data, request, profile):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
67 user = sanitizeHtml(profile).encode('utf-8')
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
68 request.write("""
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
69 <html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
70 <head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
71 <link rel="stylesheet" type="text/css" href="../css/blog.css" />
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
72 <title>%(user)s's microblog</title>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
73 </head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
74 <body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
75 <div class='mblog_title'>%(user)s</div>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """ % {'user':user})
15
78bfc398926b public blog is now shown i reverse order
Goffi <goffi@goffi.org>
parents: 12
diff changeset
77 mblog_data.reverse()
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
78 for entry in mblog_data:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
79 timestamp = float(entry.get('timestamp',0))
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
80 _datetime = datetime.fromtimestamp(timestamp)
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
81 request.write("<div class='mblog_content'><span class='mblog_timestamp'>%(date)s</span>%(content)s</div>" % {
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
82 'date':_datetime,
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
83 'content':sanitizeHtml(entry['content']).encode('utf-8')})
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
84 request.write('</body></html>')
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
85 request.finish()
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
86
12
513fe9bd0665 server: fixed wrong parameter number in blog resource
Goffi <goffi@goffi.org>
parents: 10
diff changeset
87 def render_error_blog(self, error, request, profile):
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
88 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data")
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
89 request.finish()