annotate libervia/server/pages.py @ 1177:75c5e8d21c7d

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