Mercurial > libervia-web
annotate libervia_server/blog.py @ 388:893451e35686
server_side: display blog banner if it is defined
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 25 Feb 2014 11:31:11 +0100 |
parents | 41a386a27002 |
children | 2d782349b88a |
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 |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
22 from twisted.internet import defer |
10 | 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 |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
28 import re |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
29 |
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
30 |
10 | 31 class MicroBlog(Resource): |
32 isLeaf = True | |
33 | |
34 ERROR_TEMPLATE = """ | |
35 <html> | |
36 <head> | |
37 <title>MICROBLOG ERROR</title> | |
38 </head> | |
39 <body> | |
40 <h1 style='text-align: center; color: red;'>%s</h1> | |
41 </body> | |
42 </html> | |
43 """ | |
44 | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
45 def __init__(self, host): |
10 | 46 self.host = host |
47 Resource.__init__(self) | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
48 if not host.bridge.isConnected("libervia"): # FIXME: hard coded value for test |
10 | 49 host.bridge.connect("libervia") |
50 | |
51 def render_GET(self, request): | |
52 if not request.postpath: | |
53 return MicroBlog.ERROR_TEMPLATE % "You must indicate a nickname" | |
54 else: | |
55 prof_requested = request.postpath[0] | |
56 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here | |
57 prof_found = self.host.bridge.getProfileName(prof_requested) | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
58 if not prof_found or prof_found == 'libervia': |
10 | 59 return MicroBlog.ERROR_TEMPLATE % "Invalid nickname" |
60 else: | |
149
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
61 def got_jid(pub_jid_s): |
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
62 pub_jid = JID(pub_jid_s) |
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
63 d2 = defer.Deferred() |
344
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
64 if len(request.postpath) > 1 and request.postpath[1] == 'atom.xml': |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
65 d2.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, prof_found], None) |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
66 self.host.bridge.getLastGroupBlogsAtom(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback) |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
67 else: |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
68 d2.addCallbacks(self._render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None) |
344
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
69 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
|
70 |
149
f78761e1be8e
server side: fixed public microblog
Goffi <goffi@goffi.org>
parents:
131
diff
changeset
|
71 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
|
72 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
|
73 d1.addCallbacks(got_jid) |
10 | 74 |
75 return server.NOT_DONE_YET | |
76 | |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
77 def _render_html_blog(self, mblog_data, request, profile): |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
78 """Retrieve the blog banner or other user's specific stuff before actually rendering the static blog""" |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
79 def check_banner(banner): |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
80 """Regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/""" |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
81 if re.match(r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$", banner): |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
82 style = {'banner': banner} |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
83 else: |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
84 style = {} |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
85 self.render_html_blog(mblog_data, style, request, profile) |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
86 eb = lambda failure: self.render_error_blog(failure, request, profile) |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
87 self.host.bridge.asyncGetParamA('Blog banner', 'Misc', 'value', Const.SERVER_SECURITY_LIMIT, profile, callback=check_banner, errback=eb) |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
88 |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
89 def render_html_blog(self, mblog_data, style, request, profile): |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
90 """Actually rendering the static blog |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
91 @param mblog_data: list of microblog data |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
92 @param style: dict defining the blog's rendering parameters |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
93 @param request: the HTTP request |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
94 @profile |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
95 """ |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
96 if not isinstance(style, dict): |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
97 style = {} |
10 | 98 user = sanitizeHtml(profile).encode('utf-8') |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
99 banner = style['banner'].encode('utf-8') if 'banner' in style else '' |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
100 banner_elt = "<img src='%(banner)s' alt='%(user)s'/>" % {'user': user, 'banner': banner} if banner else user |
10 | 101 request.write(""" |
102 <html> | |
103 <head> | |
175
764ca916e56e
browser side: fixed charset in public blog page
Goffi <goffi@goffi.org>
parents:
165
diff
changeset
|
104 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
375
41a386a27002
server and browser sides: add in the static blog the link to the Atom feed + set encoding of libervia.html
souliane <souliane@mailoo.org>
parents:
371
diff
changeset
|
105 <link rel="alternate" type="application/atom+xml" href="%(user)s/atom.xml"/> |
10 | 106 <link rel="stylesheet" type="text/css" href="../css/blog.css" /> |
107 <title>%(user)s's microblog</title> | |
108 </head> | |
109 <body> | |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
110 <div class='mblog_title'>%(banner_elt)s</div> |
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
111 """ % {'user': user, 'banner_elt': banner_elt}) |
371
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
112 mblog_data = sorted(mblog_data, key=lambda entry: (-float(entry.get('published', 0)))) |
10 | 113 for entry in mblog_data: |
345
2109d7d30ffc
server_side: sort blog post with "published" instead of "timestamp" + display titles in <h1> tags.
souliane <souliane@mailoo.org>
parents:
344
diff
changeset
|
114 timestamp = float(entry.get('published', 0)) |
10 | 115 _datetime = datetime.fromtimestamp(timestamp) |
371
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
116 |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
117 def getText(key): |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
118 if ('%s_xhtml' % key) in entry: |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
119 return entry['%s_xhtml' % key].encode('utf-8') |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
120 elif key in entry: |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
121 processor = addURLToText if key.startswith('content') else sanitizeHtml |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
122 return processor(entry[key]).encode('utf-8') |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
123 return '' |
d7870ab9d1ff
server_side: blog: bug fixes
souliane <souliane@mailoo.org>
parents:
345
diff
changeset
|
124 |
345
2109d7d30ffc
server_side: sort blog post with "published" instead of "timestamp" + display titles in <h1> tags.
souliane <souliane@mailoo.org>
parents:
344
diff
changeset
|
125 body = getText('content') |
2109d7d30ffc
server_side: sort blog post with "published" instead of "timestamp" + display titles in <h1> tags.
souliane <souliane@mailoo.org>
parents:
344
diff
changeset
|
126 title = getText('title') |
2109d7d30ffc
server_side: sort blog post with "published" instead of "timestamp" + display titles in <h1> tags.
souliane <souliane@mailoo.org>
parents:
344
diff
changeset
|
127 if title: |
2109d7d30ffc
server_side: sort blog post with "published" instead of "timestamp" + display titles in <h1> tags.
souliane <souliane@mailoo.org>
parents:
344
diff
changeset
|
128 body = "<h1>%s</h1>\n%s" % (title, body) |
228
6efd189e8d78
server_side: put the blog entry content in its own span + CSS customization
souliane <souliane@mailoo.org>
parents:
176
diff
changeset
|
129 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
|
130 <span class='mblog_content'>%(content)s</span></div>""" % { |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
131 '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
|
132 'content': body}) |
10 | 133 request.write('</body></html>') |
134 request.finish() | |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
135 |
344
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
136 def render_atom_feed(self, feed, request): |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
137 request.write(feed.encode('utf-8')) |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
138 request.finish() |
d7b98e499935
server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents:
340
diff
changeset
|
139 |
12
513fe9bd0665
server: fixed wrong parameter number in blog resource
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
140 def render_error_blog(self, error, request, profile): |
10 | 141 request.write(MicroBlog.ERROR_TEMPLATE % "Can't access requested data") |
142 request.finish() |