Mercurial > libervia-web
annotate libervia_server/blog.py @ 342:44491e963eee
browser_side: muc user symbols (used for showing who is playing) can also be removed
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 17 Jan 2014 17:20:04 +0100 |
parents | ce5b33f499c5 |
children | d7b98e499935 |
rev | line source |
---|---|
10 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
340 | 5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> |
10 | 6 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
10 # (at your option) any later version. |
10 | 11 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
15 # GNU Affero General Public License for more details. |
10 | 16 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
10 | 19 |
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
|
20 from sat_frontends.tools.strings import addURLToText |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
317
diff
changeset
|
21 from libervia_server.html_tools import sanitizeHtml |
10 | 22 from twisted.internet import reactor, defer |
23 from twisted.web import server | |
24 from twisted.web.resource import Resource | |
25 from twisted.words.protocols.jabber.jid import JID | |
26 from datetime import datetime | |
317
bbadd490e63c
misc: gather the constants in a single file, as it is done for other frontends
souliane <souliane@mailoo.org>
parents:
306
diff
changeset
|
27 from constants import Const |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
28 |
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
29 |
10 | 30 class MicroBlog(Resource): |
31 isLeaf = True | |
32 | |
33 ERROR_TEMPLATE = """ | |
34 <html> | |
35 <head> | |
36 <title>MICROBLOG ERROR</title> | |
37 </head> | |
38 <body> | |
39 <h1 style='text-align: center; color: red;'>%s</h1> | |
40 </body> | |
41 </html> | |
42 """ | |
43 | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
44 def __init__(self, host): |
10 | 45 self.host = host |
46 Resource.__init__(self) | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
47 if not host.bridge.isConnected("libervia"): # FIXME: hard coded value for test |
10 | 48 host.bridge.connect("libervia") |
49 | |
50 def render_GET(self, request): | |
51 if not request.postpath: | |
52 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname" | |
53 else: | |
54 prof_requested = request.postpath[0] | |
55 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here | |
56 prof_found = self.host.bridge.getProfileName(prof_requested) | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
57 if not prof_found or prof_found == 'libervia': |
10 | 58 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname" |
59 else: | |
149
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
60 def got_jid(pub_jid_s): |
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
61 pub_jid = JID(pub_jid_s) |
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
62 d2 = defer.Deferred() |
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
63 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
|
64 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
|
65 |
149
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
66 d1 = defer.Deferred() |
317
bbadd490e63c
misc: gather the constants in a single file, as it is done for other frontends
souliane <souliane@mailoo.org>
parents:
306
diff
changeset
|
67 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', Const.SERVER_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
|
68 d1.addCallbacks(got_jid) |
10 | 69 |
70 return server.NOT_DONE_YET | |
71 | |
72 def render_html_blog(self, mblog_data, request, profile): | |
73 user = sanitizeHtml(profile).encode('utf-8') | |
74 request.write(""" | |
75 <html> | |
76 <head> | |
175
764ca916e56e
browser side: fixed charset in public blog page
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
77 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
10 | 78 <link rel="stylesheet" type="text/css" href="../css/blog.css" /> |
79 <title>%(user)s's microblog</title> | |
80 </head> | |
81 <body> | |
82 <div class='mblog_title'>%(user)s</div> | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
83 """ % {'user': user}) |
176
b9edfa058786
server side: fixed public blog items order
Goffi <goffi@goffi.org>
parents:
175
diff
changeset
|
84 #mblog_data.reverse() |
10 | 85 for entry in mblog_data: |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
86 timestamp = float(entry.get('timestamp', 0)) |
10 | 87 _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
|
88 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
|
89 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
|
90 <span class='mblog_content'>%(content)s</span></div>""" % { |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
91 '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
|
92 'content': body}) |
10 | 93 request.write('</body></html>') |
94 request.finish() | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
95 |
12
513fe9bd0665
server: fixed wrong parameter number in blog resource
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
96 def render_error_blog(self, error, request, profile): |
10 | 97 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data") |
98 request.finish() |