Mercurial > libervia-web
annotate src/server/blog.py @ 815:55a2cf64ffee
browser (chat): printing the history is left to quick_frontend
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 22 Dec 2015 11:37:25 +0100 |
parents | f0ad3f62bae2 |
children | f8a7a046ff9c |
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 |
685 | 5 # Copyright (C) 2011, 2012, 2013, 2014, 2015 Jérôme Poisson <goffi@goffi.org> |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
6 # Copyright (C) 2013, 2014, 2015 Adrien Cossa <souliane@mailoo.org> |
10 | 7 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # (at your option) any later version. |
10 | 12 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # GNU Affero General Public License for more details. |
10 | 17 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
18 # 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
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
10 | 20 |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
21 from sat.core.i18n import _, D_ |
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 |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
23 from sat.core.log import getLogger |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
24 log = getLogger(__name__) |
760
78caef6ed8e7
static blog: replaced mbdata2tags by sat.tools.common.dict2iter
Goffi <goffi@goffi.org>
parents:
749
diff
changeset
|
25 from sat.tools import common |
728
9d35d75566fb
server_side (blog): clean error message when unknown profile is requested
souliane <souliane@mailoo.org>
parents:
727
diff
changeset
|
26 from dbus.exceptions import DBusException |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
27 from twisted.internet import defer |
10 | 28 from twisted.web import server |
29 from twisted.web.resource import Resource | |
30 from twisted.words.protocols.jabber.jid import JID | |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
31 from jinja2 import Environment, PackageLoader |
10 | 32 from datetime import datetime |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
33 from sys import path |
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
|
34 import uuid |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
35 import re |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
36 import os |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
37 |
588
c8cca1a373dd
server_side: static blog: convert \n in raw text message to <br/>
souliane <souliane@mailoo.org>
parents:
586
diff
changeset
|
38 from libervia.server.html_tools import sanitizeHtml, convertNewLinesToXHTML |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
39 from libervia.server.constants import Const as C |
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
40 |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
228
diff
changeset
|
41 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
42 NS_MICROBLOG = 'urn:xmpp:microblog:0' |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
43 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
44 |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
45 class TemplateProcessor(object): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
46 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
47 THEME = 'default' |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
48 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
49 def __init__(self, host): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
50 self.host = host |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
51 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
52 # add Libervia's themes directory to the python path |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
53 path.append(os.path.dirname(os.path.normpath(self.host.themes_dir))) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
54 themes = os.path.basename(os.path.normpath(self.host.themes_dir)) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
55 self.env = Environment(loader=PackageLoader(themes, self.THEME)) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
56 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
57 def useTemplate(self, request, tpl, data=None): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
58 root_url = '../' * len(request.postpath) |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
59 theme_url = os.path.join(root_url, 'themes', self.THEME) |
10 | 60 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
61 data_ = {'images': os.path.join(theme_url, 'images'), |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
62 'styles': os.path.join(theme_url, 'styles'), |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
63 } |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
64 if data: |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
65 data_.update(data) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
66 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
67 template = self.env.get_template('%s.html' % tpl) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
68 return template.render(**data_).encode('utf-8') |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
69 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
70 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
71 class MicroBlog(Resource, TemplateProcessor): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
72 isLeaf = True |
10 | 73 |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
74 def __init__(self, host): |
10 | 75 self.host = host |
76 Resource.__init__(self) | |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
77 TemplateProcessor.__init__(self, host) |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
78 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedHandler) |
484
ae86b32b959c
server side (blog): manage our own avatars cache because existing entity data are not updated
souliane <souliane@mailoo.org>
parents:
483
diff
changeset
|
79 self.avatars_cache = {} |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
80 self.waiting_deferreds = {} |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
81 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
82 def entityDataUpdatedHandler(self, entity_s, key, value, dummy): |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
83 """Retrieve the avatar we've been waiting for and fires the callback. |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
84 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
85 @param entity_s (str): JID of the contact |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
86 @param key (str): entity data key |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
87 @param value (str): entity data value |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
88 @param dummy (str): that would be C.SERVICE_PROFILE |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
89 """ |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
90 if key != "avatar": |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
91 return |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
92 log.debug(_(u"Received a new avatar for entity %s") % entity_s) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
93 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
94 url = os.path.join(C.AVATARS_DIR, value) |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
95 self.avatars_cache[entity_s] = url |
749
7168a9873dde
server_side (blog): fixes avatar callback
souliane <souliane@mailoo.org>
parents:
747
diff
changeset
|
96 try: |
7168a9873dde
server_side (blog): fixes avatar callback
souliane <souliane@mailoo.org>
parents:
747
diff
changeset
|
97 self.waiting_deferreds[entity_s].callback(url) |
7168a9873dde
server_side (blog): fixes avatar callback
souliane <souliane@mailoo.org>
parents:
747
diff
changeset
|
98 del self.waiting_deferreds[entity_s] |
7168a9873dde
server_side (blog): fixes avatar callback
souliane <souliane@mailoo.org>
parents:
747
diff
changeset
|
99 except KeyError: |
7168a9873dde
server_side (blog): fixes avatar callback
souliane <souliane@mailoo.org>
parents:
747
diff
changeset
|
100 pass |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
101 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
102 def getAvatarURL(self, pub_jid): |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
103 """Return avatar of a jid if in cache, else ask for it. |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
104 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
105 @param pub_jid (JID): publisher JID |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
106 @return: deferred avatar URL (unicode) |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
107 """ |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
108 bare_jid_s = pub_jid.userhost() |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
109 try: |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
110 url = self.avatars_cache[bare_jid_s] |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
111 except KeyError: |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
112 self.avatars_cache[bare_jid_s] = '' # avoid to request the vcard several times |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
113 self.host.bridge.getCard(bare_jid_s, C.SERVICE_PROFILE) |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
114 d = defer.Deferred() |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
115 self.waiting_deferreds[bare_jid_s] = d |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
116 return d |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
117 return defer.succeed(url if url else C.DEFAULT_AVATAR_URL) |
10 | 118 |
119 def render_GET(self, request): | |
120 if not request.postpath: | |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
121 return self.useTemplate(request, "static_blog_error", {'message': "You must indicate a nickname"}) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
122 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
123 prof_requested = request.postpath[0] |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
124 #TODO : char check: only use alphanumeric chars + some extra(_,-,...) here |
728
9d35d75566fb
server_side (blog): clean error message when unknown profile is requested
souliane <souliane@mailoo.org>
parents:
727
diff
changeset
|
125 try: |
9d35d75566fb
server_side (blog): clean error message when unknown profile is requested
souliane <souliane@mailoo.org>
parents:
727
diff
changeset
|
126 prof_found = self.host.bridge.getProfileName(prof_requested) |
9d35d75566fb
server_side (blog): clean error message when unknown profile is requested
souliane <souliane@mailoo.org>
parents:
727
diff
changeset
|
127 except DBusException: |
9d35d75566fb
server_side (blog): clean error message when unknown profile is requested
souliane <souliane@mailoo.org>
parents:
727
diff
changeset
|
128 prof_found = None |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
129 if not prof_found or prof_found == C.SERVICE_PROFILE: |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
130 return self.useTemplate(request, "static_blog_error", {'message': "Invalid nickname"}) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
131 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
132 d = defer.Deferred() |
747 | 133 self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', profile_key=prof_found, callback=d.callback, errback=d.errback) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
134 d.addCallbacks(lambda pub_jid_s: self.gotJID(pub_jid_s, request, prof_found)) |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
135 return server.NOT_DONE_YET |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
136 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
137 def gotJID(self, pub_jid_s, request, profile): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
138 pub_jid = JID(pub_jid_s) |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
139 |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
140 self.parseURLParams(request) |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
141 if request.item_id: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
142 item_ids = [request.item_id] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
143 max_items = 1 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
144 else: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
145 item_ids = [] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
146 max_items = int(request.extra_dict['rsm_max']) |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
147 |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
148 if request.atom: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
149 self.host.bridge.mbGetAtom(pub_jid.userhost(), NS_MICROBLOG, max_items, item_ids, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
150 request.extra_dict, C.SERVICE_PROFILE, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
151 lambda feed: self.render_atom_feed(feed, request), |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
152 lambda failure: self.render_error_blog(failure, request, pub_jid)) |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
153 elif request.item_id: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
154 self.getItemById(pub_jid, request.item_id, request.extra_dict, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
155 request.extra_comments_dict, request, profile) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
156 else: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
157 self.getItems(pub_jid, max_items, request.extra_dict, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
158 request.extra_comments_dict, request, profile) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
159 |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
160 def parseURLParams(self, request): |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
161 """Parse the request URL parameters. |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
162 |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
163 @param request: HTTP request |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
164 """ |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
165 request.item_id = None |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
166 request.atom = False |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
167 |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
168 if len(request.postpath) > 1: |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
169 if request.postpath[1] == 'atom.xml': # return the atom feed |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
170 request.atom = True |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
171 else: |
786
f0ad3f62bae2
server_side (blog): pubsub item ID doesn't need to be an UUID
souliane <souliane@mailoo.org>
parents:
760
diff
changeset
|
172 request.item_id = request.postpath[1] |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
173 |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
174 self.parseURLParamsRSM(request) |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
175 request.display_single = (request.item_id is not None) or int(request.extra_dict['rsm_max']) == 1 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
176 self.parseURLParamsCommentsRSM(request) |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
177 |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
178 def parseURLParamsRSM(self, request): |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
179 """Parse RSM request data from the URL parameters for main items |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
180 |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
181 @param request: HTTP request |
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
182 """ |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
183 request.extra_dict = {} |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
184 if request.item_id: # XXX: item_id and RSM are not compatible |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
185 return |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
186 try: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
187 request.extra_dict['rsm_max'] = request.args['max'][0] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
188 except (ValueError, KeyError): |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
189 request.extra_dict['rsm_max'] = unicode(C.RSM_MAX_ITEMS) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
190 try: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
191 request.extra_dict['rsm_index'] = request.args['index'][0] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
192 except (ValueError, KeyError): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
193 try: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
194 request.extra_dict['rsm_before'] = request.args['before'][0] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
195 except KeyError: |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
196 try: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
197 request.extra_dict['rsm_after'] = request.args['after'][0] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
198 except KeyError: |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
199 pass |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
200 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
201 def parseURLParamsCommentsRSM(self, request): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
202 """Parse RSM request data from the URL parameters for comments |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
203 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
204 @param request: HTTP request |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
205 """ |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
206 request.extra_comments_dict = {} |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
207 if request.display_single: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
208 try: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
209 request.extra_comments_dict['rsm_max'] = request.args['comments_max'][0] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
210 except (ValueError, KeyError): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
211 request.extra_comments_dict['rsm_max'] = unicode(C.RSM_MAX_COMMENTS) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
212 else: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
213 request.extra_comments_dict['rsm_max'] = "0" |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
214 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
215 def getItemById(self, pub_jid, item_id, extra_dict, extra_comments_dict, request, profile): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
216 """ |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
217 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
218 @param pub_jid (jid.JID): publisher JID |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
219 @param item_id(unicode): ID of the item to retrieve |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
220 @param extra_dict (dict): extra configuration for initial items only |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
221 @param extra_comments_dict (dict): extra configuration for comments only |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
222 @param request: HTTP request |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
223 @param profile |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
224 """ |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
225 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
226 def gotItems(items): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
227 items, metadata = items |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
228 item = items[0] # assume there's only one item |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
229 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
230 def gotCount(items_bis): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
231 metadata_bis = items_bis[1] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
232 metadata['rsm_count'] = metadata_bis['rsm_count'] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
233 index_key = "rsm_index" if metadata_bis.get("rsm_index") else "rsm_count" |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
234 metadata['rsm_index'] = unicode(int(metadata_bis[index_key]) - 1) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
235 metadata['rsm_first'] = metadata['rsm_last'] = item["id"] |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
236 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
237 def gotComments(comments): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
238 # build the items as self.getItems would do it (and as self.render_html_blog expects them to be) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
239 comments = [(item['comments_service'], item['comments_node'], "", comments[0], comments[1])] |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
240 self.render_html_blog([(item, comments)], metadata, request, pub_jid, profile) |
10 | 241 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
242 # get the comments |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
243 max_comments = int(extra_comments_dict['rsm_max']) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
244 self.host.bridge.mbGet(item['comments_service'], item['comments_node'], max_comments, [], |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
245 extra_comments_dict, C.SERVICE_PROFILE, callback=gotComments) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
246 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
247 # XXX: retrieve RSM information related to the main item. We can't do it while |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
248 # retrieving the item, because item_ids and rsm should not be used together. |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
249 self.host.bridge.mbGet(pub_jid.userhost(), NS_MICROBLOG, 1, [], |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
250 {"rsm_max": "1", "rsm_after": item["id"]}, C.SERVICE_PROFILE, callback=gotCount) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
251 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
252 # get the main item |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
253 self.host.bridge.mbGet(pub_jid.userhost(), NS_MICROBLOG, 1, [item_id], |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
254 extra_dict, C.SERVICE_PROFILE, callback=gotItems) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
255 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
256 def getItems(self, pub_jid, max_items, extra_dict, extra_comments_dict, request, profile): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
257 """ |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
258 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
259 @param pub_jid (jid.JID): publisher JID |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
260 @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
261 @param extra_dict (dict): extra configuration for initial items only |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
262 @param extra_comments_dict (dict): extra configuration for comments only |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
263 @param request: HTTP request |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
264 @param profile |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
265 """ |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
266 def getResultCb(data, rt_session): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
267 remaining, results = data |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
268 for result in results: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
269 service, node, failure, items, metadata = result |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
270 if not failure: |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
271 self.render_html_blog(items, metadata, request, pub_jid, profile) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
272 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
273 if remaining: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
274 self._getResults(rt_session) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
275 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
276 def getResult(rt_session): |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
277 self.host.bridge.mbGetFromManyWithCommentsRTResult(rt_session, C.SERVICE_PROFILE, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
278 callback=lambda data: getResultCb(data, rt_session), |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
279 errback=lambda failure: self.render_error_blog(failure, request, pub_jid)) |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
280 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
281 max_comments = int(extra_comments_dict['rsm_max']) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
282 self.host.bridge.mbGetFromManyWithComments(C.JID, [pub_jid.userhost()], max_items, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
283 max_comments, extra_dict, extra_comments_dict, |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
284 C.SERVICE_PROFILE, callback=getResult) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
285 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
286 def render_html_blog(self, items, metadata, request, pub_jid, profile): |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
287 """Retrieve the user parameters before actually rendering the static blog |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
288 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
289 @param items(list[tuple(dict, list)]): same as in self.__render_html_blog |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
290 @param metadata(dict): original node metadata |
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
|
291 @param request: HTTP request |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
292 @param pub_jid (JID): publisher JID |
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
293 @param profile (unicode): %(doc_profile)s |
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
|
294 """ |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
295 d_list = [] |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
296 options = {} |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
297 |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
298 def getCallback(param_name): |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
299 d = defer.Deferred() |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
300 d.addCallback(lambda value: options.update({param_name: value})) |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
301 d_list.append(d) |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
302 return d.callback |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
303 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
304 eb = lambda failure: self.render_error_blog(failure, request, pub_jid) |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
305 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
306 self.getAvatarURL(pub_jid).addCallbacks(getCallback('avatar'), eb) |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
307 for param_name in (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION): |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
308 self.host.bridge.asyncGetParamA(param_name, C.STATIC_BLOG_KEY, 'value', C.SERVER_SECURITY_LIMIT, profile, callback=getCallback(param_name), errback=eb) |
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
309 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
310 cb = lambda dummy: self.__render_html_blog(items, metadata, options, request, pub_jid) |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
311 defer.DeferredList(d_list).addCallback(cb) |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
312 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
313 def __render_html_blog(self, items, metadata, options, request, pub_jid): |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
314 """Actually render the static blog. |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
315 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
316 If mblog_data is a list of dict, we are missing the comments items so we just |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
317 display the main items. If mblog_data is a list of couple, each couple is |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
318 associating a main item data with the list of its comments, so we render all. |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
319 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
320 @param items(list[tuple(dict, list)]): list of 2-tuple with |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
321 - item(dict): item microblog data |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
322 - comments_list(list[tuple]): list of 5-tuple with |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
323 - service (unicode): pubsub service where the comments node is |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
324 - node (unicode): comments node |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
325 - failure (unicode): empty in case of success, else error message |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
326 - comments(list[dict]): list of microblog data |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
327 - comments_metadata(dict): metadata of the comment node |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
328 @param metadata(dict): original node metadata |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
329 @param options: dict defining the blog's parameters |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
330 @param request: the HTTP request |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
331 @param pub_jid (JID): publisher JID |
388
893451e35686
server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents:
375
diff
changeset
|
332 """ |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
333 if not isinstance(options, dict): |
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
334 options = {} |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
335 user = sanitizeHtml(pub_jid.user) |
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
|
336 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
|
337 base_url = root_url + 'blog/' + user |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
338 |
483
0bbbef1d53a8
server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents:
481
diff
changeset
|
339 def getOption(key): |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
340 return sanitizeHtml(options[key]) if key in options else '' |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
341 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
342 def getImageParams(key, default, alt): |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
343 """regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/""" |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
344 url = options[key] if key in options else '' |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
345 regexp = r"^(https?|ftp)://[a-z0-9-]+(\.[a-z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$" |
702
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
346 if re.match(regexp, url): |
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
347 url = url |
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
348 else: |
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
349 url = default |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
350 return BlogImage(url, alt) |
441
63017904c4d4
server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents:
436
diff
changeset
|
351 |
702
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
352 avatar = os.path.normpath(root_url + getOption('avatar')) |
d94feb0d849e
server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents:
694
diff
changeset
|
353 title = getOption(C.STATIC_BLOG_PARAM_TITLE) or user |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
354 data = {'base_url': base_url, |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
355 'keywords': getOption(C.STATIC_BLOG_PARAM_KEYWORDS), |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
356 'description': getOption(C.STATIC_BLOG_PARAM_DESCRIPTION), |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
357 'title': title, |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
358 'favicon': avatar, |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
359 'banner_img': getImageParams(C.STATIC_BLOG_PARAM_BANNER, avatar, title) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
360 } |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
361 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
362 items.sort(key=lambda entry: (-float(entry[0].get('updated', 0)))) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
363 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
364 data['navlinks'] = NavigationLinks(request, items, metadata, base_url) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
365 data['messages'] = [] |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
366 for item in items: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
367 item, comments_list = item |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
368 comments, comments_count = [], 0 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
369 for node_comments in comments_list: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
370 comments.extend(node_comments[3]) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
371 try: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
372 comments_count += int(node_comments[4]['rsm_count']) |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
373 except KeyError: |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
374 pass |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
375 data['messages'].append(BlogMessage(request, base_url, item, comments, comments_count)) |
586
3eb3a2c0c011
browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents:
487
diff
changeset
|
376 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
377 request.write(self.useTemplate(request, 'static_blog', data)) |
10 | 378 request.finish() |
243
63e9b680d3e7
browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
229
diff
changeset
|
379 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
380 def render_atom_feed(self, feed, request): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
381 request.write(feed.encode('utf-8')) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
382 request.finish() |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
383 |
727
3bd097380da7
server side (blog): fixes displaying the avatar
souliane <souliane@mailoo.org>
parents:
726
diff
changeset
|
384 def render_error_blog(self, error, request, pub_jid): |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
385 request.write(self.useTemplate(request, "static_blog_error", {'message': "Can't access requested data"})) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
386 request.finish() |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
387 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
388 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
389 class NavigationLinks(object): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
390 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
391 def __init__(self, request, items, rsm_data, base_url): |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
392 """Build the navigation links. |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
393 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
394 @param items (list): list of items |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
395 @param rsm_data (dict): rsm data |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
396 @param base_url (unicode): the base URL for this user's blog |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
397 @return: dict |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
398 """ |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
399 for key in ('later_message', 'later_messages', 'older_message', 'older_messages'): |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
400 count = int(rsm_data.get('rsm_count', 0)) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
401 setattr(self, key, '') # key must exist when using the template |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
402 if count <= 0 or (request.display_single == key.endswith('s')): |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
403 continue |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
404 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
405 index = int(rsm_data['rsm_index']) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
406 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
407 link_data = {'base_url': base_url, 'suffix': ''} |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
408 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
409 if key.startswith('later_message'): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
410 if index <= 0: |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
411 continue |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
412 link_data['item_id'] = rsm_data['rsm_first'] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
413 link_data['post_arg'] = 'before' |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
414 else: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
415 if index + len(items) >= count: |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
416 continue |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
417 link_data['item_id'] = rsm_data['rsm_last'] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
418 link_data['post_arg'] = 'after' |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
419 |
708
e9a6cbb924e6
server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents:
705
diff
changeset
|
420 if request.display_single: |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
421 link_data['suffix'] = '&max=1' |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
422 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
423 link = "%(base_url)s?%(post_arg)s=%(item_id)s%(suffix)s" % link_data |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
424 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
425 setattr(self, key, BlogLink(link, key, key.replace('_', ' '))) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
426 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
427 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
428 class BlogImage(object): |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
429 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
430 def __init__(self, url_, alt): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
431 self.url = url_ |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
432 self.alt = alt |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
433 |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
434 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
435 class BlogLink(object): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
436 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
437 def __init__(self, url_, style, text): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
438 self.url = url_ |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
439 self.style = style |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
440 self.text = text |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
441 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
442 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
443 class BlogMessage(object): |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
444 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
445 def __init__(self, request, base_url, entry, comments=None, comments_count=0): |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
446 """ |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
447 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
448 @param request: HTTP request |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
449 @param base_url (unicode): the base URL |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
450 @param entry(dict): item microblog data |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
451 @param comments(list[dict]): list of microblog data |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
452 @param comments_count (int): total number of comments |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
453 """ |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
454 timestamp = float(entry.get('published', 0)) |
746
25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
Goffi <goffi@goffi.org>
parents:
743
diff
changeset
|
455 |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
456 # FIXME: for now we assume that the comments' depth is only 1 |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
457 is_comment = not entry.get('comments', False) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
458 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
459 self.date = datetime.fromtimestamp(timestamp) |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
460 self.type = "comment" if is_comment else "main_item" |
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
461 self.style = 'mblog_comment' if is_comment else '' |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
462 self.content = self.getText(entry, 'content') |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
463 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
464 if is_comment: |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
465 self.author = (_("from %s") % entry['author']) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
466 else: |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
467 self.author = ' ' |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
468 self.url = (u"%s/%s" % (base_url, entry['id'])) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
469 self.title = self.getText(entry, 'title') |
760
78caef6ed8e7
static blog: replaced mbdata2tags by sat.tools.common.dict2iter
Goffi <goffi@goffi.org>
parents:
749
diff
changeset
|
470 self.tags = list(common.dict2iter('tag', entry)) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
471 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
472 count_text = lambda count: D_('comments') if count > 1 else D_('comment') |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
473 |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
474 self.comments_text = "%s %s" % (comments_count, count_text(comments_count)) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
475 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
476 delta = comments_count - len(comments) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
477 if request.display_single and delta > 0: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
478 prev_url = "%s?comments_max=%s" % (self.url, unicode(comments_count)) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
479 prev_text = D_("show %(count)d previous %(comments)s") % \ |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
480 {'count': delta, 'comments': count_text(delta)} |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
481 self.all_comments_link = BlogLink(prev_url, "comments_link", prev_text) |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
482 |
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
483 if comments: |
725
c1abaa91a121
server (blog): update to the new mechanism in XEP-0277 + improvement:
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
484 comments.sort(key=lambda comment: float(comment.get('published', 0))) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
708
diff
changeset
|
485 self.comments = [BlogMessage(request, base_url, comment) for comment in comments] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
486 |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
487 def getText(self, entry, key): |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
488 if ('%s_xhtml' % key) in entry: |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
489 return entry['%s_xhtml' % key] |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
490 elif key in entry: |
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
491 processor = addURLToText if key.startswith('content') else sanitizeHtml |
726
e949b7c7ed9c
server side (blog): fixes unicode error
souliane <souliane@mailoo.org>
parents:
725
diff
changeset
|
492 return convertNewLinesToXHTML(processor(entry[key])) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
702
diff
changeset
|
493 return None |