Mercurial > libervia-web
annotate libervia/server/pages.py @ 1157:64952ba7affe
pages: fixed children retrieval in onFileChange
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 23 Feb 2019 21:04:31 +0100 |
parents | 94f9d81a475e |
children | 6424d3684d1e |
rev | line source |
---|---|
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
1 #!/usr/bin/python |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
2 # -*- coding: utf-8 -*- |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
3 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
336
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
1144 | 5 # Copyright (C) 2011-2019 Jérôme Poisson <goffi@goffi.org> |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
6 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
336
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:
336
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:
336
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:
336
diff
changeset
|
10 # (at your option) any later version. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
11 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
336
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:
336
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:
336
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:
336
diff
changeset
|
15 # GNU Affero General Public License for more details. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
16 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
336
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:
336
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
19 from twisted.web import server |
858 | 20 from twisted.web import resource as web_resource |
21 from twisted.web import util as web_util | |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
22 from twisted.internet import defer |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
23 from twisted.words.protocols.jabber import jid |
917 | 24 from twisted.python import failure |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
448
diff
changeset
|
25 |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
26 from sat.core.i18n import _ |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
27 from sat.core import exceptions |
1109
3a7b2b239d3e
pages: change module for date_parse, following backend change
Goffi <goffi@goffi.org>
parents:
1092
diff
changeset
|
28 from sat.tools.common import date_utils |
438
582c435dab6b
server side: new log system is used
Goffi <goffi@goffi.org>
parents:
435
diff
changeset
|
29 from sat.core.log import getLogger |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
30 |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
31 from libervia.server.constants import Const as C |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
32 from libervia.server import session_iface |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
33 from libervia.server.utils import quote, SubPage |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
34 |
995 | 35 from collections import namedtuple |
36 import uuid | |
984
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
37 import os.path |
f0fc28b3bd1e
server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents:
980
diff
changeset
|
38 import urllib |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
39 import time |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
40 import hashlib |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
41 |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
42 log = getLogger(__name__) |
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
43 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
44 WebsocketMeta = namedtuple("WebsocketMeta", ("url", "token", "debug")) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
45 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
46 |
1018 | 47 class CacheBase(object): |
48 def __init__(self): | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
49 self._created = time.time() |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
50 self._last_access = self._created |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
51 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
52 @property |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
53 def created(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
54 return self._created |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
55 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
56 @property |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
57 def last_access(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
58 return self._last_access |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
59 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
60 @last_access.setter |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
61 def last_access(self, timestamp): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
62 self._last_access = timestamp |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
63 |
1018 | 64 |
65 class CachePage(CacheBase): | |
66 def __init__(self, rendered): | |
67 super(CachePage, self).__init__() | |
68 self._created = time.time() | |
69 self._last_access = self._created | |
70 self._rendered = rendered | |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
71 self._hash = hashlib.sha256(rendered).hexdigest() |
1018 | 72 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
73 @property |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
74 def rendered(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
75 return self._rendered |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
76 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
77 @property |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
78 def hash(self): |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
79 return self._hash |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
80 |
1018 | 81 |
82 class CacheURL(CacheBase): | |
83 def __init__(self, request): | |
84 super(CacheURL, self).__init__() | |
85 try: | |
86 self._data = request.data.copy() | |
87 except AttributeError: | |
88 self._data = {} | |
89 self._template_data = request.template_data.copy() | |
90 self._prepath = request.prepath[:] | |
91 self._postpath = request.postpath[:] | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
92 del self._template_data["csrf_token"] |
1018 | 93 |
94 def use(self, request): | |
95 self.last_access = time.time() | |
96 request.data = self._data.copy() | |
97 request.template_data.update(self._template_data) | |
98 request.prepath = self._prepath[:] | |
99 request.postpath = self._postpath[:] | |
100 | |
101 | |
917 | 102 class LiberviaPage(web_resource.Resource): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
103 isLeaf = True # we handle subpages ourself |
995 | 104 signals_handlers = {} |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
105 cache = {} |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
106 # Set of tuples (service/node/sub_id) of nodes subscribed for caching |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
107 # sub_id can be empty string if not handled by service |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
108 cache_pubsub_sub = set() |
917 | 109 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
110 def __init__( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
111 self, host, vhost_root, root_dir, url, name=None, redirect=None, access=None, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
112 dynamic=False, parse_url=None, prepare_render=None, render=None, template=None, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
113 on_data_post=None, on_data=None, on_signal=None, url_cache=False, |
1153 | 114 replace_on_conflict=False |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
115 ): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
116 """Initiate LiberviaPage instance |
917 | 117 |
118 LiberviaPages are the main resources of Libervia, using easy to set python files | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
119 The non mandatory arguments are the variables found in page_meta.py |
917 | 120 @param host(Libervia): the running instance of Libervia |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
121 @param vhost_root(web_resource.Resource): root resource of the virtual host which |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
122 handle this page. |
962 | 123 @param root_dir(unicode): aboslute file path of the page |
124 @param url(unicode): relative URL to the page | |
125 this URL may not be valid, as pages may require path arguments | |
917 | 126 @param name(unicode, None): if not None, a unique name to identify the page |
127 can then be used for e.g. redirection | |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
128 "/" is not allowed in names (as it can be used to construct URL paths) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
129 @param redirect(unicode, None): if not None, this page will be redirected. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
130 A redirected parameter is used as in self.pageRedirect. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
131 parse_url will not be skipped |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
132 using this redirect parameter is called "full redirection" |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
133 using self.pageRedirect is called "partial redirection" (because some |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
134 rendering method can still be used, e.g. parse_url) |
917 | 135 @param access(unicode, None): permission needed to access the page |
136 None means public access. | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
137 Pages inherit from parent pages: e.g. if a "settings" page is restricted |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
138 to admins, and if "settings/blog" is public, it still can only be accessed by |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
139 admins. See C.PAGES_ACCESS_* for details |
995 | 140 @param dynamic(bool): if True, activate websocket for bidirectional communication |
917 | 141 @param parse_url(callable, None): if set it will be called to handle the URL path |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
142 after this method, the page will be rendered if noting is left in path |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
143 (request.postpath) else a the request will be transmitted to a subpage |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
144 @param prepare_render(callable, None): if set, will be used to prepare the |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
145 rendering. That often means gathering data using the bridge |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
146 @param render(callable, None): if not template is set, this method will be |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
147 called and what it returns will be rendered. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
148 This method is mutually exclusive with template and must return a unicode |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
149 string. |
917 | 150 @param template(unicode, None): path to the template to render. |
151 This method is mutually exclusive with render | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
152 @param on_data_post(callable, None): method to call when data is posted |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
153 None if not post is handled |
962 | 154 on_data_post can return a string with following value: |
155 - C.POST_NO_CONFIRM: confirm flag will not be set | |
995 | 156 @param on_data(callable, None): method to call when dynamic data is sent |
157 this method is used with Libervia's websocket mechanism | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
158 @param on_signal(callable, None): method to call when a registered signal is |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
159 received. This method is used with Libervia's websocket mechanism |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
160 @param url_cache(boolean): if set, result of parse_url is cached (per profile). |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
161 Useful when costly calls (e.g. network) are done while parsing URL. |
1153 | 162 @param replace_on_conflict(boolean): if True, don't raise ConflictError if a |
163 page of this name already exists, but replace it | |
917 | 164 """ |
165 | |
166 web_resource.Resource.__init__(self) | |
167 self.host = host | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
168 self.vhost_root = vhost_root |
917 | 169 self.root_dir = root_dir |
962 | 170 self.url = url |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
171 self.name = name |
917 | 172 if name is not None: |
1153 | 173 if (name in self.named_pages |
174 and not (replace_on_conflict and self.named_pages[name].url == url)): | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
175 raise exceptions.ConflictError( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
176 _(u'a Libervia page named "{}" already exists'.format(name))) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
177 if u"/" in name: |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
178 raise ValueError(_(u'"/" is not allowed in page names')) |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
179 if not name: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
180 raise ValueError(_(u"a page name can't be empty")) |
917 | 181 self.named_pages[name] = self |
182 if access is None: | |
183 access = C.PAGES_ACCESS_PUBLIC | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
184 if access not in ( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
185 C.PAGES_ACCESS_PUBLIC, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
186 C.PAGES_ACCESS_PROFILE, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
187 C.PAGES_ACCESS_NONE, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
188 ): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
189 raise NotImplementedError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
190 _(u"{} access is not implemented yet").format(access) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
191 ) |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
192 self.access = access |
995 | 193 self.dynamic = dynamic |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
194 if redirect is not None: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
195 # only page access and name make sense in case of full redirection |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
196 # so we check that rendering methods/values are not set |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
197 if not all( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
198 lambda x: x is not None |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
199 for x in (parse_url, prepare_render, render, template) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
200 ): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
201 raise ValueError( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
202 _(u"you can't use full page redirection with other rendering" |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
203 u"method, check self.pageRedirect if you need to use them")) |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
204 self.redirect = redirect |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
205 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
206 self.redirect = None |
917 | 207 self.parse_url = parse_url |
208 self.prepare_render = prepare_render | |
209 self.template = template | |
210 self.render_method = render | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
211 self.on_data_post = on_data_post |
995 | 212 self.on_data = on_data |
213 self.on_signal = on_signal | |
1018 | 214 self.url_cache = url_cache |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
215 if access == C.PAGES_ACCESS_NONE: |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
216 # none pages just return a 404, no further check is needed |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
217 return |
1059
d127a85b2fee
pages: don't log error anymore when empty page_meta.py are used
Goffi <goffi@goffi.org>
parents:
1056
diff
changeset
|
218 if template is not None and render is not None: |
d127a85b2fee
pages: don't log error anymore when empty page_meta.py are used
Goffi <goffi@goffi.org>
parents:
1056
diff
changeset
|
219 log.error(_(u"render and template methods can't be used at the same time")) |
917 | 220 if parse_url is not None and not callable(parse_url): |
221 log.error(_(u"parse_url must be a callable")) | |
222 | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
223 # if not None, next rendering will be cached |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
224 # it must then contain a list of the the keys to use (without the page instance) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
225 # e.g. [C.SERVICE_PROFILE, "pubsub", server@example.tld, pubsub_node] |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
226 self._do_cache = None |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
227 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
228 def __unicode__(self): |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
229 return u"LiberviaPage {name} at {url} (vhost: {vhost_root})".format( |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
230 name=self.name or u"<anonymous>", url=self.url, vhost_root=self.vhost_root) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
231 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
232 def __str__(self): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
233 return self.__unicode__().encode("utf-8") |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
234 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
235 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
236 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
237 def named_pages(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
238 return self.vhost_root.named_pages |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
239 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
240 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
241 def uri_callbacks(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
242 return self.vhost_root.uri_callbacks |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
243 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
244 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
245 def pages_redirects(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
246 return self.vhost_root.pages_redirects |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
247 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
248 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
249 def cached_urls(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
250 return self.vhost_root.cached_urls |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
251 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
252 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
253 def main_menu(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
254 return self.vhost_root.main_menu |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
255 |
1153 | 256 @staticmethod |
257 def createPage(host, meta_path, vhost_root, url_elts, replace_on_conflict=False): | |
258 """Create a LiberviaPage instance | |
259 | |
260 @param meta_path(unicode): path to the page_meta.py file | |
261 @param vhost_root(resource.Resource): root resource of the virtual host | |
262 @param url_elts(list[unicode]): list of path element from root site to this page | |
263 @param replace_on_conflict(bool): same as for [LiberviaPage] | |
264 @return (tuple[dict, LiberviaPage]): tuple with: | |
265 - page_data: dict containing data of the page | |
266 - libervia_page: created resource | |
267 """ | |
268 dir_path = os.path.dirname(meta_path) | |
269 page_data = {"__name__": u".".join([u"page"] + url_elts)} | |
270 # we don't want to force the presence of __init__.py | |
271 # so we use execfile instead of import. | |
272 # TODO: when moved to Python 3, __init__.py is not mandatory anymore | |
273 # so we can switch to import | |
274 execfile(meta_path, page_data) | |
275 return page_data, LiberviaPage( | |
276 host=host, | |
277 vhost_root=vhost_root, | |
278 root_dir=dir_path, | |
279 url=u"/" + u"/".join(url_elts), | |
280 name=page_data.get(u"name"), | |
281 redirect=page_data.get(u"redirect"), | |
282 access=page_data.get(u"access"), | |
283 dynamic=page_data.get(u"dynamic", False), | |
284 parse_url=page_data.get(u"parse_url"), | |
285 prepare_render=page_data.get(u"prepare_render"), | |
286 render=page_data.get(u"render"), | |
287 template=page_data.get(u"template"), | |
288 on_data_post=page_data.get(u"on_data_post"), | |
289 on_data=page_data.get(u"on_data"), | |
290 on_signal=page_data.get(u"on_signal"), | |
291 url_cache=page_data.get(u"url_cache", False), | |
292 replace_on_conflict=replace_on_conflict | |
293 ) | |
294 | |
925 | 295 @classmethod |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
296 def importPages(cls, host, vhost_root, root_path=None, _parent=None, _path=None, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
297 _extra_pages=False): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
298 """Recursively import Libervia pages |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
299 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
300 @param host(Libervia): Libervia instance |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
301 @param vhost_root(LiberviaRootResource): root of this VirtualHost |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
302 @param root_path(unicode, None): use this root path instead of vhost_root's one |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
303 Used to add default site pages to external sites |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
304 @param _parent(Resource, None): _parent page. Do not set yourself, this is for |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
305 internal use only |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
306 @param _path(list(unicode), None): current path. Do not set yourself, this is for |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
307 internal use only |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
308 @param _extra_pages(boolean): set to True when extra pages are used (i.e. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
309 root_path is set). Do not set yourself, this is for internal use only |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
310 """ |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
311 if _path is None: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
312 _path = [] |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
313 if _parent is None: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
314 if root_path is None: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
315 root_dir = os.path.join(vhost_root.site_path, C.PAGES_DIR) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
316 else: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
317 root_dir = os.path.join(root_path, C.PAGES_DIR) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
318 _extra_pages = True |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
319 _parent = vhost_root |
917 | 320 else: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
321 root_dir = _parent.root_dir |
917 | 322 for d in os.listdir(root_dir): |
323 dir_path = os.path.join(root_dir, d) | |
324 if not os.path.isdir(dir_path): | |
325 continue | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
326 if _extra_pages and d in _parent.children: |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
327 log.debug(_(u"[{host_name}] {path} is already present, ignoring it") |
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
328 .format(host_name=vhost_root.host_name, path=u'/'.join(_path+[d]))) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
329 continue |
917 | 330 meta_path = os.path.join(dir_path, C.PAGES_META_FILE) |
331 if os.path.isfile(meta_path): | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
332 new_path = _path + [d] |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
333 try: |
1153 | 334 page_data, resource = cls.createPage(host, meta_path, vhost_root, new_path) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
335 except exceptions.ConflictError as e: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
336 if _extra_pages: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
337 # extra pages are discarded if there is already an existing page |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
338 continue |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
339 else: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
340 raise e |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
341 _parent.putChild(d, resource) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
342 log_msg = (u"[{host_name}] Added /{path} page".format( |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
343 host_name=vhost_root.host_name, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
344 path=u"[…]/".join(new_path))) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
345 if _extra_pages: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
346 log.debug(log_msg) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
347 else: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
348 log.info(log_msg) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
349 if "uri_handlers" in page_data: |
925 | 350 if not isinstance(page_data, dict): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
351 log.error(_(u"uri_handlers must be a dict")) |
925 | 352 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
353 for uri_tuple, cb_name in page_data["uri_handlers"].iteritems(): |
925 | 354 if len(uri_tuple) != 2 or not isinstance(cb_name, basestring): |
355 log.error(_(u"invalid uri_tuple")) | |
356 continue | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
357 if not _extra_pages: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
358 log.info(_(u"setting {}/{} URIs handler") |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
359 .format(*uri_tuple)) |
925 | 360 try: |
361 cb = page_data[cb_name] | |
362 except KeyError: | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
363 log.error(_(u"missing {name} method to handle {1}/{2}") |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
364 .format(name=cb_name, *uri_tuple)) |
925 | 365 continue |
366 else: | |
1038 | 367 resource.registerURI(uri_tuple, cb) |
925 | 368 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
369 LiberviaPage.importPages( |
1153 | 370 host, vhost_root, _parent=resource, _path=new_path, |
371 _extra_pages=_extra_pages) | |
372 | |
373 @classmethod | |
374 def onFileChange(cls, host, file_path, flags, site_root, site_path): | |
375 """Method triggered by file_watcher when something is changed in files | |
376 | |
377 This method is used in dev mode to reload pages when needed | |
378 @param file_path(filepath.FilePath): path of the file which triggered the event | |
379 @param flags[list[unicode]): human readable flags of the event (from | |
380 internet.inotify) | |
381 @param site_root(LiberviaRootResource): root of the site | |
382 @param site_path(unicode): absolute path of the site | |
383 """ | |
384 if flags == ['create']: | |
385 return | |
386 path = file_path.path.decode('utf-8') | |
387 base_name = os.path.basename(path) | |
388 if base_name != u"page_meta.py": | |
389 # we only handle libervia pages | |
390 return | |
391 | |
392 log.debug(u"{flags} event(s) received for {file_path}".format( | |
393 flags=u", ".join(flags), file_path=file_path)) | |
394 | |
395 dir_path = os.path.dirname(path) | |
396 if not dir_path.startswith(site_path): | |
397 raise exceptions.InternalError(u"watched file should start with site path") | |
398 | |
399 path_elts = [p for p in dir_path[len(site_path):].split('/') if p] | |
400 if not path_elts: | |
401 return | |
402 | |
403 if path_elts[0] == C.PAGES_DIR: | |
404 # a page has been modified | |
405 del path_elts[0] | |
406 if not path_elts: | |
407 # we need at least one element to parse | |
408 return | |
409 # we retrieve page by starting from site root and finding each path element | |
410 parent = page = site_root | |
411 new_page = False | |
412 for idx, child_name in enumerate(path_elts): | |
413 try: | |
1157
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
414 try: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
415 page = page.original.children[child_name] |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
416 except AttributeError: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
417 page = page.children[child_name] |
1153 | 418 except KeyError: |
419 if idx != len(path_elts)-1: | |
420 # a page has been created in a subdir when one or more | |
421 # page_meta.py are missing on the way | |
422 log.warning(_(u"Can't create a page at {path}, missing parents") | |
423 .format(path=path)) | |
424 return | |
425 new_page = True | |
426 else: | |
427 if idx<len(path_elts)-1: | |
428 parent = page.original | |
429 | |
430 try: | |
431 # we (re)create a page with the new/modified code | |
432 __, resource = cls.createPage(host, path, site_root, path_elts, | |
433 replace_on_conflict=True) | |
434 if not new_page: | |
1157
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
435 try: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
436 resource.children = page.original.children |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
437 except AttributeError: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
438 # FIXME: this .original handling madness is due to EncodingResourceWrapper |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
439 # EncodingResourceWrapper should probably be removed |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
440 resource.children = page.children |
1153 | 441 except Exception as e: |
442 log.warning(_(u"Can't create page: {reason}").format(reason=e)) | |
443 else: | |
444 url_elt = path_elts[-1] | |
445 if not new_page: | |
446 # the page was already existing, we remove it | |
447 del parent.children[url_elt] | |
448 # we can now add the new page | |
449 parent.putChild(url_elt, resource) | |
450 if new_page: | |
451 log.info(_(u"{page} created").format(page=resource)) | |
452 else: | |
453 log.info(_(u"{page} reloaded").format(page=resource)) | |
990
6daa59d44ee2
pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents:
985
diff
changeset
|
454 |
1038 | 455 def registerURI(self, uri_tuple, get_uri_cb): |
1153 | 456 """Register a URI handler |
925 | 457 |
458 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler | |
459 type/subtype as returned by tools/common/parseXMPPUri | |
1038 | 460 or type/None to handle all subtypes |
925 | 461 @param get_uri_cb(callable): method which take uri_data dict as only argument |
1038 | 462 and return absolute path with correct arguments or None if the page |
463 can't handle this URL | |
925 | 464 """ |
1038 | 465 if uri_tuple in self.uri_callbacks: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
466 log.info(_(u"{}/{} URIs are already handled, replacing by the new handler") |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
467 .format( *uri_tuple)) |
1038 | 468 self.uri_callbacks[uri_tuple] = (self, get_uri_cb) |
925 | 469 |
995 | 470 def registerSignal(self, request, signal, check_profile=True): |
471 r"""register a signal handler | |
472 | |
473 the page must be dynamic | |
474 when signal is received, self.on_signal will be called with: | |
475 - request | |
476 - signal name | |
477 - signal arguments | |
478 signal handler will be removed when connection with dynamic page will be lost | |
479 @param signal(unicode): name of the signal | |
480 last arg of signal must be profile, as it will be checked to filter signals | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
481 @param check_profile(bool): if True, signal profile (which MUST be last arg) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
482 will be checked against session profile. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
483 /!\ if False, profile will not be checked/filtered, be sure to know what you |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
484 are doing if you unset this option /!\ |
995 | 485 """ |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
486 # FIXME: add a timeout; if socket is not opened before it, signal handler |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
487 # must be removed |
995 | 488 if not self.dynamic: |
489 log.error(_(u"You can't register signal if page is not dynamic")) | |
490 return | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
491 LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = ( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
492 self, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
493 request, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
494 check_profile, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
495 ) |
995 | 496 request._signals_registered.append(signal) |
497 | |
1147
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
498 def getConfig(self, key, default=None, value_type=None): |
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
499 return self.host.getConfig(self.vhost_root, key=key, default=default, |
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
500 value_type=value_type) |
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
501 |
1146
76d75423ef53
server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
502 def getBuildPath(self, session_data): |
76d75423ef53
server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
503 return session_data.cache_dir + self.vhost.site_name |
76d75423ef53
server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
504 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
505 def getPageByName(self, name): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
506 return self.vhost_root.getPageByName(name) |
927
bb4dfc2802c0
server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents:
926
diff
changeset
|
507 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
508 def getPagePathFromURI(self, uri): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
509 return self.vhost.getPagePathFromURI(uri) |
936
78692d47340d
server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
510 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
511 def getPageRedirectURL(self, request, page_name=u"login", url=None): |
962 | 512 """generate URL for a page with redirect_url parameter set |
513 | |
514 mainly used for login page with redirection to current page | |
515 @param request(server.Request): current HTTP request | |
516 @param page_name(unicode): name of the page to go | |
517 @param url(None, unicode): url to redirect to | |
518 None to use request path (i.e. current page) | |
519 @return (unicode): URL to use | |
520 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
521 return u"{root_url}?redirect_url={redirect_url}".format( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
522 root_url=self.getPageByName(page_name).url, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
523 redirect_url=urllib.quote_plus(request.uri) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
524 if url is None |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
525 else url.encode("utf-8"), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
526 ) |
962 | 527 |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
528 def getURL(self, *args): |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
529 """retrieve URL of the page set arguments |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
530 |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
531 *args(list[unicode]): argument to add to the URL as path elements |
1014
dfced7992460
pages: ignore empty or unset arguments in getURL
Goffi <goffi@goffi.org>
parents:
1013
diff
changeset
|
532 empty or None arguments will be ignored |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
533 """ |
1014
dfced7992460
pages: ignore empty or unset arguments in getURL
Goffi <goffi@goffi.org>
parents:
1013
diff
changeset
|
534 url_args = [quote(a) for a in args if a] |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
535 |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
536 if self.name is not None and self.name in self.pages_redirects: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
537 # we check for redirection |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
538 redirect_data = self.pages_redirects[self.name] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
539 args_hash = tuple(args) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
540 for limit in xrange(len(args) + 1): |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
541 current_hash = args_hash[:limit] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
542 if current_hash in redirect_data: |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
543 url_base = redirect_data[current_hash] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
544 remaining = args[limit:] |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
545 remaining_url = "/".join(remaining) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
546 return os.path.join("/", url_base, remaining_url) |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
547 |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
548 return os.path.join(self.url, *url_args) |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
549 |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
550 def getCurrentURL(self, request): |
1015 | 551 """retrieve URL used to access this page |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
552 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
553 @return(unicode): current URL |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
554 """ |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
555 # we get url in the following way (splitting request.path instead of using |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
556 # request.prepath) because request.prepath may have been modified by |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
557 # redirection (if redirection args have been specified), while path reflect |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
558 # the real request |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
559 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
560 # we ignore empty path elements (i.e. double '/' or '/' at the end) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
561 path_elts = [p for p in request.path.split("/") if p] |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
562 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
563 if request.postpath: |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
564 if not request.postpath[-1]: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
565 # we remove trailing slash |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
566 request.postpath = request.postpath[:-1] |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
567 if request.postpath: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
568 # getSubPageURL must return subpage from the point where |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
569 # the it is called, so we have to remove remanining |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
570 # path elements |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
571 path_elts = path_elts[: -len(request.postpath)] |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
572 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
573 return u"/" + "/".join(path_elts).decode("utf-8") |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
574 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
575 def getParamURL(self, request, **kwargs): |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
576 """use URL of current request but modify the parameters in query part |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
577 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
578 **kwargs(dict[str, unicode]): argument to use as query parameters |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
579 @return (unicode): constructed URL |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
580 """ |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
581 current_url = self.getCurrentURL(request) |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
582 if kwargs: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
583 encoded = urllib.urlencode( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
584 {k: v.encode("utf-8") for k, v in kwargs.iteritems()} |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
585 ).decode("utf-8") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
586 current_url = current_url + u"?" + encoded |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
587 return current_url |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
588 |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
589 def getSubPageByName(self, subpage_name, parent=None): |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
590 """retrieve a subpage and its path using its name |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
591 |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
592 @param subpage_name(unicode): name of the sub page |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
593 it must be a direct children of parent page |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
594 @param parent(LiberviaPage, None): parent page |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
595 None to use current page |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
596 @return (tuple[str, LiberviaPage]): page subpath and instance |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
597 @raise exceptions.NotFound: no page has been found |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
598 """ |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
599 if parent is None: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
600 parent = self |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
601 for path, child in parent.children.iteritems(): |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
602 try: |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
603 child_name = child.name |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
604 except AttributeError: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
605 # LiberviaPages have a name, but maybe this is an other Resource |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
606 continue |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
607 if child_name == subpage_name: |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
608 return path, child |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
609 raise exceptions.NotFound(_(u"requested sub page has not been found")) |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
610 |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
611 def getSubPageURL(self, request, page_name, *args): |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
612 """retrieve a page in direct children and build its URL according to request |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
613 |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
614 request's current path is used as base (at current parsing point, |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
615 i.e. it's more prepath than path). |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
616 Requested page is checked in children and an absolute URL is then built |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
617 by the resulting combination. |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
618 This method is useful to construct absolute URLs for children instead of |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
619 using relative path, which may not work in subpages, and are linked to the |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
620 names of directories (i.e. relative URL will break if subdirectory is renamed |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
621 while getSubPageURL won't as long as page_name is consistent). |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
622 Also, request.path is used, keeping real path used by user, |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
623 and potential redirections. |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
624 @param request(server.Request): current HTTP request |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
625 @param page_name(unicode): name of the page to retrieve |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
626 it must be a direct children of current page |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
627 @param *args(list[unicode]): arguments to add as path elements |
1110
045e8bdaed4f
pages: ignore args if they are None in getSubPageURL
Goffi <goffi@goffi.org>
parents:
1109
diff
changeset
|
628 if an arg is None, it will be ignored |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
629 @return (unicode): absolute URL to the sub page |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
630 """ |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
631 current_url = self.getCurrentURL(request) |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
632 path, child = self.getSubPageByName(page_name) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
633 return os.path.join( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
634 u"/", current_url, path, *[quote(a) for a in args if a is not None] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
635 ) |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
636 |
1031
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
637 def getURLByNames(self, named_path): |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
638 """retrieve URL from pages names and arguments |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
639 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
640 @param request(server.Request): request linked to the session |
1031
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
641 @param named_path(list[tuple[unicode, list[unicode]]]): path to the page as a list |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
642 of tuples of 2 items: |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
643 - first item is page name |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
644 - second item is list of path arguments of this page |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
645 @return (unicode): URL to the requested page with given path arguments |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
646 @raise exceptions.NotFound: one of the page was not found |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
647 """ |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
648 current_page = None |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
649 path = [] |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
650 for page_name, page_args in named_path: |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
651 if current_page is None: |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
652 current_page = self.getPageByName(page_name) |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
653 path.append(current_page.getURL(*page_args)) |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
654 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
655 sub_path, current_page = self.getSubPageByName( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
656 page_name, parent=current_page |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
657 ) |
1031
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
658 path.append(sub_path) |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
659 if page_args: |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
660 path.extend([quote(a) for a in page_args]) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
661 return self.host.checkRedirection(self.vhost_root, u"/".join(path)) |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
662 |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
663 def getURLByPath(self, *args): |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
664 """generate URL by path |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
665 |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
666 this method as a similar effect as getURLByNames, but it is more readable |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
667 by using SubPage to get pages instead of using tuples |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
668 @param request(server.Request): request linked to the session |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
669 @param *args: path element: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
670 - if unicode, will be used as argument |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
671 - if util.SubPage instance, must be the name of a subpage |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
672 @return (unicode): generated path |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
673 """ |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
674 args = list(args) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
675 if not args: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
676 raise ValueError("You must specify path elements") |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
677 # root page is the one needed to construct the base of the URL |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
678 # if first arg is not a SubPage instance, we use current page |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
679 if not isinstance(args[0], SubPage): |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
680 root = self |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
681 else: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
682 root = self.getPageByName(args.pop(0)) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
683 # we keep track of current page to check subpage |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
684 current_page = root |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
685 url_elts = [] |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
686 arguments = [] |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
687 while True: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
688 while args and not isinstance(args[0], SubPage): |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
689 arguments.append(quote(args.pop(0))) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
690 if not url_elts: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
691 url_elts.append(root.getURL(*arguments)) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
692 else: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
693 url_elts.extend(arguments) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
694 if not args: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
695 break |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
696 else: |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
697 path, current_page = current_page.getSubPageByName(args.pop(0)) |
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
698 arguments = [path] |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
699 return self.host.checkRedirection(self.vhost_root, u"/".join(url_elts)) |
1092
63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
Goffi <goffi@goffi.org>
parents:
1071
diff
changeset
|
700 |
917 | 701 def getChildWithDefault(self, path, request): |
702 # we handle children ourselves | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
703 raise exceptions.InternalError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
704 u"this method should not be used with LiberviaPage" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
705 ) |
917 | 706 |
707 def nextPath(self, request): | |
708 """get next URL path segment, and update request accordingly | |
709 | |
710 will move first segment of postpath in prepath | |
711 @param request(server.Request): current HTTP request | |
712 @return (unicode): unquoted segment | |
713 @raise IndexError: there is no segment left | |
714 """ | |
715 pathElement = request.postpath.pop(0) | |
716 request.prepath.append(pathElement) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
717 return urllib.unquote(pathElement).decode("utf-8") |
917 | 718 |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
719 def _filterPathValue(self, value, handler, name, request): |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
720 """Modify a path value according to handler (see [getPathArgs])""" |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
721 if handler in (u"@", u"@jid") and value == u"@": |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
722 value = None |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
723 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
724 if handler in (u"", u"@"): |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
725 if value is None: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
726 return u"" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
727 elif handler in (u"jid", u"@jid"): |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
728 if value: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
729 try: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
730 return jid.JID(value) |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
731 except RuntimeError: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
732 log.warning(_(u"invalid jid argument: {value}").format(value=value)) |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
733 self.pageError(request, C.HTTP_BAD_REQUEST) |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
734 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
735 return u"" |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
736 else: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
737 return handler(self, value, name, request) |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
738 |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
739 return value |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
740 |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
741 def getPathArgs(self, request, names, min_args=0, **kwargs): |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
742 """get several path arguments at once |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
743 |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
744 Arguments will be put in request data. |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
745 Missing arguments will have None value |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
746 @param names(list[unicode]): list of arguments to get |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
747 @param min_args(int): if less than min_args are found, PageError is used with |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
748 C.HTTP_BAD_REQUEST |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
749 Use 0 to ignore |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
750 @param **kwargs: special value or optional callback to use for arguments |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
751 names of the arguments must correspond to those in names |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
752 special values may be: |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
753 - '': use empty string instead of None when no value is specified |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
754 - '@': if value of argument is empty or '@', empty string will be used |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
755 - 'jid': value must be converted to jid.JID if it exists, else empty |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
756 string is used |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
757 - '@jid': if value of arguments is empty or '@', empty string will be |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
758 used, else it will be converted to jid |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
759 """ |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
760 data = self.getRData(request) |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
761 |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
762 for idx, name in enumerate(names): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
763 if name[0] == u"*": |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
764 value = data[name[1:]] = [] |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
765 while True: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
766 try: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
767 value.append(self.nextPath(request)) |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
768 except IndexError: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
769 idx -= 1 |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
770 break |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
771 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
772 idx += 1 |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
773 else: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
774 try: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
775 value = data[name] = self.nextPath(request) |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
776 except IndexError: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
777 data[name] = None |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
778 idx -= 1 |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
779 break |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
780 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
781 values_count = idx + 1 |
1071
8f77e36cd51d
pages: fixed args count in getPathArgs
Goffi <goffi@goffi.org>
parents:
1065
diff
changeset
|
782 if values_count < min_args: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
783 log.warning(_(u"Missing arguments in URL (got {count}, expected at least " |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
784 u"{min_args})").format(count=values_count, min_args=min_args)) |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
785 self.pageError(request, C.HTTP_BAD_REQUEST) |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
786 |
1071
8f77e36cd51d
pages: fixed args count in getPathArgs
Goffi <goffi@goffi.org>
parents:
1065
diff
changeset
|
787 for name in names[values_count:]: |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
788 data[name] = None |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
789 |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
790 for name, handler in kwargs.iteritems(): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
791 if name[0] == "*": |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
792 data[name] = [ |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
793 self._filterPathValue(v, handler, name, request) for v in data[name] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
794 ] |
1060
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
795 else: |
f0f7b3324749
pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
Goffi <goffi@goffi.org>
parents:
1059
diff
changeset
|
796 data[name] = self._filterPathValue(data[name], handler, name, request) |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
797 |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
798 ## Pagination/Filtering ## |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
799 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
800 def getPubsubExtra(self, request, page_max=10, params=None, extra=None, |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
801 order_by=C.ORDER_BY_CREATION): |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
802 """Set extra dict to retrieve PubSub items corresponding to URL parameters |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
803 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
804 Following parameters are used: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
805 - after: set rsm_after with ID of item |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
806 - before: set rsm_before with ID of item |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
807 @param request(server.Request): current HTTP request |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
808 @param page_max(int): required number of items per page |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
809 @param params(None, dict[unicode, list[unicode]]): params as returned by |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
810 self.getAllPostedData. |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
811 None to parse URL automatically |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
812 @param extra(None, dict): extra dict to use, or None to use a new one |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
813 @param order_by(unicode, None): key to order by |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
814 None to not specify order |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
815 @return (dict): fill extra data |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
816 """ |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
817 if params is None: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
818 params = self.getAllPostedData(request, multiple=False) |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
819 if extra is None: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
820 extra = {} |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
821 else: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
822 assert not {u"rsm_max", u"rsm_after", u"rsm_before", |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
823 C.KEY_ORDER_BY}.intersection(extra.keys()) |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
824 extra[u"rsm_max"] = unicode(page_max) |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
825 if order_by is not None: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
826 extra[C.KEY_ORDER_BY] = order_by |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
827 if u'after' in params: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
828 extra[u'rsm_after'] = params[u'after'] |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
829 elif u'before' in params: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
830 extra[u'rsm_before'] = params[u'before'] |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
831 return extra |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
832 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
833 def setPagination(self, request, pubsub_data): |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
834 """Add to template_data if suitable |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
835 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
836 "previous_page_url" and "next_page_url" will be added using respectively |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
837 "before" and "after" URL parameters |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
838 @param request(server.Request): current HTTP request |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
839 @param pubsub_data(dict): pubsub metadata parsed with |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
840 data_objects.parsePubSubMetadata |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
841 """ |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
842 template_data = request.template_data |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
843 try: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
844 last_id = pubsub_data[u"rsm_last"] |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
845 except KeyError: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
846 # no pagination available |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
847 return |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
848 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
849 if pubsub_data.get("rsm_index", 1) > 0: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
850 # We only show previous button if it's not the first page already. |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
851 # If we have no index, we default to display the button anyway |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
852 # as we can't know if we are on the first page or not. |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
853 first_id = pubsub_data[u"rsm_first"] |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
854 template_data['previous_page_url'] = self.getParamURL(request, |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
855 before=first_id) |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
856 if not pubsub_data[u"complete"]: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
857 # we also show the page next button if complete is None because we |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
858 # can't know where we are in the feed in this case. |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
859 template_data['next_page_url'] = self.getParamURL(request, after=last_id) |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
860 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
861 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
862 ## Cache handling ## |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
863 |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
864 def _setCacheHeaders(self, request, cache): |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
865 """Set ETag and Last-Modified HTTP headers, used for caching""" |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
866 request.setHeader("ETag", cache.hash) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
867 last_modified = self.host.getHTTPDate(cache.created) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
868 request.setHeader("Last-Modified", last_modified) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
869 |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
870 def _checkCacheHeaders(self, request, cache): |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
871 """Check if a cache condition is set on the request |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
872 |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
873 if condition is valid, C.HTTP_NOT_MODIFIED is returned |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
874 """ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
875 etag_match = request.getHeader("If-None-Match") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
876 if etag_match is not None: |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
877 if cache.hash == etag_match: |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
878 self.pageError(request, C.HTTP_NOT_MODIFIED, no_body=True) |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
879 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
880 modified_match = request.getHeader("If-Modified-Since") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
881 if modified_match is not None: |
1109
3a7b2b239d3e
pages: change module for date_parse, following backend change
Goffi <goffi@goffi.org>
parents:
1092
diff
changeset
|
882 modified = date_utils.date_parse(modified_match) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
883 if modified >= int(cache.created): |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
884 self.pageError(request, C.HTTP_NOT_MODIFIED, no_body=True) |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
885 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
886 def checkCacheSubscribeCb(self, sub_id, service, node): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
887 self.cache_pubsub_sub.add((service, node, sub_id)) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
888 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
889 def checkCacheSubscribeEb(self, failure_, service, node): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
890 log.warning(_(u"Can't subscribe to node: {msg}").format(msg=failure_)) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
891 # FIXME: cache must be marked as unusable here |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
892 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
893 def psNodeWatchAddEb(self, failure_, service, node): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
894 log.warning(_(u"Can't add node watched: {msg}").format(msg=failure_)) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
895 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
896 def checkCache(self, request, cache_type, **kwargs): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
897 """check if a page is in cache and return cached version if suitable |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
898 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
899 this method may perform extra operation to handle cache (e.g. subscribing to a |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
900 pubsub node) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
901 @param request(server.Request): current HTTP request |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
902 @param cache_type(int): on of C.CACHE_* const. |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
903 @param **kwargs: args according to cache_type: |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
904 C.CACHE_PUBSUB: |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
905 service: pubsub service |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
906 node: pubsub node |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
907 short: short name of feature (needed if node is empty to find namespace) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
908 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
909 """ |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
910 if request.postpath: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
911 # we are not on the final page, no need to go further |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
912 return |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
913 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
914 profile = self.getProfile(request) or C.SERVICE_PROFILE |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
915 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
916 if cache_type == C.CACHE_PUBSUB: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
917 service, node = kwargs["service"], kwargs["node"] |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
918 if not node: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
919 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
920 short = kwargs["short"] |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
921 node = self.host.ns_map[short] |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
922 except KeyError: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
923 log.warning(_(u'Can\'t use cache for empty node without namespace ' |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
924 u'set, please ensure to set "short" and that it is ' |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
925 u'registered')) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
926 return |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
927 if profile != C.SERVICE_PROFILE: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
928 # only service profile is cache for now |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
929 return |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
930 try: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
931 cache = (self.cache[profile][cache_type][service][node] |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
932 [self.vhost_root][request.uri][self]) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
933 except KeyError: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
934 # no cache yet, let's subscribe to the pubsub node |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
935 d1 = self.host.bridgeCall( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
936 "psSubscribe", service.full(), node, {}, profile |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
937 ) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
938 d1.addCallback(self.checkCacheSubscribeCb, service, node) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
939 d1.addErrback(self.checkCacheSubscribeEb, service, node) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
940 d2 = self.host.bridgeCall("psNodeWatchAdd", service.full(), node, profile) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
941 d2.addErrback(self.psNodeWatchAddEb, service, node) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
942 self._do_cache = [self, profile, cache_type, service, node, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
943 self.vhost_root, request.uri] |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
944 # we don't return the Deferreds as it is not needed to wait for |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
945 # the subscription to continue with page rendering |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
946 return |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
947 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
948 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
949 raise exceptions.InternalError(u"Unknown cache_type") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
950 log.debug(u"using cache for {page}".format(page=self)) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
951 cache.last_access = time.time() |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
952 self._setCacheHeaders(request, cache) |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
953 self._checkCacheHeaders(request, cache) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
954 request.write(cache.rendered) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
955 request.finish() |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
956 raise failure.Failure(exceptions.CancelError(u"cache is used")) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
957 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
958 def _cacheURL(self, __, request, profile): |
1018 | 959 self.cached_urls.setdefault(profile, {})[request.uri] = CacheURL(request) |
960 | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
961 @classmethod |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
962 def onNodeEvent(cls, host, service, node, event_type, items, profile): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
963 """Invalidate cache for all pages linked to this node""" |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
964 try: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
965 cache = cls.cache[profile][C.CACHE_PUBSUB][jid.JID(service)][node] |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
966 except KeyError: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
967 log.info(_( |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
968 u"Removing subscription for {service}/{node}: " |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
969 u"the page is not cached").format(service=service, node=node)) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
970 d1 = host.bridgeCall("psUnsubscribe", service, node, profile) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
971 d1.addErrback( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
972 lambda failure_: log.warning( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
973 _(u"Can't unsubscribe from {service}/{node}: {msg}").format( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
974 service=service, node=node, msg=failure_))) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
975 d2 = host.bridgeCall("psNodeWatchAdd", service, node, profile) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
976 # TODO: check why the page is not in cache, remove subscription? |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
977 d2.addErrback( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
978 lambda failure_: log.warning( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
979 _(u"Can't remove watch for {service}/{node}: {msg}").format( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
980 service=service, node=node, msg=failure_))) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
981 else: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
982 cache.clear() |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
983 |
995 | 984 @classmethod |
985 def onSignal(cls, host, signal, *args): | |
986 """Generic method which receive registered signals | |
987 | |
988 if a callback is registered for this signal, call it | |
989 @param host: Libervia instance | |
990 @param signal(unicode): name of the signal | |
991 @param *args: args of the signals | |
992 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
993 for page, request, check_profile in cls.signals_handlers.get( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
994 signal, {} |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
995 ).itervalues(): |
995 | 996 if check_profile: |
997 signal_profile = args[-1] | |
998 request_profile = page.getProfile(request) | |
999 if not request_profile: | |
1000 # if you want to use signal without session, unset check_profile | |
1001 # (be sure to know what you are doing) | |
1002 log.error(_(u"no session started, signal can't be checked")) | |
1003 continue | |
1004 if signal_profile != request_profile: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1005 # we ignore the signal, it's not for our profile |
995 | 1006 continue |
1007 if request._signals_cache is not None: | |
1008 # socket is not yet opened, we cache the signal | |
1009 request._signals_cache.append((request, signal, args)) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1010 log.debug( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1011 u"signal [{signal}] cached: {args}".format(signal=signal, args=args) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1012 ) |
995 | 1013 else: |
1014 page.on_signal(page, request, signal, *args) | |
1015 | |
1016 def onSocketOpen(self, request): | |
1017 """Called for dynamic pages when socket has just been opened | |
1018 | |
1019 we send all cached signals | |
1020 """ | |
1021 assert request._signals_cache is not None | |
1022 cache = request._signals_cache | |
1023 request._signals_cache = None | |
1024 for request, signal, args in cache: | |
1025 self.on_signal(self, request, signal, *args) | |
1026 | |
1027 def onSocketClose(self, request): | |
1028 """Called for dynamic pages when socket has just been closed | |
1029 | |
1030 we remove signal handler | |
1031 """ | |
1032 for signal in request._signals_registered: | |
1033 try: | |
1034 del LiberviaPage.signals_handlers[signal][id(request)] | |
1035 except KeyError: | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1036 log.error(_(u"Can't find signal handler for [{signal}], this should not " |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1037 u"happen").format(signal=signal)) |
995 | 1038 else: |
1039 log.debug(_(u"Removed signal handler")) | |
1040 | |
1062
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1041 def delegateToResource(self, request, resource): |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1042 """continue workflow with Twisted Resource""" |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1043 buf = resource.render(request) |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1044 if buf == server.NOT_DONE_YET: |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1045 pass |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1046 else: |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1047 request.write(buf) |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1048 request.finish() |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1049 raise failure.Failure(exceptions.CancelError(u"resource delegation")) |
1062
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1050 |
962 | 1051 def HTTPRedirect(self, request, url): |
1052 """redirect to an URL using HTTP redirection | |
1053 | |
1054 @param request(server.Request): current HTTP request | |
1055 @param url(unicode): url to redirect to | |
1056 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1057 web_util.redirectTo(url.encode("utf-8"), request) |
962 | 1058 request.finish() |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1059 raise failure.Failure(exceptions.CancelError(u"HTTP redirection is used")) |
962 | 1060 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1061 def redirectOrContinue(self, request, redirect_arg=u"redirect_url"): |
962 | 1062 """helper method to redirect a page to an url given as arg |
1063 | |
1064 if the arg is not present, the page will continue normal workflow | |
1065 @param request(server.Request): current HTTP request | |
1066 @param redirect_arg(unicode): argument to use to get redirection URL | |
1067 @interrupt: redirect the page to requested URL | |
1068 @interrupt pageError(C.HTTP_BAD_REQUEST): empty or non local URL is used | |
1069 """ | |
1070 try: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1071 url = request.args["redirect_url"][0] |
1061
bacb8f229742
pages: fixed unicode escaping when using a redirecting URL
Goffi <goffi@goffi.org>
parents:
1060
diff
changeset
|
1072 except (KeyError, IndexError): |
962 | 1073 pass |
1074 else: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1075 # a redirection is requested |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1076 if not url or url[0] != u"/": |
962 | 1077 # we only want local urls |
1078 self.pageError(request, C.HTTP_BAD_REQUEST) | |
1079 else: | |
1080 self.HTTPRedirect(request, url) | |
1081 | |
1032
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1082 def pageRedirect(self, page_path, request, skip_parse_url=True, path_args=None): |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1083 """redirect a page to a named page |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1084 |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1085 the workflow will continue with the workflow of the named page, |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1086 skipping named page's parse_url method if it exist. |
962 | 1087 If you want to do a HTTP redirection, use HTTPRedirect |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1088 @param page_path(unicode): path to page (elements are separated by "/"): |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1089 if path starts with a "/": |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1090 path is a full path starting from root |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1091 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1092 - first element is name as registered in name variable |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1093 - following element are subpages path |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1094 e.g.: "blog" redirect to page named "blog" |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1095 "blog/atom.xml" redirect to atom.xml subpage of "blog" |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1096 "/common/blog/atom.xml" redirect to the page at the given full path |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1097 @param request(server.Request): current HTTP request |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1098 @param skip_parse_url(bool): if True, parse_url method on redirect page will be |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1099 skipped |
1032
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1100 @param path_args(list[unicode], None): path arguments to use in redirected page |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1101 @raise KeyError: there is no known page with this name |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1102 """ |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1103 # FIXME: render non LiberviaPage resources |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1104 path = page_path.rstrip(u"/").split(u"/") |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1105 if not path[0]: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1106 redirect_page = self.vhost_root |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1107 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1108 redirect_page = self.named_pages[path[0]] |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1109 |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1110 for subpage in path[1:]: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1111 if redirect_page is self.vhost_root: |
962 | 1112 redirect_page = redirect_page.children[subpage] |
1113 else: | |
1114 redirect_page = redirect_page.original.children[subpage] | |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1115 |
1032
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1116 if path_args is not None: |
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1117 args = [quote(a) for a in path_args] |
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1118 request.postpath = args + request.postpath |
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1119 |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1120 if self._do_cache: |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1121 # if cache is needed, it will be handled by final page |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1122 redirect_page._do_cache = self._do_cache |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1123 self._do_cache = None |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1124 |
1015 | 1125 redirect_page.renderPage(request, skip_parse_url=skip_parse_url) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1126 raise failure.Failure(exceptions.CancelError(u"page redirection is used")) |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1127 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1128 def pageError(self, request, code=C.HTTP_NOT_FOUND, no_body=False): |
917 | 1129 """generate an error page and terminate the request |
1130 | |
1131 @param request(server.Request): HTTP request | |
1132 @param core(int): error code to use | |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1133 @param no_body: don't write body if True |
917 | 1134 """ |
1135 request.setResponseCode(code) | |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1136 if no_body: |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1137 request.finish() |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1138 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1139 template = u"error/" + unicode(code) + ".html" |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1140 if self.vhost_root.site_name: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1141 request.template_data[u'site'] = self.vhost_root.site_name |
917 | 1142 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1143 rendered = self.host.renderer.render( |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1144 template, |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1145 error_code=code, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1146 **request.template_data |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1147 ) |
917 | 1148 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1149 self.writeData(rendered, request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1150 raise failure.Failure(exceptions.CancelError(u"error page is used")) |
917 | 1151 |
1152 def writeData(self, data, request): | |
937
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1153 """write data to transport and finish the request""" |
917 | 1154 if data is None: |
1155 self.pageError(request) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1156 data_encoded = data.encode("utf-8") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1157 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1158 if self._do_cache is not None: |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1159 redirected_page = self._do_cache.pop(0) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1160 cache = reduce(lambda d, k: d.setdefault(k, {}), self._do_cache, self.cache) |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1161 page_cache = cache[redirected_page] = CachePage(data_encoded) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1162 self._setCacheHeaders(request, page_cache) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1163 log.debug(_(u"{page} put in cache for [{profile}]") |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1164 .format( page=self, profile=self._do_cache[0])) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1165 self._do_cache = None |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1166 self._checkCacheHeaders(request, page_cache) |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1167 |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1168 request.write(data_encoded) |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1169 request.finish() |
917 | 1170 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1171 def _subpagesHandler(self, __, request): |
917 | 1172 """render subpage if suitable |
1173 | |
1174 this method checks if there is still an unmanaged part of the path | |
1175 and check if it corresponds to a subpage. If so, it render the subpage | |
1176 else it render a NoResource. | |
1177 If there is no unmanaged part of the segment, current page workflow is pursued | |
1178 """ | |
1179 if request.postpath: | |
1180 subpage = self.nextPath(request) | |
1181 try: | |
1182 child = self.children[subpage] | |
1183 except KeyError: | |
1184 self.pageError(request) | |
1185 else: | |
1186 child.render(request) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1187 raise failure.Failure(exceptions.CancelError(u"subpage page is used")) |
917 | 1188 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1189 def _prepare_dynamic(self, __, request): |
998
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1190 # we need to activate dynamic page |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1191 # we set data for template, and create/register token |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1192 socket_token = unicode(uuid.uuid4()) |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1193 socket_url = self.host.getWebsocketURL(request) |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1194 socket_debug = C.boolConst(self.host.debug) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1195 request.template_data["websocket"] = WebsocketMeta( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1196 socket_url, socket_token, socket_debug |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1197 ) |
998
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1198 self.host.registerWSToken(socket_token, self, request) |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1199 # we will keep track of handlers to remove |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1200 request._signals_registered = [] |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1201 # we will cache registered signals until socket is opened |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1202 request._signals_cache = [] |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1203 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1204 def _prepare_render(self, __, request): |
926
612e33fd32a8
server (pages): fixed _prepare_render handling
Goffi <goffi@goffi.org>
parents:
925
diff
changeset
|
1205 return defer.maybeDeferred(self.prepare_render, self, request) |
917 | 1206 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1207 def _render_method(self, __, request): |
917 | 1208 return defer.maybeDeferred(self.render_method, self, request) |
1209 | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1210 def _render_template(self, __, request): |
962 | 1211 template_data = request.template_data |
1212 | |
1213 # if confirm variable is set in case of successfuly data post | |
1214 session_data = self.host.getSessionData(request, session_iface.ISATSession) | |
1215 if session_data.popPageFlag(self, C.FLAG_CONFIRM): | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1216 template_data[u"confirm"] = True |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1217 if self.vhost_root.site_name: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1218 template_data[u'site'] = self.vhost_root.site_name |
962 | 1219 |
917 | 1220 return self.host.renderer.render( |
1221 self.template, | |
1148
3c7a64adfd42
pages: use absolute path for build_path + added page_url to template (which is getURL() without argument)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
1222 page_url=self.getURL(), |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
1223 media_path=u"/" + C.MEDIA_DIR, |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1224 cache_path=session_data.cache_dir, |
1148
3c7a64adfd42
pages: use absolute path for build_path + added page_url to template (which is getURL() without argument)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
1225 build_path=u"/" + C.BUILD_DIR + u"/", |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1226 main_menu=self.main_menu, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1227 **template_data) |
917 | 1228 |
1229 def _renderEb(self, failure_, request): | |
1230 """don't raise error on CancelError""" | |
1231 failure_.trap(exceptions.CancelError) | |
1232 | |
937
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1233 def _internalError(self, failure_, request): |
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1234 """called if an error is not catched""" |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1235 log.error(_(u"Uncatched error for HTTP request on {url}: {msg}") |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1236 .format( url=request.URLPath(), msg=failure_)) |
937
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1237 self.pageError(request, C.HTTP_INTERNAL_ERROR) |
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1238 |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1239 def _on_data_post_redirect(self, ret, request): |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1240 """called when page's on_data_post has been done successfuly |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1241 |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1242 This will do a Post/Redirect/Get pattern. |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1243 this method redirect to the same page or to request.data['post_redirect_page'] |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1244 post_redirect_page can be either a page or a tuple with page as first item, then |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1245 a list of unicode arguments to append to the url. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1246 if post_redirect_page is not used, initial request.uri (i.e. the same page as |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1247 where the data have been posted) will be used for redirection. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1248 HTTP status code "See Other" (303) is used as it is the recommanded code in |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1249 this case. |
962 | 1250 @param ret(None, unicode, iterable): on_data_post return value |
1251 see LiberviaPage.__init__ on_data_post docstring | |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1252 """ |
962 | 1253 if ret is None: |
1254 ret = () | |
1255 elif isinstance(ret, basestring): | |
1256 ret = (ret,) | |
1257 else: | |
1258 ret = tuple(ret) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1259 raise NotImplementedError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1260 _(u"iterable in on_data_post return value is not used yet") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1261 ) |
957
67bf14c91d5c
server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
1262 session_data = self.host.getSessionData(request, session_iface.ISATSession) |
974
4aa38c49bff7
pages: fixed use of request data in _on_data_post_redirect
Goffi <goffi@goffi.org>
parents:
972
diff
changeset
|
1263 request_data = self.getRData(request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1264 if "post_redirect_page" in request_data: |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1265 redirect_page_data = request_data["post_redirect_page"] |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1266 if isinstance(redirect_page_data, tuple): |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1267 redirect_page = redirect_page_data[0] |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
1268 redirect_page_args = redirect_page_data[1:] |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
1269 redirect_uri = redirect_page.getURL(*redirect_page_args) |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1270 else: |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1271 redirect_page = redirect_page_data |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
1272 redirect_uri = redirect_page.url |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1273 else: |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1274 redirect_page = self |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1275 redirect_uri = request.uri |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1276 |
962 | 1277 if not C.POST_NO_CONFIRM in ret: |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1278 session_data.setPageFlag(redirect_page, C.FLAG_CONFIRM) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1279 request.setResponseCode(C.HTTP_SEE_OTHER) |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1280 request.setHeader("location", redirect_uri) |
955
4f7cb6335a33
server(pages): do Post/Redirect/Get pattern when on_data_post is used (avoid double posting on refresh)
Goffi <goffi@goffi.org>
parents:
950
diff
changeset
|
1281 request.finish() |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1282 raise failure.Failure(exceptions.CancelError(u"Post/Redirect/Get is used")) |
955
4f7cb6335a33
server(pages): do Post/Redirect/Get pattern when on_data_post is used (avoid double posting on refresh)
Goffi <goffi@goffi.org>
parents:
950
diff
changeset
|
1283 |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1284 def _on_data_post(self, __, request): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1285 csrf_token = self.host.getSessionData( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1286 request, session_iface.ISATSession |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1287 ).csrf_token |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1288 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1289 given_csrf = self.getPostedData(request, u"csrf_token") |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1290 except KeyError: |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1291 given_csrf = None |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1292 if given_csrf is None or given_csrf != csrf_token: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1293 log.warning( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1294 _(u"invalid CSRF token, hack attempt? URL: {url}, IP: {ip}").format( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1295 url=request.uri, ip=request.getClientIP() |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1296 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1297 ) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1298 self.pageError(request, C.HTTP_UNAUTHORIZED) |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1299 d = defer.maybeDeferred(self.on_data_post, self, request) |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1300 d.addCallback(self._on_data_post_redirect, request) |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1301 return d |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1302 |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1303 def getPostedData(self, request, keys, multiple=False): |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
1304 """get data from a POST request or from URL's query part and decode it |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1305 |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1306 @param request(server.Request): request linked to the session |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1307 @param keys(unicode, iterable[unicode]): name of the value(s) to get |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1308 unicode to get one value |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1309 iterable to get more than one |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1310 @param multiple(bool): True if multiple values are possible/expected |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1311 if False, the first value is returned |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1312 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1313 values received for this(these) key(s) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1314 @raise KeyError: one specific key has been requested, and it is missing |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1315 """ |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1316 # FIXME: request.args is already unquoting the value, it seems we are doing |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1317 # double unquote |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1318 if isinstance(keys, basestring): |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1319 keys = [keys] |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1320 get_first = True |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1321 else: |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1322 get_first = False |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1323 |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1324 ret = [] |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1325 for key in keys: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1326 gen = (urllib.unquote(v).decode("utf-8") for v in request.args.get(key, [])) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1327 if multiple: |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1328 ret.append(gen) |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1329 else: |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1330 try: |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1331 ret.append(next(gen)) |
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1332 except StopIteration: |
962 | 1333 raise KeyError(key) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1334 |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1335 return ret[0] if get_first else ret |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1336 |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1337 def getAllPostedData(self, request, except_=(), multiple=True): |
959 | 1338 """get all posted data |
1339 | |
1340 @param request(server.Request): request linked to the session | |
1341 @param except_(iterable[unicode]): key of values to ignore | |
1342 csrf_token will always be ignored | |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1343 @param multiple(bool): if False, only the first values are returned |
959 | 1344 @return (dict[unicode, list[unicode]]): post values |
1345 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1346 except_ = tuple(except_) + (u"csrf_token",) |
959 | 1347 ret = {} |
1348 for key, values in request.args.iteritems(): | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1349 key = urllib.unquote(key).decode("utf-8") |
959 | 1350 if key in except_: |
1351 continue | |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1352 if not multiple: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1353 ret[key] = urllib.unquote(values[0]).decode("utf-8") |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1354 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1355 ret[key] = [urllib.unquote(v).decode("utf-8") for v in values] |
959 | 1356 return ret |
1357 | |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1358 def getProfile(self, request): |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1359 """helper method to easily get current profile |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1360 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1361 @return (unicode, None): current profile |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1362 None if no profile session is started |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1363 """ |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1364 sat_session = self.host.getSessionData(request, session_iface.ISATSession) |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1365 return sat_session.profile |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1366 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1367 def getRData(self, request): |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1368 """helper method to get request data dict |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1369 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1370 this dictionnary if for the request only, it is not saved in session |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1371 It is mainly used to pass data between pages/methods called during request |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1372 workflow |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1373 @return (dict): request data |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1374 """ |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1375 try: |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1376 return request.data |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1377 except AttributeError: |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1378 request.data = {} |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1379 return request.data |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1380 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1381 def _checkAccess(self, data, request): |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1382 """Check access according to self.access |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1383 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1384 if access is not granted, show a HTTP_UNAUTHORIZED pageError and stop request, |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1385 else return data (so it can be inserted in deferred chain |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1386 """ |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1387 if self.access == C.PAGES_ACCESS_PUBLIC: |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1388 pass |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1389 elif self.access == C.PAGES_ACCESS_PROFILE: |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1390 profile = self.getProfile(request) |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1391 if not profile: |
962 | 1392 # no session started |
1393 if not self.host.options["allow_registration"]: | |
1394 # registration not allowed, access is not granted | |
1395 self.pageError(request, C.HTTP_UNAUTHORIZED) | |
1396 else: | |
1397 # registration allowed, we redirect to login page | |
1398 login_url = self.getPageRedirectURL(request) | |
1399 self.HTTPRedirect(request, login_url) | |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1400 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1401 return data |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1402 |
995 | 1403 def renderPartial(self, request, template, template_data): |
1404 """Render a template to be inserted in dynamic page | |
1405 | |
1406 this is NOT the normal page rendering method, it is used only to update | |
1407 dynamic pages | |
1408 @param template(unicode): path of the template to render | |
1409 @param template_data(dict): template_data to use | |
1410 """ | |
1411 if not self.dynamic: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1412 raise exceptions.InternalError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1413 _(u"renderPartial must only be used with dynamic pages") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1414 ) |
995 | 1415 session_data = self.host.getSessionData(request, session_iface.ISATSession) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1416 if self.vhost_root.site_name: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1417 template_data[u'site'] = self.vhost_root.site_name |
995 | 1418 |
1419 return self.host.renderer.render( | |
1420 template, | |
1148
3c7a64adfd42
pages: use absolute path for build_path + added page_url to template (which is getURL() without argument)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
1421 page_url=self.getURL(), |
3c7a64adfd42
pages: use absolute path for build_path + added page_url to template (which is getURL() without argument)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
1422 media_path=u"/" + C.MEDIA_DIR, |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1423 cache_path=session_data.cache_dir, |
1148
3c7a64adfd42
pages: use absolute path for build_path + added page_url to template (which is getURL() without argument)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
1424 build_path=u"/" + C.BUILD_DIR + u"/", |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1425 main_menu=self.main_menu, |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1426 **template_data |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1427 ) |
995 | 1428 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1429 def renderAndUpdate( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1430 self, request, template, selectors, template_data_update, update_type="append" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1431 ): |
995 | 1432 """Helper method to render a partial page element and update the page |
1433 | |
1434 this is NOT the normal page rendering method, it is used only to update | |
1435 dynamic pages | |
1436 @param request(server.Request): current HTTP request | |
1437 @param template: same as for [renderPartial] | |
1438 @param selectors: CSS selectors to use | |
1439 @param template_data_update: template data to use | |
1440 template data cached in request will be copied then updated | |
1441 with this data | |
1442 @parap update_type(unicode): one of: | |
1443 append: append rendered element to selected element | |
1444 """ | |
1445 template_data = request.template_data.copy() | |
1446 template_data.update(template_data_update) | |
1447 html = self.renderPartial(request, template, template_data) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1448 request.sendData(u"dom", selectors=selectors, update_type=update_type, html=html) |
995 | 1449 |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1450 def renderPage(self, request, skip_parse_url=False): |
937
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1451 """Main method to handle the workflow of a LiberviaPage""" |
994
b92b06f023cb
pages: profile is now always set in template_data, and None if there is no user logged
Goffi <goffi@goffi.org>
parents:
990
diff
changeset
|
1452 |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1453 # template_data are the variables passed to template |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1454 if not hasattr(request, "template_data"): |
957
67bf14c91d5c
server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
1455 session_data = self.host.getSessionData(request, session_iface.ISATSession) |
67bf14c91d5c
server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
1456 csrf_token = session_data.csrf_token |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1457 request.template_data = { |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1458 u"profile": session_data.profile, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1459 u"csrf_token": csrf_token, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1460 } |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1461 |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1462 # XXX: here is the code which need to be executed once |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1463 # at the beginning of the request hanling |
917 | 1464 if request.postpath and not request.postpath[-1]: |
1465 # we don't differenciate URLs finishing with '/' or not | |
1466 del request.postpath[-1] | |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1467 |
917 | 1468 d = defer.Deferred() |
922
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1469 d.addCallback(self._checkAccess, request) |
16d1084d1371
server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents:
921
diff
changeset
|
1470 |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1471 if self.redirect is not None: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1472 d.addCallback( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1473 lambda __: self.pageRedirect( |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1474 self.redirect, request, skip_parse_url=False |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1475 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1476 ) |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1477 |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1478 if self.parse_url is not None and not skip_parse_url: |
1018 | 1479 if self.url_cache: |
1480 profile = self.getProfile(request) | |
1481 try: | |
1482 cache_url = self.cached_urls[profile][request.uri] | |
1483 except KeyError: | |
1484 # no cache for this URI yet | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1485 # we do normal URL parsing, and then the cache |
1018 | 1486 d.addCallback(self.parse_url, request) |
1487 d.addCallback(self._cacheURL, request, profile) | |
1488 else: | |
1489 log.debug(_(u"using URI cache for {page}").format(page=self)) | |
1490 cache_url.use(request) | |
1491 else: | |
1492 d.addCallback(self.parse_url, request) | |
917 | 1493 |
1494 d.addCallback(self._subpagesHandler, request) | |
1495 | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1496 if request.method not in (C.HTTP_METHOD_GET, C.HTTP_METHOD_POST): |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1497 # only HTTP GET and POST are handled so far |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1498 d.addCallback(lambda __: self.pageError(request, C.HTTP_BAD_REQUEST)) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1499 |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1500 if request.method == C.HTTP_METHOD_POST: |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1501 if self.on_data_post is None: |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1502 # if we don't have on_data_post, the page was not expecting POST |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1503 # so we return an error |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1504 d.addCallback(lambda __: self.pageError(request, C.HTTP_BAD_REQUEST)) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1505 else: |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1506 d.addCallback(self._on_data_post, request) |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1507 # by default, POST follow normal behaviour after on_data_post is called |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1508 # this can be changed by a redirection or other method call in on_data_post |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1509 |
998
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1510 if self.dynamic: |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1511 d.addCallback(self._prepare_dynamic, request) |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1512 |
917 | 1513 if self.prepare_render: |
1514 d.addCallback(self._prepare_render, request) | |
1515 | |
1516 if self.template: | |
962 | 1517 d.addCallback(self._render_template, request) |
917 | 1518 elif self.render_method: |
1519 d.addCallback(self._render_method, request) | |
1520 | |
1521 d.addCallback(self.writeData, request) | |
1522 d.addErrback(self._renderEb, request) | |
937
00236973e138
server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents:
936
diff
changeset
|
1523 d.addErrback(self._internalError, request) |
917 | 1524 d.callback(self) |
1525 return server.NOT_DONE_YET | |
1526 | |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1527 def render_GET(self, request): |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1528 return self.renderPage(request) |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1529 |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1530 def render_POST(self, request): |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1531 return self.renderPage(request) |