annotate libervia/server/pages.py @ 1277:2e4fcd31f2a9

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