annotate libervia_server/blog.py @ 411:a0256b81d367

setup.py: update website (it's not http://www.salut-a-toi.org), and removed pyjamas framework which is not standard in pypi
author Goffi <goffi@goffi.org>
date Tue, 18 Mar 2014 19:18:16 +0100
parents 35a43d0dc032
children bbdbee25123a
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
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 331
diff changeset
4 # Libervia: a Salut à Toi frontend
340
ce5b33f499c5 dates update
Goffi <goffi@goffi.org>
parents: 339
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
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
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
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
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
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
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
19
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
20 from sat.core.i18n import _
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
21 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
22 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
23 from twisted.internet import defer
10
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
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
28 from constants import Const
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
29 import uuid
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
30 import re
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
31
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
32
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class MicroBlog(Resource):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
34 isLeaf = True
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
35
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
36 ERROR_TEMPLATE = """
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
37 <html>
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
38 <head profile="http://www.w3.org/2005/10/profile">
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
39 <link rel="icon" type="image/png" href="%(root)ssat_logo_16.png">
10
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>
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
43 <h1 style='text-align: center; color: red;'>%(message)s</h1>
10
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:
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
56 return MicroBlog.ERROR_TEMPLATE % {'root': '',
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
57 'message': "You must indicate a nickname"}
10
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 prof_requested = request.postpath[0]
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
60 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
61 prof_found = self.host.bridge.getProfileName(prof_requested)
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
62 if not prof_found or prof_found == 'libervia':
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
63 return MicroBlog.ERROR_TEMPLATE % {'root': '../' * len(request.postpath),
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
64 'message': "Invalid nickname"}
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
65 else:
149
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
66 def got_jid(pub_jid_s):
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
67 pub_jid = JID(pub_jid_s)
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
68 d2 = defer.Deferred()
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
69 item_id = None
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
70 if len(request.postpath) > 1:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
71 if request.postpath[1] == 'atom.xml': # return the atom feed
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
72 d2.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, prof_found], None)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
73 self.host.bridge.getLastGroupBlogsAtom(pub_jid.userhost(), 10, 'libervia', d2.callback, d2.errback)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
74 return
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
75 try: # check if the given path is a valid UUID
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
76 uuid.UUID(request.postpath[1])
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
77 item_id = request.postpath[1]
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
78 except ValueError:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
79 pass
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
80 d2.addCallbacks(self.render_html_blog, self.render_error_blog, [request, prof_found], None, [request, prof_found], None)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
81 if item_id: # display one message and its comments
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
82 self.host.bridge.getGroupBlogsWithComments(pub_jid.userhost(), [item_id], 'libervia', d2.callback, d2.errback)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
83 else: # display the last messages without comment
344
d7b98e499935 server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents: 340
diff changeset
84 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
85
149
f78761e1be8e server side: fixed public microblog
Goffi <goffi@goffi.org>
parents: 131
diff changeset
86 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
87 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
88 d1.addCallbacks(got_jid)
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
89
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return server.NOT_DONE_YET
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
91
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
92 def render_html_blog(self, mblog_data, request, profile):
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
93 """Retrieve the blog banner or other user's specific stuff before actually rendering the static blog
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
94 @param mblog_data: list of microblog data or list of couple (microblog data, list of microblog data)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
95 @param request: HTTP request
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
96 @param profile
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
97 """
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
98 def check_banner(banner):
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
99 """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
100 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
101 style = {'banner': banner}
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
102 else:
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
103 style = {}
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
104 self.__render_html_blog(mblog_data, style, request, profile)
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
105 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
106 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
107
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
108 def __render_html_blog(self, mblog_data, style, request, profile):
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
109 """Actually render the static blog. If mblog_data is a list of dict, we are missing
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
110 the comments items so we just display the main items. If mblog_data is a list of couple,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
111 each couple is associating a main item data with the list of its comments, so we render all.
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
112 @param mblog_data: list of microblog data or list of couple (microblog data, list of microblog data)
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
113 @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
114 @param request: the HTTP request
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
115 @profile
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
116 """
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
117 if not isinstance(style, dict):
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
118 style = {}
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
119 user = sanitizeHtml(profile).encode('utf-8')
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
120 root_url = '../' * len(request.postpath)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
121 base_url = root_url + 'blog/' + user
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
122 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
123 banner_elt = "<img src='%(banner)s' alt='%(user)s'/>" % {'user': user, 'banner': banner} if banner else user
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
124 request.write("""
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
125 <html>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
126 <head>
175
764ca916e56e browser side: fixed charset in public blog page
Goffi <goffi@goffi.org>
parents: 165
diff changeset
127 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
128 <link rel="alternate" type="application/atom+xml" href="%(base)s/atom.xml"/>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
129 <link rel="stylesheet" type="text/css" href="%(root)scss/blog.css" />
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
130 <link rel="icon" type="image/png" href="%(root)ssat_logo_16.png">
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
131 <title>%(user)s's microblog</title>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
132 </head>
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
133 <body>
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
134 <div class="mblog_title"><a href="%(base)s">%(banner_elt)s</a></div>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
135 """ % {'base': base_url,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
136 'root': root_url,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
137 'user': user,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
138 'banner_elt': banner_elt})
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
139 mblog_data = [(entry if isinstance(entry, tuple) else (entry, [])) for entry in mblog_data]
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
140 mblog_data = sorted(mblog_data, key=lambda entry: (-float(entry[0].get('published', 0))))
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
141 for entry in mblog_data:
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
142 self.__render_html_entry(entry[0], base_url, request)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
143 comments = sorted(entry[1], key=lambda entry: (float(entry.get('published', 0))))
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
144 for comment in comments:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
145 self.__render_html_entry(comment, base_url, request)
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
146 request.write('</body></html>')
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
147 request.finish()
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
148
389
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
149 def __render_html_entry(self, entry, base_url, request):
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
150 """Render one microblog entry.
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
151 @param entry: the microblog entry
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
152 @param base_url: the base url of the blog
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
153 @param request: the HTTP request
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
154 """
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
155 timestamp = float(entry.get('published', 0))
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
156 datetime_ = datetime.fromtimestamp(timestamp)
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
157 is_comment = entry['type'] == 'comment'
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
158 if is_comment:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
159 author = (_("comment from %s") % entry['author']).encode('utf-8')
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
160 item_link = ''
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
161 else:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
162 author = '&nbsp;'
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
163 item_link = ("%(base)s/%(item_id)s" % {'base': base_url, 'item_id': entry['id']}).encode('utf-8')
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
164
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
165 def getText(key):
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
166 if ('%s_xhtml' % key) in entry:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
167 return entry['%s_xhtml' % key].encode('utf-8')
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
168 elif key in entry:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
169 processor = addURLToText if key.startswith('content') else sanitizeHtml
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
170 return processor(entry[key]).encode('utf-8')
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
171 return ''
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
172
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
173 def addMainItemLink(elem):
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
174 if not item_link or not elem:
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
175 return elem
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
176 return """<a href="%(link)s" class="item_link">%(elem)s</a>""" % {'link': item_link, 'elem': elem}
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
177
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
178 header = addMainItemLink("""<div class="mblog_header">
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
179 <div class="mblog_metadata">
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
180 <div class="mblog_author">%(author)s</div>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
181 <div class="mblog_timestamp">%(date)s</div>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
182 </div>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
183 </div>""" % {'author': author, 'date': datetime_})
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
184
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
185 title = addMainItemLink(getText('title'))
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
186 body = getText('content')
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
187 if title: # insert the title within the body
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
188 body = """<h1>%(title)s</h1>\n%(body)s""" % {'title': title, 'body': body}
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
189
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
190 request.write("""<div class="mblog_entry %(extra_style)s">
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
191 %(header)s
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
192 <span class="mblog_content">%(content)s</span>
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
193 </div>""" %
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
194 {'extra_style': 'mblog_comment' if entry['type'] == 'comment' else '',
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
195 'item_link': item_link,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
196 'header': header,
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
197 'content': body})
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
198
344
d7b98e499935 server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents: 340
diff changeset
199 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
200 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
201 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
202
12
513fe9bd0665 server: fixed wrong parameter number in blog resource
Goffi <goffi@goffi.org>
parents: 10
diff changeset
203 def render_error_blog(self, error, request, profile):
409
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
204 request.write(MicroBlog.ERROR_TEMPLATE % {'root': '../' * len(request.postpath),
35a43d0dc032 browser and server sides: display favicon
souliane <souliane@mailoo.org>
parents: 389
diff changeset
205 'message': "Can't access requested data"})
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
206 request.finish()