annotate src/server/blog.py @ 708:e9a6cbb924e6

server_side: fixes static blog navigation links
author souliane <souliane@mailoo.org>
date Mon, 08 Jun 2015 08:51:41 +0200
parents 531eacb82e9f
children bf562fb9c273
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
685
9877607c719a 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 679
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014, 2015 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
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
20 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
21 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
22 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
23 log = getLogger(__name__)
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 446
diff changeset
24
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
25 from twisted.internet import defer
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.web import server
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.web.resource import Resource
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.words.protocols.jabber.jid import JID
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from datetime import datetime
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
30 from sys import path
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
31 import importlib
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
32 import uuid
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
33 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
34 import os
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
35
588
c8cca1a373dd server_side: static blog: convert \n in raw text message to <br/>
souliane <souliane@mailoo.org>
parents: 586
diff changeset
36 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
37 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
38
229
e632f77c4219 bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents: 228
diff changeset
39
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
40 class TemplateProcessor(object):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
41
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
42 THEME = 'default'
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
43
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
44 def __init__(self, host):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
45 self.host = host
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 # add Libervia's themes directory to the python path
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
48 path.append(os.path.dirname(self.host.themes_dir))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
49
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
50 def useTemplate(self, request, tpl, data=None):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
51 root_url = '../' * len(request.postpath)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
52 theme_url = os.path.join(root_url, 'themes', self.THEME)
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
53
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
54 # import the theme module
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
55 themes = os.path.basename(os.path.dirname(os.path.dirname(self.host.themes_dir)))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
56 theme = importlib.import_module("%s.templates" % self.THEME, themes)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
57 data_ = {'theme': theme_url,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
58 'images': os.path.join(theme_url, 'images'),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
59 'styles': os.path.join(theme_url, 'styles'),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
60 }
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
61 if data:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
62 data_.update(data)
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
63 return getattr(theme, tpl.upper()).encode('utf-8').format(**data_)
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
64
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
65
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
66 class MicroBlog(Resource, TemplateProcessor):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
67 isLeaf = True
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
68
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
69 def __init__(self, host):
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.host = host
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
71 Resource.__init__(self)
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
72 TemplateProcessor.__init__(self, host)
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
73 self.host.bridge.register('entityDataUpdated', self.entityDataUpdatedCb)
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
74 self.host.bridge.register('actionResult', self.actionResultCb) # FIXME: actionResult is to be removed
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
75 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
76 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
77
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
78 def entityDataUpdatedCb(self, entity_jid_s, key, value, dummy):
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
79 """Retrieve the avatar we've been waiting for and fires the callback
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
80 for self.getAvatar to return.
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
81
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
82 @param entity_jid_s (str): JID of the contact
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
83 @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
84 @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
85 @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
86 """
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
87 if key != 'avatar':
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
88 return
487
e588335b6aa8 browser and server side: force lower case for the profile name at new account creation + fixes CSS for textarea
souliane <souliane@mailoo.org>
parents: 484
diff changeset
89 entity_jid_s = entity_jid_s.lower()
694
82123705474b massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 685
diff changeset
90 log.debug(_(u"Received a new avatar for entity %s") % entity_jid_s)
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
91 avatar = C.AVATARS_DIR + value
ae86b32b959c server side (blog): manage our own avatars cache because existing entity data are not updated
souliane <souliane@mailoo.org>
parents: 483
diff changeset
92 self.avatars_cache[entity_jid_s] = 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
93 try:
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
94 self.waiting_deferreds[entity_jid_s][1].callback(avatar)
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
95 del self.waiting_deferreds[entity_jid_s]
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
96 except KeyError:
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
97 pass
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
98
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
99 def actionResultCb(self, answer_type, action_id, data, dummy):
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
100 """Fires the callback for self.getAvatar to return
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
101
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
102 @param answer_type (str): 'SUPPRESS' or another value that we would ignore
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
103 @param action_id (str): the request ID
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
104 @param data (dict): ignored
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
105 @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
106 """
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
107 # FIXME: actionResult is to be removed. For now we use it to get notified
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
108 # when the requested vCard hasn't been found. Replace with the new system.
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
109 if answer_type != 'SUPPRESS':
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
110 return
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
111 try:
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
112 entity_jid_s = [key for (key, value) in self.waiting_deferreds.items() if value[0] == action_id][0]
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
113 except IndexError: # impossible to guess the entity
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
114 return
694
82123705474b massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 685
diff changeset
115 log.debug(_(u"Using default avatar for entity %s") % entity_jid_s)
606
7af8f4ab3675 browser side: fixed avatar getting + better DEFAULT/EMPTY avatars constants + use of jid.JID for microblog's author
Goffi <goffi@goffi.org>
parents: 487
diff changeset
116 self.avatars_cache[entity_jid_s] = C.DEFAULT_AVATAR_URL
7af8f4ab3675 browser side: fixed avatar getting + better DEFAULT/EMPTY avatars constants + use of jid.JID for microblog's author
Goffi <goffi@goffi.org>
parents: 487
diff changeset
117 self.waiting_deferreds[entity_jid_s][1].callback(C.DEFAULT_AVATAR_URL)
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
118 del self.waiting_deferreds[entity_jid_s]
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
119
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
120 def getAvatar(self, profile):
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
121 """Get the avatar of the given profile
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
122
606
7af8f4ab3675 browser side: fixed avatar getting + better DEFAULT/EMPTY avatars constants + use of jid.JID for microblog's author
Goffi <goffi@goffi.org>
parents: 487
diff changeset
123 @param profile(unicode): %(doc_profile)s
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
124 @return: deferred avatar path, relative to the server's root
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
125 """
487
e588335b6aa8 browser and server side: force lower case for the profile name at new account creation + fixes CSS for textarea
souliane <souliane@mailoo.org>
parents: 484
diff changeset
126 jid_s = (profile + '@' + self.host.bridge.getNewAccountDomain()).lower()
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
127 if jid_s in self.avatars_cache:
ae86b32b959c server side (blog): manage our own avatars cache because existing entity data are not updated
souliane <souliane@mailoo.org>
parents: 483
diff changeset
128 return defer.succeed(self.avatars_cache[jid_s])
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
129 # FIXME: request_id is no more need when actionResult is removed
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
130 request_id = self.host.bridge.getCard(jid_s, 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
131 self.waiting_deferreds[jid_s] = (request_id, defer.Deferred())
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
132 return self.waiting_deferreds[jid_s][1]
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
133
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def render_GET(self, request):
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if not request.postpath:
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
136 return self.useTemplate(request, "error", {'message': "You must indicate a nickname"})
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
137
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
138 prof_requested = request.postpath[0]
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
139 #TODO: char check: only use alphanumerical chars + some extra(_,-,...) here
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
140 prof_found = self.host.bridge.getProfileName(prof_requested)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
141 if not prof_found or prof_found == C.SERVICE_PROFILE:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
142 return self.useTemplate(request, "error", {'message': "Invalid nickname"})
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
143
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
144 d = defer.Deferred()
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
145 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', C.SERVER_SECURITY_LIMIT, prof_found, callback=d.callback, errback=d.errback))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
146 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
147 return server.NOT_DONE_YET
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
148
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
149 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
150 pub_jid = JID(pub_jid_s)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
151 d = defer.Deferred()
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
152
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
153 self.parseURLParams(request)
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
154 item_id, rsm_ = request.item_id, request.rsm_data
705
531eacb82e9f browser and server sides: renamed max to max_ after sat's changeset 1423 (882e5fabf68c)
souliane <souliane@mailoo.org>
parents: 704
diff changeset
155 max_items = int(rsm_['max_'])
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
156
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
157 if request.atom:
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
158 d.addCallbacks(self.render_atom_feed, self.render_error_blog, [request], None, [request, profile], None)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
159 self.host.bridge.getGroupBlogsAtom(pub_jid.userhost(), rsm_, C.SERVICE_PROFILE, d.callback, d.errback)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
160 return
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
161
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
162 d.addCallbacks(self.render_html_blog, self.render_error_blog, [request, profile], None, [request, profile], None)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
163 if item_id:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
164 if max_items > 0: # display one message and its comments
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
165 self.host.bridge.getGroupBlogsWithComments(pub_jid.userhost(), [item_id], {}, max_items, C.SERVICE_PROFILE, d.callback, d.errback)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
166 else: # display one message, count its comments
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
167 self.host.bridge.getGroupBlogs(pub_jid.userhost(), [item_id], {}, True, C.SERVICE_PROFILE, d.callback, d.errback)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
168 else:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
169 if max_items == 1: # display one message and its comments
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
170 self.host.bridge.getGroupBlogsWithComments(pub_jid.userhost(), [], rsm_, C.RSM_MAX_COMMENTS, C.SERVICE_PROFILE, d.callback, d.errback)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
171 else: # display the last messages, count their comments
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
172 self.host.bridge.getGroupBlogs(pub_jid.userhost(), [], rsm_, True, C.SERVICE_PROFILE, d.callback, d.errback)
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
173
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
174 def parseURLParams(self, request):
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
175 """Parse the request URL parameters.
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
176
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
177 @param request: HTTP request
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
178 """
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
179 request.item_id = None
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
180 request.atom = False
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
181
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
182 if len(request.postpath) > 1:
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
183 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
184 request.atom = True
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
185 else:
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
186 try: # check if the given path is a valid UUID
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
187 uuid.UUID(request.postpath[1])
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
188 request.item_id = request.postpath[1]
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
189 except ValueError:
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
190 pass
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
191
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
192 self.parseURLParamsRSM(request)
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
193 request.display_single = (request.item_id is not None) or int(request.rsm_data['max_']) == 1
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
194
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
195 def parseURLParamsRSM(self, request):
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
196 """Parse RSM request data from the URL parameters.
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
197
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
198 @param request: HTTP request
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
199 """
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
200 rsm_ = {}
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
201 try:
705
531eacb82e9f browser and server sides: renamed max to max_ after sat's changeset 1423 (882e5fabf68c)
souliane <souliane@mailoo.org>
parents: 704
diff changeset
202 rsm_['max_'] = request.args['max'][0]
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
203 except (ValueError, KeyError):
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
204 rsm_['max_'] = unicode(C.RSM_MAX_ITEMS if request.item_id else C.RSM_MAX_COMMENTS)
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
205 try:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
206 rsm_['index'] = request.args['index'][0]
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
207 except (ValueError, KeyError):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
208 try:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
209 rsm_['before'] = request.args['before'][0]
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
210 except KeyError:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
211 try:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
212 rsm_['after'] = request.args['after'][0]
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
213 except KeyError:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
214 pass
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
215 request.rsm_data = rsm_
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
216
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
217 def render_html_blog(self, mblog_data, request, 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
218 """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
219
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
220 @param mblog_data (list): couple (list, dict) with:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
221 - a list of microblog data, or a list of couple containing:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
222 - microblog data (main item)
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
223 - couple (comments data, RSM response data for the comments)
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
224 - RSM response data for the main items
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
225 @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
226 @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
227 """
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
228 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
229 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
230
63017904c4d4 server_side: static blog retrieve page's title, meta description and keywords from the parameters
souliane <souliane@mailoo.org>
parents: 436
diff changeset
231 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
232 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
233 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
234 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
235 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
236
388
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
237 eb = lambda failure: self.render_error_blog(failure, request, 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
238
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
239 self.getAvatar(profile).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
240 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
241 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
242
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
243 cb = lambda dummy: self.__render_html_blog(mblog_data, options, request, 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
244 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
245
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
246 def __render_html_blog(self, mblog_data, options, request, profile):
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
247 """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
248 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
249 each couple is 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
250
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
251 @param mblog_data (list): couple (list, dict) with:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
252 - a list of microblog data, or a list of couple containing:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
253 - microblog data (main item)
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
254 - couple (comments data, RSM response data for the comments)
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
255 - RSM response data for the main items
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
256 @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
257 @param request: the HTTP request
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
258 @profile
893451e35686 server_side: display blog banner if it is defined
souliane <souliane@mailoo.org>
parents: 375
diff changeset
259 """
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
260 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
261 options = {}
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
262 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
263 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
264 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
265
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
266 def getOption(key):
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
267 return sanitizeHtml(options[key]).encode('utf-8') 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
268
702
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
269 def getImageOption(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
270 """regexp from http://answers.oreilly.com/topic/280-how-to-validate-urls-with-regular-expressions/"""
483
0bbbef1d53a8 server side (blog): use user's avatar a the blog's favicon + small refactorization
souliane <souliane@mailoo.org>
parents: 481
diff changeset
271 url = options[key].encode('utf-8') 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
272 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
273 if re.match(regexp, url):
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
274 url = url
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
275 suffix = "<br/>"
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
276 else:
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
277 url = default
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
278 suffix = ""
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
279 return self.useTemplate(request, "banner", {'alt': alt, 'url': url, 'suffix': suffix})
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
280
702
d94feb0d849e server_side: give a better look to the static blog
souliane <souliane@mailoo.org>
parents: 694
diff changeset
281 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
282 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
283 data = {'base_url': base_url,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
284 'user': user,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
285 'keywords': getOption(C.STATIC_BLOG_PARAM_KEYWORDS),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
286 'description': getOption(C.STATIC_BLOG_PARAM_DESCRIPTION),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
287 'title': getOption(C.STATIC_BLOG_PARAM_TITLE) or "%s's microblog" % user,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
288 'favicon': avatar,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
289 'banner_elt': getImageOption(C.STATIC_BLOG_PARAM_BANNER, avatar, title),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
290 'title_elt': title,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
291 }
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
292
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
293 mblog_data, main_rsm = mblog_data
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
294 mblog_data = [(entry if isinstance(entry, tuple) else (entry, ([], {}))) for entry in mblog_data]
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
295 mblog_data = sorted(mblog_data, 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
296
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
297 data.update(self.getNavigationLinks(request, mblog_data, main_rsm, base_url))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
298 request.write(self.useTemplate(request, 'header', data))
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
299
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
300 BlogMessages(self.host, request, base_url, mblog_data).render()
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
301
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
302 request.write(self.useTemplate(request, "footer", data))
10
c28a4840e1a8 server: microblog resource
Goffi <goffi@goffi.org>
parents:
diff changeset
303 request.finish()
243
63e9b680d3e7 browser_side, blog: better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 229
diff changeset
304
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
305 def getNavigationLinks(self, request, mblog_data, rsm_data, base_url):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
306 """Build the navigation links.
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
307
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
308 @param mblog_data (dict): the microblogs that are displayed on the page
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
309 @param rsm_data (dict): rsm data
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
310 @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
311 @return: dict
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
312 """
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
313 data = {}
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
314 for key in ('later_message', 'later_messages', 'older_message', 'older_messages'):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
315 count = int(rsm_data.get('count', 0))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
316 data[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
317 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
318 continue
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
319
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
320 index = int(rsm_data['index'])
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
321
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
322 link_data = {'base_url': base_url, 'suffix': ''}
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
323
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
324 if key.startswith('later_message'):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
325 if index <= 0:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
326 continue
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
327 link_data['item_id'] = rsm_data['first']
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
328 link_data['post_arg'] = 'before'
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
329 else:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
330 if index + len(mblog_data) >= count:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
331 continue
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
332 link_data['item_id'] = rsm_data['last']
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
333 link_data['post_arg'] = 'after'
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
334
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
335 if request.display_single:
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
336 link_data['suffix'] = '&max=1'
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
337
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
338 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
339
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
340 link_data = {'link': link, 'class': key, 'text': key.replace('_', ' ')}
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
341 data[key] = (self.useTemplate(request, 'nav_link', link_data)).encode('utf-8')
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
342
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
343 return data
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
344
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
345 def render_atom_feed(self, feed, request):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
346 request.write(feed.encode('utf-8'))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
347 request.finish()
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
348
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
349 def render_error_blog(self, error, request, profile):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
350 request.write(self.useTemplate(request, "error", {'message': "Can't access requested data"}))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
351 request.finish()
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
352
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
353
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
354 class BlogMessages(TemplateProcessor):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
355
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
356 def __init__(self, host, request, base_url, mblog_data):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
357 TemplateProcessor.__init__(self, host)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
358 self.request = request
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
359 self.base_url = base_url
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
360 self.mblog_data = mblog_data
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
361
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
362 def render(self):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
363 for entry, comments_data in self.mblog_data:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
364 comments, comments_rsm = comments_data
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
365 comments = sorted(comments, key=lambda entry: (float(entry.get('published', 0))))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
366 self.render_html(entry, comments)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
367
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
368 def getText(self, entry, key):
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
369 if ('%s_xhtml' % key) in entry:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
370 return entry['%s_xhtml' % key].encode('utf-8')
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
371 elif key in entry:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
372 processor = addURLToText if key.startswith('content') else sanitizeHtml
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
373 return convertNewLinesToXHTML(processor(entry[key])).encode('utf-8')
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
374 return None
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
375
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
376 def render_html(self, entry, comments=None):
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
377 """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
378 @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
379 @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
380 @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
381 """
2d782349b88a server_side: display blog comments when you click on a main item header or title
souliane <souliane@mailoo.org>
parents: 388
diff changeset
382 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
383 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
384
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
385 data = {'date': datetime.fromtimestamp(timestamp),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
386 'comments_link': '',
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
387 'previous_comments': '',
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
388 }
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
389
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
390 if is_comment:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
391 author = (_("from %s") % entry['author']).encode('utf-8')
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
392 else:
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
393 author = '&nbsp;'
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
394 message_link = (u"%s/%s" % (self.base_url, entry['id'])).encode('utf-8')
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
395
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
396 count_text = lambda count: D_('comments') if count > 1 else D_('comment')
586
3eb3a2c0c011 browser and server side: uses RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 487
diff changeset
397
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
398 comments_count = int(entry['comments_count'])
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
399 delta = comments_count - len(comments)
708
e9a6cbb924e6 server_side: fixes static blog navigation links
souliane <souliane@mailoo.org>
parents: 705
diff changeset
400 if self.request.display_single and delta > 0:
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
401 data['comments_link'] = ("%s?max=%s" % (message_link, entry['comments_count']))
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
402 data['previous_comments'] = D_("Show %(count)d previous %(comments)s") % \
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
403 {'count': delta, 'comments': count_text(delta)}
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
404
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
405 data.update({'comments_count': comments_count,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
406 'comments_text': count_text(comments_count),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
407 'message_link': message_link,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
408 'message_title': self.getText(entry, 'title'),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
409 })
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
410
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
411 data.update({'author': author,
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
412 'extra_style': 'mblog_comment' if entry['type'] == 'comment' else '',
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
413 'content': self.getText(entry, 'content'),
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
414 })
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
415
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
416 tpl = "%s%s" % ("" if data.get('message_title', None) else "micro_", "comment" if is_comment else "message")
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
417 self.request.write(self.useTemplate(self.request, tpl, data))
344
d7b98e499935 server_side: Atom feed for blogposts is accessible from <host>/blog/<user>/atom.xml
souliane <souliane@mailoo.org>
parents: 340
diff changeset
418
704
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
419 if comments:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
420 for comment in comments:
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
421 self.render_html(comment)
5319110a862c server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents: 702
diff changeset
422