annotate server_side/blog.py @ 306:52b1afd7ac3f

server_side: display rich text in blogs (addURLToText is now in sat_frontends/tools/strings)
author souliane <souliane@mailoo.org>
date Wed, 18 Dec 2013 15:39:13 +0100
parents 63e9b680d3e7
children bbadd490e63c
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
165
9763dec220ed dates update
Goffi <goffi@goffi.org>
parents: 149
diff changeset
6 Copyright (C) 2011, 2012, 2013 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
306
52b1afd7ac3f server_side: display rich text in blogs (addURLToText is now in sat_frontends/tools/strings)
souliane <souliane@mailoo.org>
parents: 243
diff changeset
22 from sat_frontends.tools.strings import addURLToText
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from server_side.html_tools import sanitizeHtml
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import reactor, defer
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.web import server
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.web.resource import Resource
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.jid import JID
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from datetime import datetime
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
29
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
30 # Security limit for Libervia server_side
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
31 SECURITY_LIMIT = -1
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
32
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
33
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
34 class MicroBlog(Resource):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
35 isLeaf = True
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
36
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
37 ERROR_TEMPLATE = """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
38 <html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
39 <head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
40 <title>MICROBLOG ERROR</title>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
41 </head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
42 <body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
43 <h1 style='text-align: center; color: red;'>%s</h1>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
44 </body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
45 </html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
46 """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
47
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
48 def __init__(self, host):
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
50 Resource.__init__(self)
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
51 if not host.bridge.isConnected("libervia"): # FIXME: hard coded value for test
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
52 host.bridge.connect("libervia")
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
53
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def render_GET(self, request):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if not request.postpath:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname"
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
57 else:
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
58 prof_requested = request.postpath[0]
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
59 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
60 prof_found = self.host.bridge.getProfileName(prof_requested)
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
61 if not prof_found or prof_found == 'libervia':
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname"
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
63 else:
149
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
64 def got_jid(pub_jid_s):
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
65 pub_jid = JID(pub_jid_s)
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
66 d2 = defer.Deferred()
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
67 d2.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
68 self.host.bridge.getLastGroupBlogs(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
69
149
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
70 d1 = defer.Deferred()
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
71 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', SECURITY_LIMIT, prof_found, callback=d1.callback, errback=d1.errback))
149
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
72 d1.addCallbacks(got_jid)
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
73
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return server.NOT_DONE_YET
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
75
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def render_html_blog(self, mblog_data, request, profile):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
77 user = sanitizeHtml(profile).encode('utf-8')
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
78 request.write("""
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
79 <html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
80 <head>
175
764ca916e56e browser side: fixed charset in public blog page
Goffi <goffi@goffi.org>
parents: 165
diff changeset
81 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
82 <link rel="stylesheet" type="text/css" href="../css/blog.css" />
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
83 <title>%(user)s's microblog</title>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
84 </head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
85 <body>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
86 <div class='mblog_title'>%(user)s</div>
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
87 """ % {'user': user})
176
b9edfa058786 server side: fixed public blog items order
Goffi <goffi@goffi.org>
parents: 175
diff changeset
88 #mblog_data.reverse()
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
89 for entry in mblog_data:
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
90 timestamp = float(entry.get('timestamp', 0))
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
91 _datetime = datetime.fromtimestamp(timestamp)
306
52b1afd7ac3f server_side: display rich text in blogs (addURLToText is now in sat_frontends/tools/strings)
souliane <souliane@mailoo.org>
parents: 243
diff changeset
92 body = addURLToText(sanitizeHtml(entry['content'])).encode('utf-8') if 'xhtml' not in entry else entry['xhtml'].encode()
228
6efd189e8d78 server_side: put the blog entry content in its own span + CSS customization
souliane <souliane@mailoo.org>
parents: 176
diff changeset
93 request.write("""<div class='mblog_entry'><span class='mblog_timestamp'>%(date)s</span>
6efd189e8d78 server_side: put the blog entry content in its own span + CSS customization
souliane <souliane@mailoo.org>
parents: 176
diff changeset
94 <span class='mblog_content'>%(content)s</span></div>""" % {
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
95 'date': _datetime,
306
52b1afd7ac3f server_side: display rich text in blogs (addURLToText is now in sat_frontends/tools/strings)
souliane <souliane@mailoo.org>
parents: 243
diff changeset
96 'content': body})
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
97 request.write('</body></html>')
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
98 request.finish()
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
99
12
513fe9bd0665 server: fixed wrong parameter number in blog resource
Goffi <goffi@goffi.org>
parents: 10
diff changeset
100 def render_error_blog(self, error, request, profile):
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
101 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data")
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
102 request.finish()