Mercurial > libervia-web
annotate libervia/web/server/pages.py @ 1618:5d9889f14012 default tip @
server: start major redesign
- Add icons to menu items
- Switch menu items representation from tuple to dictionary for future extensibility:
- Include icon information
- Prepare for additional data
- Remove "login" from main menu, add login page URL to template data, as it is now a separate right-aligned item
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Oct 2024 23:07:01 +0200 |
parents | 97ea776df74c |
children |
rev | line source |
---|---|
1239 | 1 #!/usr/bin/env python3 |
2 | |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
336
diff
changeset
|
3 # Libervia: a Salut à Toi frontend |
1396 | 4 # Copyright (C) 2011-2021 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 | 18 |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
19 from __future__ import annotations |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
20 import copy |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
21 from functools import reduce |
1246 | 22 import hashlib |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
23 import json |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
24 import os.path |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
25 from pathlib import Path |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
26 import time |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
27 import traceback |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
28 from typing import List, Optional, Union |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
29 import urllib.error |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
30 import urllib.parse |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
31 import urllib.request |
1246 | 32 |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
33 from twisted.internet import defer |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
34 from twisted.python import failure |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
35 from twisted.python.filepath import FilePath |
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
|
36 from twisted.web import server |
858 | 37 from twisted.web import resource as web_resource |
38 from twisted.web import util as web_util | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
39 from twisted.words.protocols.jabber import jid |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
448
diff
changeset
|
40 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
41 from libervia.backend.core import exceptions |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
42 from libervia.backend.core.i18n import _ |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
43 from libervia.backend.core.log import getLogger |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
44 from libervia.backend.tools.common import date_utils |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
45 from libervia.backend.tools.common import utils |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
46 from libervia.backend.tools.common import data_format |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
47 from libervia.backend.tools.utils import as_deferred |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1514
diff
changeset
|
48 from libervia.frontends.bridge.bridge_frontend import BridgeException |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
49 |
1286
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
50 from . import session_iface |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
51 from .classes import WebsocketMeta |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
52 from .classes import Script |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
53 from .constants import Const as C |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
54 from .resources import LiberviaRootResource |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
55 from .utils import SubPage, quote |
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
|
56 |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
57 log = getLogger(__name__) |
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1144
diff
changeset
|
58 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
59 |
1018 | 60 class CacheBase(object): |
61 def __init__(self): | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
62 self._created = time.time() |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
63 self._last_access = 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 created(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
67 return self._created |
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 @property |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
70 def last_access(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
71 return self._last_access |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
72 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
73 @last_access.setter |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
74 def last_access(self, timestamp): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
75 self._last_access = timestamp |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
76 |
1018 | 77 |
78 class CachePage(CacheBase): | |
79 def __init__(self, rendered): | |
80 super(CachePage, self).__init__() | |
81 self._created = time.time() | |
82 self._last_access = self._created | |
83 self._rendered = rendered | |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
84 self._hash = hashlib.sha256(rendered).hexdigest() |
1018 | 85 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
86 @property |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
87 def rendered(self): |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
88 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
|
89 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
90 @property |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
91 def hash(self): |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
92 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
|
93 |
1018 | 94 |
95 class CacheURL(CacheBase): | |
96 def __init__(self, request): | |
97 super(CacheURL, self).__init__() | |
98 try: | |
1193
aee3d8fa679f
pages: fixed cached data when `url_cache` is used:
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
99 self._data = copy.deepcopy(request.data) |
1018 | 100 except AttributeError: |
101 self._data = {} | |
1193
aee3d8fa679f
pages: fixed cached data when `url_cache` is used:
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
102 self._template_data = copy.deepcopy(request.template_data) |
1018 | 103 self._prepath = request.prepath[:] |
104 self._postpath = request.postpath[:] | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
105 del self._template_data["csrf_token"] |
1018 | 106 |
107 def use(self, request): | |
108 self.last_access = time.time() | |
1193
aee3d8fa679f
pages: fixed cached data when `url_cache` is used:
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
109 request.data = copy.deepcopy(self._data) |
aee3d8fa679f
pages: fixed cached data when `url_cache` is used:
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
110 request.template_data.update(copy.deepcopy(self._template_data)) |
1018 | 111 request.prepath = self._prepath[:] |
112 request.postpath = self._postpath[:] | |
113 | |
114 | |
917 | 115 class LiberviaPage(web_resource.Resource): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
116 isLeaf = True # we handle subpages ourself |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
117 cache = {} |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
118 # 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
|
119 # 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
|
120 cache_pubsub_sub = set() |
917 | 121 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
122 def __init__( |
1417 | 123 self, host, vhost_root, root_dir, url, name=None, label=None, redirect=None, |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
124 access=None, dynamic=True, parse_url=None, add_breadcrumb=None, |
1417 | 125 prepare_render=None, render=None, template=None, on_data_post=None, on_data=None, |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
126 url_cache=False, replace_on_conflict=False |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
127 ): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
128 """Initiate LiberviaPage instance |
917 | 129 |
130 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
|
131 The non mandatory arguments are the variables found in page_meta.py |
917 | 132 @param host(Libervia): the running instance of Libervia |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
133 @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
|
134 handle this page. |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1277
diff
changeset
|
135 @param root_dir(Path): absolute file path of the page |
962 | 136 @param url(unicode): relative URL to the page |
137 this URL may not be valid, as pages may require path arguments | |
917 | 138 @param name(unicode, None): if not None, a unique name to identify the page |
139 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
|
140 "/" 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
|
141 @param redirect(unicode, None): if not None, this page will be redirected. |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
142 A redirected parameter is used as in self.page_redirect. |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
143 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
|
144 using this redirect parameter is called "full redirection" |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
145 using self.page_redirect is called "partial redirection" (because some |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
146 rendering method can still be used, e.g. parse_url) |
917 | 147 @param access(unicode, None): permission needed to access the page |
148 None means public access. | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
149 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
|
150 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
|
151 admins. See C.PAGES_ACCESS_* for details |
995 | 152 @param dynamic(bool): if True, activate websocket for bidirectional communication |
917 | 153 @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
|
154 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
|
155 (request.postpath) else a the request will be transmitted to a subpage |
1417 | 156 @param add_breadcrumb(callable, None): if set, manage the breadcrumb data for this |
157 page, otherwise it will be set automatically from page name or label. | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
158 @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
|
159 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
|
160 @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
|
161 called and what it returns will be rendered. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
162 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
|
163 string. |
917 | 164 @param template(unicode, None): path to the template to render. |
165 This method is mutually exclusive with render | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
166 @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
|
167 None if data post is not handled |
1284
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
168 "continue" if data post is not handled there, and we must not interrupt |
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
169 workflow (i.e. it's handled in "render" method). |
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
170 otherwise, on_data_post can return a string with following value: |
962 | 171 - 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
|
172 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
|
173 - 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
|
174 as a notification |
995 | 175 @param on_data(callable, None): method to call when dynamic data is sent |
176 this method is used with Libervia's websocket mechanism | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
177 @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
|
178 Useful when costly calls (e.g. network) are done while parsing URL. |
1153 | 179 @param replace_on_conflict(boolean): if True, don't raise ConflictError if a |
180 page of this name already exists, but replace it | |
917 | 181 """ |
182 | |
183 web_resource.Resource.__init__(self) | |
184 self.host = host | |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
185 self.vhost_root = vhost_root |
917 | 186 self.root_dir = root_dir |
962 | 187 self.url = url |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
188 self.name = name |
1417 | 189 self.label = label |
1246 | 190 self.dyn_data = {} |
917 | 191 if name is not None: |
1153 | 192 if (name in self.named_pages |
193 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
|
194 raise exceptions.ConflictError( |
1216 | 195 _('a Libervia page named "{}" already exists'.format(name))) |
196 if "/" in name: | |
197 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
|
198 if not name: |
1216 | 199 raise ValueError(_("a page name can't be empty")) |
917 | 200 self.named_pages[name] = self |
201 if access is None: | |
202 access = C.PAGES_ACCESS_PUBLIC | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
203 if access not in ( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
204 C.PAGES_ACCESS_PUBLIC, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
205 C.PAGES_ACCESS_PROFILE, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
206 C.PAGES_ACCESS_NONE, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
207 ): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
208 raise NotImplementedError( |
1216 | 209 _("{} access is not implemented yet").format(access) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
210 ) |
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
|
211 self.access = access |
995 | 212 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
|
213 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
|
214 # 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
|
215 # 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
|
216 if not all( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
217 lambda x: x is not None |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
218 for x in (parse_url, prepare_render, render, template) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
219 ): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
220 raise ValueError( |
1216 | 221 _("you can't use full page redirection with other rendering" |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
222 "method, check self.page_redirect 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
|
223 self.redirect = redirect |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
224 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
225 self.redirect = None |
917 | 226 self.parse_url = parse_url |
1417 | 227 self.add_breadcrumb = add_breadcrumb |
917 | 228 self.prepare_render = prepare_render |
229 self.template = template | |
230 self.render_method = render | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
231 self.on_data_post = on_data_post |
995 | 232 self.on_data = on_data |
1018 | 233 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
|
234 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
|
235 # 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
|
236 return |
1059
d127a85b2fee
pages: don't log error anymore when empty page_meta.py are used
Goffi <goffi@goffi.org>
parents:
1056
diff
changeset
|
237 if template is not None and render is not None: |
1216 | 238 log.error(_("render and template methods can't be used at the same time")) |
917 | 239 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
240 # if not None, next rendering will be cached |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
241 # 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
|
242 # 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
|
243 self._do_cache = None |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
244 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
245 def __str__(self): |
1216 | 246 return "LiberviaPage {name} at {url} (vhost: {vhost_root})".format( |
247 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
|
248 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
249 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
250 def named_pages(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
251 return self.vhost_root.named_pages |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
252 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
253 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
254 def uri_callbacks(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
255 return self.vhost_root.uri_callbacks |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
256 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
257 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
258 def pages_redirects(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
259 return self.vhost_root.pages_redirects |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
260 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
261 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
262 def cached_urls(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
263 return self.vhost_root.cached_urls |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
264 |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
265 @property |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
266 def main_menu(self): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
267 return self.vhost_root.main_menu |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
268 |
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
|
269 @property |
1618 | 270 def login_url(self): |
271 return self.vhost_root.login_url | |
272 | |
273 @property | |
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
|
274 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
|
275 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
|
276 |
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
|
277 |
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
|
278 @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
|
279 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
|
280 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
|
281 |
1153 | 282 @staticmethod |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
283 def create_page(host, meta_path, vhost_root, url_elts, replace_on_conflict=False): |
1153 | 284 """Create a LiberviaPage instance |
285 | |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
286 @param meta_path(Path): path to the page_meta.py file |
1153 | 287 @param vhost_root(resource.Resource): root resource of the virtual host |
288 @param url_elts(list[unicode]): list of path element from root site to this page | |
289 @param replace_on_conflict(bool): same as for [LiberviaPage] | |
290 @return (tuple[dict, LiberviaPage]): tuple with: | |
291 - page_data: dict containing data of the page | |
292 - libervia_page: created resource | |
293 """ | |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
294 dir_path = meta_path.parent |
1216 | 295 page_data = {"__name__": ".".join(["page"] + url_elts)} |
1153 | 296 # we don't want to force the presence of __init__.py |
297 # so we use execfile instead of import. | |
298 # TODO: when moved to Python 3, __init__.py is not mandatory anymore | |
299 # so we can switch to import | |
1216 | 300 exec(compile(open(meta_path, "rb").read(), meta_path, 'exec'), page_data) |
1153 | 301 return page_data, LiberviaPage( |
302 host=host, | |
303 vhost_root=vhost_root, | |
304 root_dir=dir_path, | |
1216 | 305 url="/" + "/".join(url_elts), |
306 name=page_data.get("name"), | |
1417 | 307 label=page_data.get("label"), |
1216 | 308 redirect=page_data.get("redirect"), |
309 access=page_data.get("access"), | |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
310 dynamic=page_data.get("dynamic", True), |
1216 | 311 parse_url=page_data.get("parse_url"), |
1417 | 312 add_breadcrumb=page_data.get("add_breadcrumb"), |
1216 | 313 prepare_render=page_data.get("prepare_render"), |
314 render=page_data.get("render"), | |
315 template=page_data.get("template"), | |
316 on_data_post=page_data.get("on_data_post"), | |
317 on_data=page_data.get("on_data"), | |
318 url_cache=page_data.get("url_cache", False), | |
1153 | 319 replace_on_conflict=replace_on_conflict |
320 ) | |
321 | |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
322 @staticmethod |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
323 def create_browser_data( |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
324 vhost_root, |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
325 resource: Optional[LiberviaPage], |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
326 browser_path: Path, |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
327 path_elts: Optional[List[str]], |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
328 engine: str = "brython" |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
329 ) -> None: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
330 """create and store data for browser dynamic code""" |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
331 dyn_data = { |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
332 "path": browser_path, |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
333 "url_hash": ( |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
334 hashlib.sha256('/'.join(path_elts).encode()).hexdigest() |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
335 if path_elts is not None else None |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
336 ), |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
337 } |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
338 browser_meta_path = browser_path / C.PAGES_BROWSER_META_FILE |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
339 if browser_meta_path.is_file(): |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
340 with browser_meta_path.open() as f: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
341 browser_meta = json.load(f) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
342 utils.recursive_update(vhost_root.browser_modules, browser_meta) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
343 if resource is not None: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
344 utils.recursive_update(resource.dyn_data, browser_meta) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
345 |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
346 init_path = browser_path / '__init__.py' |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
347 if init_path.is_file(): |
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 if resource is not None: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
351 resource.dyn_data[engine] = dyn_data |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
352 elif path_elts is None: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
353 try: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
354 next(browser_path.glob('*.py')) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
355 except StopIteration: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
356 # no python file, nothing for Brython |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
357 pass |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
358 else: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
359 vhost_root.browser_modules.setdefault( |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
360 engine, []).append(dyn_data) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
361 |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
362 |
925 | 363 @classmethod |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
364 def import_pages(cls, host, vhost_root, root_path=None, _parent=None, _path=None, |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
365 _extra_pages=False): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
366 """Recursively import Libervia pages |
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 @param host(Libervia): Libervia instance |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
369 @param vhost_root(LiberviaRootResource): root of this VirtualHost |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
370 @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
|
371 Used to add default site pages to external sites |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
372 @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
|
373 internal use only |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
374 @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
|
375 internal use only |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
376 @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
|
377 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
|
378 """ |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
379 if _path is None: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
380 _path = [] |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
381 if _parent is None: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
382 if root_path is None: |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
383 root_dir = vhost_root.site_path / C.PAGES_DIR |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
384 else: |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
385 root_dir = root_path / C.PAGES_DIR |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
386 _extra_pages = True |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
387 _parent = vhost_root |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
388 root_browser_path = root_dir / C.PAGES_BROWSER_DIR |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
389 if root_browser_path.is_dir(): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
390 cls.create_browser_data(vhost_root, None, root_browser_path, None) |
917 | 391 else: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
392 root_dir = _parent.root_dir |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
393 |
917 | 394 for d in os.listdir(root_dir): |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
395 dir_path = root_dir / d |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
396 if not dir_path.is_dir(): |
917 | 397 continue |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
398 if _extra_pages and d in _parent.children: |
1216 | 399 log.debug(_("[{host_name}] {path} is already present, ignoring it") |
400 .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
|
401 continue |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
402 meta_path = dir_path / C.PAGES_META_FILE |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
403 if meta_path.is_file(): |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
404 new_path = _path + [d] |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
405 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
406 page_data, resource = cls.create_page( |
1246 | 407 host, meta_path, vhost_root, new_path) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
408 except exceptions.ConflictError as e: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
409 if _extra_pages: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
410 # 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
|
411 continue |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
412 else: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
413 raise e |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1246
diff
changeset
|
414 _parent.putChild(str(d).encode(), resource) |
1216 | 415 log_msg = ("[{host_name}] Added /{path} page".format( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
416 host_name=vhost_root.host_name, |
1216 | 417 path="[…]/".join(new_path))) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
418 if _extra_pages: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
419 log.debug(log_msg) |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
420 else: |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
421 log.info(log_msg) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
422 if "uri_handlers" in page_data: |
925 | 423 if not isinstance(page_data, dict): |
1216 | 424 log.error(_("uri_handlers must be a dict")) |
925 | 425 else: |
1216 | 426 for uri_tuple, cb_name in page_data["uri_handlers"].items(): |
427 if len(uri_tuple) != 2 or not isinstance(cb_name, str): | |
428 log.error(_("invalid uri_tuple")) | |
925 | 429 continue |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
430 if not _extra_pages: |
1216 | 431 log.info(_("setting {}/{} URIs handler") |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
432 .format(*uri_tuple)) |
925 | 433 try: |
434 cb = page_data[cb_name] | |
435 except KeyError: | |
1216 | 436 log.error(_("missing {name} method to handle {1}/{2}") |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
437 .format(name=cb_name, *uri_tuple)) |
925 | 438 continue |
439 else: | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
440 resource.register_uri(uri_tuple, cb) |
925 | 441 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
442 LiberviaPage.import_pages( |
1153 | 443 host, vhost_root, _parent=resource, _path=new_path, |
444 _extra_pages=_extra_pages) | |
1246 | 445 # 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
|
446 browser_path = dir_path / C.PAGES_BROWSER_DIR |
1246 | 447 if browser_path.is_dir(): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
448 cls.create_browser_data(vhost_root, resource, browser_path, new_path) |
1153 | 449 |
450 @classmethod | |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
451 def on_file_change( |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
452 cls, |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
453 host, |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
454 file_path: FilePath, |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
455 flags: List[str], |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
456 site_root: LiberviaRootResource, |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
457 site_path: Path |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
458 ) -> None: |
1153 | 459 """Method triggered by file_watcher when something is changed in files |
460 | |
461 This method is used in dev mode to reload pages when needed | |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
462 @param file_path: path of the file which triggered the event |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
463 @param flags: human readable flags of the event (from |
1153 | 464 internet.inotify) |
1514
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
465 @param site_root: root of the site |
16228994ca3b
server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
466 @param site_path: absolute path of the site |
1153 | 467 """ |
468 if flags == ['create']: | |
469 return | |
1277
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
470 path = Path(file_path.path.decode()) |
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
471 base_name = path.name |
1216 | 472 if base_name != "page_meta.py": |
1153 | 473 # we only handle libervia pages |
474 return | |
475 | |
1216 | 476 log.debug("{flags} event(s) received for {file_path}".format( |
477 flags=", ".join(flags), file_path=file_path)) | |
1153 | 478 |
1277
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
479 dir_path = path.parent |
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
480 |
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
481 if dir_path == site_path: |
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
482 return |
1153 | 483 |
1277
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
484 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
|
485 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
|
486 |
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
487 path_elts = list(dir_path.relative_to(site_path).parts) |
1153 | 488 |
489 if path_elts[0] == C.PAGES_DIR: | |
490 # a page has been modified | |
491 del path_elts[0] | |
492 if not path_elts: | |
493 # we need at least one element to parse | |
494 return | |
495 # we retrieve page by starting from site root and finding each path element | |
496 parent = page = site_root | |
497 new_page = False | |
498 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
|
499 child_name = child_name.encode() |
1153 | 500 try: |
1157
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
501 try: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
502 page = page.original.children[child_name] |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
503 except AttributeError: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
504 page = page.children[child_name] |
1153 | 505 except KeyError: |
506 if idx != len(path_elts)-1: | |
507 # a page has been created in a subdir when one or more | |
508 # page_meta.py are missing on the way | |
1216 | 509 log.warning(_("Can't create a page at {path}, missing parents") |
1153 | 510 .format(path=path)) |
511 return | |
512 new_page = True | |
513 else: | |
514 if idx<len(path_elts)-1: | |
1423
870b198e98ea
pages: fix `onFileChange` when the page is not wrapped
Goffi <goffi@goffi.org>
parents:
1417
diff
changeset
|
515 try: |
870b198e98ea
pages: fix `onFileChange` when the page is not wrapped
Goffi <goffi@goffi.org>
parents:
1417
diff
changeset
|
516 parent = page.original |
870b198e98ea
pages: fix `onFileChange` when the page is not wrapped
Goffi <goffi@goffi.org>
parents:
1417
diff
changeset
|
517 except AttributeError: |
870b198e98ea
pages: fix `onFileChange` when the page is not wrapped
Goffi <goffi@goffi.org>
parents:
1417
diff
changeset
|
518 parent = page |
1153 | 519 |
520 try: | |
521 # we (re)create a page with the new/modified code | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
522 __, resource = cls.create_page(host, path, site_root, path_elts, |
1153 | 523 replace_on_conflict=True) |
524 if not new_page: | |
1157
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
525 try: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
526 resource.children = page.original.children |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
527 except AttributeError: |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
528 # FIXME: this .original handling madness is due to EncodingResourceWrapper |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
529 # EncodingResourceWrapper should probably be removed |
64952ba7affe
pages: fixed children retrieval in onFileChange
Goffi <goffi@goffi.org>
parents:
1153
diff
changeset
|
530 resource.children = page.children |
1153 | 531 except Exception as e: |
1216 | 532 log.warning(_("Can't create page: {reason}").format(reason=e)) |
1153 | 533 else: |
1277
2e4fcd31f2a9
pages: use Path in onFileChange + fixed encoding issue
Goffi <goffi@goffi.org>
parents:
1276
diff
changeset
|
534 url_elt = path_elts[-1].encode() |
1153 | 535 if not new_page: |
536 # the page was already existing, we remove it | |
537 del parent.children[url_elt] | |
538 # we can now add the new page | |
539 parent.putChild(url_elt, resource) | |
1380
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
540 |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
541 # is there any browser data to create? |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
542 browser_path = resource.root_dir / C.PAGES_BROWSER_DIR |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
543 if browser_path.is_dir(): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
544 cls.create_browser_data( |
1380
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
545 resource.vhost_root, |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
546 resource, |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
547 browser_path, |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
548 resource.url.split('/') |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
549 ) |
df79874947d7
pages: fix browser data re-creation on dev mode reload
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
550 |
1153 | 551 if new_page: |
1216 | 552 log.info(_("{page} created").format(page=resource)) |
1153 | 553 else: |
1216 | 554 log.info(_("{page} reloaded").format(page=resource)) |
990
6daa59d44ee2
pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents:
985
diff
changeset
|
555 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
556 def check_csrf(self, request): |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
557 session = self.host.get_session_data( |
1506 | 558 request, session_iface.IWebSession |
1479
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
559 ) |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
560 if session.profile is None: |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
561 # CSRF doesn't make sense when no user is logged |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
562 log.debug("disabling CSRF check because service profile is used") |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
563 return |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
564 csrf_token = session.csrf_token |
1283
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
565 given_csrf = request.getHeader("X-Csrf-Token") |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
566 if given_csrf is None: |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
567 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
568 given_csrf = self.get_posted_data(request, "csrf_token") |
1283
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
569 except KeyError: |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
570 pass |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
571 if given_csrf is None or given_csrf != csrf_token: |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
572 log.warning( |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
573 _("invalid CSRF token, hack attempt? URL: {url}, IP: {ip}").format( |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
574 url=request.uri, ip=request.getClientIP() |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
575 ) |
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
576 ) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
577 self.page_error(request, C.HTTP_FORBIDDEN) |
1283
436ef2ad92af
pages: moved CSRF checking code to a separate method:
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
578 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
579 def expose_to_scripts( |
1286
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
580 self, |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
581 request: server.Request, |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
582 **kwargs: str |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
583 ) -> None: |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
584 """Make a local variable available to page script as a global variable |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
585 |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
586 No check is done for conflicting name, use this carefully |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
587 """ |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
588 template_data = request.template_data |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
589 scripts = template_data.setdefault("scripts", utils.OrderedSet()) |
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
590 for name, value in kwargs.items(): |
1304
70e3341d25bf
pages: take care of None/null when exposing a variable
Goffi <goffi@goffi.org>
parents:
1302
diff
changeset
|
591 if value is None: |
70e3341d25bf
pages: take care of None/null when exposing a variable
Goffi <goffi@goffi.org>
parents:
1302
diff
changeset
|
592 value = "null" |
1471
83dce05226ac
pages: workaround to expose variables coming from python-dbus
Goffi <goffi@goffi.org>
parents:
1465
diff
changeset
|
593 elif isinstance(value, str): |
83dce05226ac
pages: workaround to expose variables coming from python-dbus
Goffi <goffi@goffi.org>
parents:
1465
diff
changeset
|
594 # FIXME: workaround for subtype used by python-dbus (dbus.String) |
83dce05226ac
pages: workaround to expose variables coming from python-dbus
Goffi <goffi@goffi.org>
parents:
1465
diff
changeset
|
595 # to be removed when we get rid of python-dbus |
83dce05226ac
pages: workaround to expose variables coming from python-dbus
Goffi <goffi@goffi.org>
parents:
1465
diff
changeset
|
596 value = repr(str(value)) |
1304
70e3341d25bf
pages: take care of None/null when exposing a variable
Goffi <goffi@goffi.org>
parents:
1302
diff
changeset
|
597 else: |
70e3341d25bf
pages: take care of None/null when exposing a variable
Goffi <goffi@goffi.org>
parents:
1302
diff
changeset
|
598 value = repr(value) |
70e3341d25bf
pages: take care of None/null when exposing a variable
Goffi <goffi@goffi.org>
parents:
1302
diff
changeset
|
599 scripts.add(Script(content=f"var {name}={value};")) |
1286
63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
Goffi <goffi@goffi.org>
parents:
1285
diff
changeset
|
600 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
601 def register_uri(self, uri_tuple, get_uri_cb): |
1153 | 602 """Register a URI handler |
925 | 603 |
604 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
605 type/subtype as returned by tools/common/parse_xmpp_uri |
1038 | 606 or type/None to handle all subtypes |
925 | 607 @param get_uri_cb(callable): method which take uri_data dict as only argument |
1038 | 608 and return absolute path with correct arguments or None if the page |
609 can't handle this URL | |
925 | 610 """ |
1038 | 611 if uri_tuple in self.uri_callbacks: |
1216 | 612 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
|
613 .format( *uri_tuple)) |
1038 | 614 self.uri_callbacks[uri_tuple] = (self, get_uri_cb) |
925 | 615 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
616 def config_get(self, key, default=None, value_type=None): |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
617 return self.host.config_get(self.vhost_root, key=key, default=default, |
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
|
618 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
|
619 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
620 def get_build_path(self, session_data): |
1146
76d75423ef53
server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
621 return session_data.cache_dir + self.vhost.site_name |
76d75423ef53
server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
622 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
623 def get_page_by_name(self, name): |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
624 return self.vhost_root.get_page_by_name(name) |
927
bb4dfc2802c0
server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents:
926
diff
changeset
|
625 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
626 def get_page_path_from_uri(self, uri): |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
627 return self.vhost_root.get_page_path_from_uri(uri) |
936
78692d47340d
server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
628 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
629 def get_page_redirect_url(self, request, page_name="login", url=None): |
962 | 630 """generate URL for a page with redirect_url parameter set |
631 | |
632 mainly used for login page with redirection to current page | |
633 @param request(server.Request): current HTTP request | |
634 @param page_name(unicode): name of the page to go | |
635 @param url(None, unicode): url to redirect to | |
636 None to use request path (i.e. current page) | |
637 @return (unicode): URL to use | |
638 """ | |
1216 | 639 return "{root_url}?redirect_url={redirect_url}".format( |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
640 root_url=self.get_page_by_name(page_name).url, |
1216 | 641 redirect_url=urllib.parse.quote_plus(request.uri) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
642 if url is None |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
643 else url.encode("utf-8"), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
644 ) |
962 | 645 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
646 def get_url(self, *args: str, **kwargs: str) -> str: |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
647 """retrieve URL of the page set arguments |
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
648 |
1456
284522d8af44
pages: check redirections for LiberviaPage.getURL:
Goffi <goffi@goffi.org>
parents:
1455
diff
changeset
|
649 @param *args: arguments to add to the URL as path elements empty or None |
284522d8af44
pages: check redirections for LiberviaPage.getURL:
Goffi <goffi@goffi.org>
parents:
1455
diff
changeset
|
650 arguments will be ignored |
1463
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
651 @param **kwargs: query parameters |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
652 """ |
1014
dfced7992460
pages: ignore empty or unset arguments in getURL
Goffi <goffi@goffi.org>
parents:
1013
diff
changeset
|
653 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
|
654 |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
655 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
|
656 # we check for redirection |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
657 redirect_data = self.pages_redirects[self.name] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
658 args_hash = tuple(args) |
1476
c669b5bfb8a0
pages: fix args range in `getURL` + use `urljoin`:
Goffi <goffi@goffi.org>
parents:
1473
diff
changeset
|
659 for limit in range(len(args), -1, -1): |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
660 current_hash = args_hash[:limit] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
661 if current_hash in redirect_data: |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
662 url_base = redirect_data[current_hash] |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
663 remaining = args[limit:] |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
664 remaining_url = "/".join(remaining) |
1476
c669b5bfb8a0
pages: fix args range in `getURL` + use `urljoin`:
Goffi <goffi@goffi.org>
parents:
1473
diff
changeset
|
665 url = urllib.parse.urljoin(url_base, remaining_url) |
1463
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
666 break |
1473
b4cead3cea43
pages: fix crash when page is redirected but with different args
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
667 else: |
b4cead3cea43
pages: fix crash when page is redirected but with different args
Goffi <goffi@goffi.org>
parents:
1471
diff
changeset
|
668 url = os.path.join(self.url, *url_args) |
1463
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
669 else: |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
670 url = os.path.join(self.url, *url_args) |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
671 |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
672 if kwargs: |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
673 encoded = urllib.parse.urlencode( |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
674 {k: v for k, v in kwargs.items()} |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
675 ) |
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
676 url += f"?{encoded}" |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
677 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
678 return self.host.check_redirection( |
1456
284522d8af44
pages: check redirections for LiberviaPage.getURL:
Goffi <goffi@goffi.org>
parents:
1455
diff
changeset
|
679 self.vhost_root, |
1463
2c8449885272
pages: query parameters can now be specified using keyword arguments in `getURL:`
Goffi <goffi@goffi.org>
parents:
1456
diff
changeset
|
680 url |
1456
284522d8af44
pages: check redirections for LiberviaPage.getURL:
Goffi <goffi@goffi.org>
parents:
1455
diff
changeset
|
681 ) |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
682 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
683 def get_current_url(self, request): |
1015 | 684 """retrieve URL used to access this page |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
685 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
686 @return(unicode): current URL |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
687 """ |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
688 # 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
|
689 # request.prepath) because request.prepath may have been modified by |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
690 # redirection (if redirection args have been specified), while path reflect |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
691 # the real request |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
692 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
693 # we ignore empty path elements (i.e. double '/' or '/' at the end) |
1216 | 694 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
|
695 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
696 if request.postpath: |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
697 if not request.postpath[-1]: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
698 # we remove trailing slash |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
699 request.postpath = request.postpath[:-1] |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
700 if request.postpath: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
701 # get_sub_page_url must return subpage from the point where |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
702 # the it is called, so we have to remove remanining |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
703 # path elements |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
704 path_elts = path_elts[: -len(request.postpath)] |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
705 |
1216 | 706 return "/" + "/".join(path_elts) |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
707 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
708 def get_param_url(self, request, **kwargs): |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
709 """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
|
710 |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
711 **kwargs(dict[str, unicode]): argument to use as query parameters |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
712 @return (unicode): constructed URL |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
713 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
714 current_url = self.get_current_url(request) |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
715 if kwargs: |
1216 | 716 encoded = urllib.parse.urlencode( |
717 {k: v for k, v in kwargs.items()} | |
718 ) | |
719 current_url = current_url + "?" + encoded | |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
720 return current_url |
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
721 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
722 def get_sub_page_by_name(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
|
723 """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
|
724 |
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
|
725 @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
|
726 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
|
727 @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
|
728 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
|
729 @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
|
730 @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
|
731 """ |
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
|
732 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
|
733 parent = self |
1216 | 734 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
|
735 try: |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
736 child_name = child.name |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
737 except AttributeError: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
738 # 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
|
739 continue |
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
740 if child_name == subpage_name: |
1216 | 741 return path.decode('utf-8'), child |
1378
e3e303a30a74
pages (tickets): renamed "tickets" to "lists":
Goffi <goffi@goffi.org>
parents:
1376
diff
changeset
|
742 raise exceptions.NotFound( |
e3e303a30a74
pages (tickets): renamed "tickets" to "lists":
Goffi <goffi@goffi.org>
parents:
1376
diff
changeset
|
743 _("requested sub page has not been found ({subpage_name})").format( |
e3e303a30a74
pages (tickets): renamed "tickets" to "lists":
Goffi <goffi@goffi.org>
parents:
1376
diff
changeset
|
744 subpage_name=subpage_name)) |
1030
66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
Goffi <goffi@goffi.org>
parents:
1019
diff
changeset
|
745 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
746 def get_sub_page_url(self, request, page_name, *args): |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
747 """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
|
748 |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
749 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
|
750 i.e. it's more prepath than path). |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
751 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
|
752 by the resulting combination. |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
753 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
|
754 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
|
755 names of directories (i.e. relative URL will break if subdirectory is renamed |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
756 while get_sub_page_url won't as long as page_name is consistent). |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
757 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
|
758 and potential redirections. |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
759 @param request(server.Request): current HTTP request |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
760 @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
|
761 it must be a direct children of current page |
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
762 @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
|
763 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
|
764 @return (unicode): absolute URL to the sub page |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
765 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
766 current_url = self.get_current_url(request) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
767 path, child = self.get_sub_page_by_name(page_name) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
768 return os.path.join( |
1216 | 769 "/", 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
|
770 ) |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
771 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
772 def get_url_by_names(self, named_path): |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1169
diff
changeset
|
773 """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
|
774 |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
775 @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
|
776 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
|
777 - 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
|
778 - 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
|
779 @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
|
780 @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
|
781 """ |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
782 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
|
783 path = [] |
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
784 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
|
785 if current_page is None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
786 current_page = self.get_page_by_name(page_name) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
787 path.append(current_page.get_url(*page_args)) |
1031
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
788 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
789 sub_path, current_page = self.get_sub_page_by_name( |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
790 page_name, parent=current_page |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
791 ) |
1031
4ba7df23b976
pages: new method getURLByNames to retrieve URL from list of page names/path arguments
Goffi <goffi@goffi.org>
parents:
1030
diff
changeset
|
792 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
|
793 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
|
794 path.extend([quote(a) for a in page_args]) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
795 return self.host.check_redirection(self.vhost_root, "/".join(path)) |
980
bcacf970f970
core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
796 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
797 def get_url_by_path(self, *args): |
1171
469d0de8da0e
pages (blog, u): added atom feed link in "links" template data.
Goffi <goffi@goffi.org>
parents:
1169
diff
changeset
|
798 """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
|
799 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
800 this method as a similar effect as get_url_by_names, but it is more readable |
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
|
801 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
|
802 @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
|
803 - 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
|
804 - 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
|
805 @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
|
806 """ |
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
|
807 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
|
808 if not args: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
809 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
|
810 # 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
|
811 # 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
|
812 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
|
813 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
|
814 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
815 root = self.get_page_by_name(args.pop(0)) |
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
|
816 # 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
|
817 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
|
818 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
|
819 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
|
820 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
|
821 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
|
822 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
|
823 if not url_elts: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
824 url_elts.append(root.get_url(*arguments)) |
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
|
825 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
|
826 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
|
827 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
|
828 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
|
829 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
830 path, current_page = current_page.get_sub_page_by_name(args.pop(0)) |
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
|
831 arguments = [path] |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
832 return self.host.check_redirection(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
|
833 |
917 | 834 def getChildWithDefault(self, path, request): |
835 # we handle children ourselves | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
836 raise exceptions.InternalError( |
1216 | 837 "this method should not be used with LiberviaPage" |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
838 ) |
917 | 839 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
840 def next_path(self, request): |
917 | 841 """get next URL path segment, and update request accordingly |
842 | |
843 will move first segment of postpath in prepath | |
844 @param request(server.Request): current HTTP request | |
845 @return (unicode): unquoted segment | |
846 @raise IndexError: there is no segment left | |
847 """ | |
848 pathElement = request.postpath.pop(0) | |
849 request.prepath.append(pathElement) | |
1216 | 850 return urllib.parse.unquote(pathElement.decode('utf-8')) |
917 | 851 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
852 def _filter_path_value(self, value, handler, name, request): |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
853 """Modify a path value according to handler (see [get_path_args])""" |
1216 | 854 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
|
855 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
|
856 |
1216 | 857 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
|
858 if value is None: |
1216 | 859 return "" |
860 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
|
861 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
|
862 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
|
863 return jid.JID(value) |
1317
a7ab4d244982
pages: handle `jid.InvalidFormat` exception in `_filterPathValue`
Goffi <goffi@goffi.org>
parents:
1305
diff
changeset
|
864 except (RuntimeError, jid.InvalidFormat): |
1216 | 865 log.warning(_("invalid jid argument: {value}").format(value=value)) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
866 self.page_error(request, C.HTTP_BAD_REQUEST) |
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
|
867 else: |
1216 | 868 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
|
869 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
|
870 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
|
871 |
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
|
872 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
|
873 |
1592
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
874 def get_path_args( |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
875 self, |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
876 request: server.Request, |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
877 names: list[str], |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
878 min_args: int = 0, |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
879 **kwargs |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
880 ) -> None: |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
881 """Get several path arguments at once. |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
882 |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
883 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
|
884 Missing arguments will have None value |
1592
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
885 @param names: list of arguments to get |
291a7026cb2b
server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents:
1580
diff
changeset
|
886 @param min_args: if less than min_args are found, PageError is used with |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
887 C.HTTP_BAD_REQUEST |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
888 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
|
889 @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
|
890 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
|
891 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
|
892 - '': 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
|
893 - '@': 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
|
894 - '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
|
895 string is used |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
896 - '@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
|
897 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
|
898 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
899 data = self.get_r_data(request) |
1056
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
900 |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
901 for idx, name in enumerate(names): |
1216 | 902 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
|
903 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
|
904 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
|
905 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
906 value.append(self.next_path(request)) |
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
|
907 except IndexError: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
908 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
|
909 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
|
910 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
911 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
|
912 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
|
913 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
914 value = data[name] = self.next_path(request) |
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
|
915 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
|
916 data[name] = None |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
917 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
|
918 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
|
919 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
920 values_count = idx + 1 |
1071
8f77e36cd51d
pages: fixed args count in getPathArgs
Goffi <goffi@goffi.org>
parents:
1065
diff
changeset
|
921 if values_count < min_args: |
1216 | 922 log.warning(_("Missing arguments in URL (got {count}, expected at least " |
923 "{min_args})").format(count=values_count, min_args=min_args)) | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
924 self.page_error(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
|
925 |
1071
8f77e36cd51d
pages: fixed args count in getPathArgs
Goffi <goffi@goffi.org>
parents:
1065
diff
changeset
|
926 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
|
927 data[name] = None |
47c354ca66a3
pages: new getPathArgs helper method to retrieve several path arguments at once
Goffi <goffi@goffi.org>
parents:
1054
diff
changeset
|
928 |
1216 | 929 for name, handler in kwargs.items(): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
930 if name[0] == "*": |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
931 data[name] = [ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
932 self._filter_path_value(v, handler, name, request) for v in data[name] |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
933 ] |
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
|
934 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
935 data[name] = self._filter_path_value(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
|
936 |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
937 ## Pagination/Filtering ## |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
938 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
939 def get_pubsub_extra(self, request, page_max=10, params=None, extra=None, |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
940 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
|
941 """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
|
942 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
943 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
|
944 - 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
|
945 - 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
|
946 @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
|
947 @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
|
948 @param params(None, dict[unicode, list[unicode]]): params as returned by |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
949 self.get_all_posted_data. |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
950 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
|
951 @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
|
952 @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
|
953 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
|
954 @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
|
955 """ |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
956 if params is None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
957 params = self.get_all_posted_data(request, multiple=False) |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
958 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
|
959 extra = {} |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
960 else: |
1216 | 961 assert not {"rsm_max", "rsm_after", "rsm_before", |
962 C.KEY_ORDER_BY}.intersection(list(extra.keys())) | |
1464
a8435aebfbcc
pages: `page_max` query parameter can now be used on pubsub based pages:
Goffi <goffi@goffi.org>
parents:
1463
diff
changeset
|
963 extra["rsm_max"] = params.get("page_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
|
964 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
|
965 extra[C.KEY_ORDER_BY] = order_by |
1216 | 966 if 'after' in params: |
967 extra['rsm_after'] = params['after'] | |
968 elif 'before' in params: | |
969 extra['rsm_before'] = params['before'] | |
1433
1a5410981c45
pages: get last page by default with RSM:
Goffi <goffi@goffi.org>
parents:
1423
diff
changeset
|
970 else: |
1a5410981c45
pages: get last page by default with RSM:
Goffi <goffi@goffi.org>
parents:
1423
diff
changeset
|
971 # RSM returns list in order (oldest first), but we want most recent first |
1a5410981c45
pages: get last page by default with RSM:
Goffi <goffi@goffi.org>
parents:
1423
diff
changeset
|
972 # so we start by the end |
1a5410981c45
pages: get last page by default with RSM:
Goffi <goffi@goffi.org>
parents:
1423
diff
changeset
|
973 extra['rsm_before'] = "" |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
974 return extra |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
975 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
976 def set_pagination(self, request: server.Request, pubsub_data: dict) -> None: |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1255
diff
changeset
|
977 """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
|
978 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
979 "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
|
980 "before" and "after" URL parameters |
1465
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
981 @param request: current HTTP request |
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
982 @param pubsub_data: pubsub metadata |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
983 """ |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
984 template_data = request.template_data |
1410
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
985 extra = {} |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
986 try: |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1294
diff
changeset
|
987 rsm = pubsub_data["rsm"] |
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1294
diff
changeset
|
988 last_id = 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
|
989 except KeyError: |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
990 # 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
|
991 return |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
992 |
1410
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
993 # if we have a search query, we must keep it |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
994 search = self.get_posted_data(request, 'search', raise_on_missing=False) |
1410
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
995 if search is not None: |
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
996 extra['search'] = search.strip() |
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
997 |
1465
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
998 # same for page_max |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
999 page_max = self.get_posted_data(request, 'page_max', raise_on_missing=False) |
1465
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
1000 if page_max is not None: |
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
1001 extra['page_max'] = page_max |
a410db730301
pages: keep `page_max` if set in query params in `setPagination`:
Goffi <goffi@goffi.org>
parents:
1464
diff
changeset
|
1002 |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1294
diff
changeset
|
1003 if rsm.get("index", 1) > 0: |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
1004 # 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
|
1005 # 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
|
1006 # as we can't know if we are on the first page or not. |
1302
04e7dd6b6f4d
pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents:
1294
diff
changeset
|
1007 first_id = rsm["first"] |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1008 template_data['previous_page_url'] = self.get_param_url( |
1410
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
1009 request, before=first_id, **extra) |
1216 | 1010 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
|
1011 # 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
|
1012 # can't know where we are in the feed in this case. |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1013 template_data['next_page_url'] = self.get_param_url( |
1410
80683be8d341
pages: keep `search` query when using pagination
Goffi <goffi@goffi.org>
parents:
1405
diff
changeset
|
1014 request, after=last_id, **extra) |
1141
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
1015 |
02fc28aac2b6
pages: move pagination core from blog to LiberviaPage so it can be reused:
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
1016 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1017 ## Cache handling ## |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1018 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1019 def _set_cache_headers(self, request, cache): |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1020 """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
|
1021 request.setHeader("ETag", cache.hash) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1022 last_modified = self.host.get_http_date(cache.created) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1023 request.setHeader("Last-Modified", last_modified) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1024 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1025 def _check_cache_headers(self, request, cache): |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1026 """Check if a cache condition is set on the request |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1027 |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1028 if condition is valid, C.HTTP_NOT_MODIFIED is returned |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1029 """ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1030 etag_match = request.getHeader("If-None-Match") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1031 if etag_match is not None: |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1032 if cache.hash == etag_match: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1033 self.page_error(request, C.HTTP_NOT_MODIFIED, no_body=True) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1034 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1035 modified_match = request.getHeader("If-Modified-Since") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1036 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
|
1037 modified = date_utils.date_parse(modified_match) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1038 if modified >= int(cache.created): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1039 self.page_error(request, C.HTTP_NOT_MODIFIED, no_body=True) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1040 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1041 def check_cache_subscribe_cb(self, sub_id, service, node): |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1042 self.cache_pubsub_sub.add((service, node, sub_id)) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1043 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1044 def check_cache_subscribe_eb(self, failure_, service, node): |
1216 | 1045 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
|
1046 # FIXME: cache must be marked as unusable here |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1047 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1048 def ps_node_watch_add_eb(self, failure_, service, node): |
1216 | 1049 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
|
1050 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1051 def use_cache(self, request: server.Request) -> bool: |
1490
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1052 """Indicate if the cache should be used |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1053 |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1054 test request header to see if it is requested to skip the cache |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1055 @return: True if cache should be used |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1056 """ |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1057 return request.getHeader('cache-control') != 'no-cache' |
774a81a6e8b5
pages: new `useCache` method to check if `no-cache` is used in request
Goffi <goffi@goffi.org>
parents:
1479
diff
changeset
|
1058 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1059 def check_cache(self, request, cache_type, **kwargs): |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1060 """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
|
1061 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1062 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
|
1063 pubsub node) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1064 @param request(server.Request): current HTTP request |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1065 @param cache_type(int): on of C.CACHE_* const. |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1066 @param **kwargs: args according to cache_type: |
1010
4de970de87d7
pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents:
1009
diff
changeset
|
1067 C.CACHE_PUBSUB: |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1068 service: pubsub service |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1069 node: pubsub node |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1070 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
|
1071 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1072 """ |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1073 if request.postpath: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1074 # 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
|
1075 return |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1076 |
1375
e943b0c8eec5
pages: cache is skipped if there are query arguments in URL
Goffi <goffi@goffi.org>
parents:
1374
diff
changeset
|
1077 if request.uri != request.path: |
e943b0c8eec5
pages: cache is skipped if there are query arguments in URL
Goffi <goffi@goffi.org>
parents:
1374
diff
changeset
|
1078 # we don't cache page with query arguments as there can be a lot of variants |
e943b0c8eec5
pages: cache is skipped if there are query arguments in URL
Goffi <goffi@goffi.org>
parents:
1374
diff
changeset
|
1079 # influencing page results (e.g. search terms) |
e943b0c8eec5
pages: cache is skipped if there are query arguments in URL
Goffi <goffi@goffi.org>
parents:
1374
diff
changeset
|
1080 log.debug("ignoring cache due to query arguments") |
e943b0c8eec5
pages: cache is skipped if there are query arguments in URL
Goffi <goffi@goffi.org>
parents:
1374
diff
changeset
|
1081 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1082 no_cache = not self.use_cache(request) |
1255
b1fb57e9176d
pages: don't use cache when `cache-control` header is set to `no-cache`
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
1083 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1084 profile = self.get_profile(request) or C.SERVICE_PROFILE |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1085 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1086 if cache_type == C.CACHE_PUBSUB: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1087 service, node = kwargs["service"], kwargs["node"] |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1088 if not node: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1089 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1090 short = kwargs["short"] |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1091 node = self.host.ns_map[short] |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1092 except KeyError: |
1216 | 1093 log.warning(_('Can\'t use cache for empty node without namespace ' |
1094 'set, please ensure to set "short" and that it is ' | |
1095 'registered')) | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1096 return |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1097 if profile != C.SERVICE_PROFILE: |
1374
25104d31479f
pages: `on_data_post` can now return `"continue"` string:
Goffi <goffi@goffi.org>
parents:
1338
diff
changeset
|
1098 # only service profile is cached for now |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1099 return |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1100 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
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
|
1101 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
|
1102 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
|
1103 # 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
|
1104 locale = None |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1105 try: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1106 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
|
1107 [self.vhost_root][request.uri][locale][self]) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1108 except KeyError: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1109 # no cache yet, let's subscribe to the pubsub node |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1110 d1 = self.host.bridge_call( |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1111 "ps_subscribe", service.full(), node, "", profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1112 ) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1113 d1.addCallback(self.check_cache_subscribe_cb, service, node) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1114 d1.addErrback(self.check_cache_subscribe_eb, service, node) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1115 d2 = self.host.bridge_call("ps_node_watch_add", service.full(), node, profile) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1116 d2.addErrback(self.ps_node_watch_add_eb, service, node) |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1117 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
|
1118 self.vhost_root, request.uri, locale] |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1119 # 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
|
1120 # the subscription to continue with page rendering |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1121 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
|
1122 else: |
b5f920845d34
pages: delete existing cache instead of just ignoring it when `no-cache` is used
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
1123 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
|
1124 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
|
1125 [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
|
1126 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
|
1127 return |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1128 |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1129 else: |
1216 | 1130 raise exceptions.InternalError("Unknown cache_type") |
1131 log.debug("using cache for {page}".format(page=self)) | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1132 cache.last_access = time.time() |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1133 self._set_cache_headers(request, cache) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1134 self._check_cache_headers(request, cache) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1135 request.write(cache.rendered) |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1136 request.finish() |
1216 | 1137 raise failure.Failure(exceptions.CancelError("cache is used")) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1138 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1139 def _cache_url(self, request, profile): |
1018 | 1140 self.cached_urls.setdefault(profile, {})[request.uri] = CacheURL(request) |
1141 | |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1142 @classmethod |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1143 def on_node_event(cls, host, service, node, event_type, items, profile): |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1144 """Invalidate cache for all pages linked to this node""" |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1145 try: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1146 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
|
1147 except KeyError: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1148 log.info(_( |
1216 | 1149 "Removing subscription for {service}/{node}: " |
1150 "the page is not cached").format(service=service, node=node)) | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1151 d1 = host.bridge_call("ps_unsubscribe", service, node, profile) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1152 d1.addErrback( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1153 lambda failure_: log.warning( |
1216 | 1154 _("Can't unsubscribe from {service}/{node}: {msg}").format( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1155 service=service, node=node, msg=failure_))) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1156 d2 = host.bridge_call("ps_node_watch_add", service, node, profile) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1157 # 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
|
1158 d2.addErrback( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1159 lambda failure_: log.warning( |
1216 | 1160 _("Can't remove watch for {service}/{node}: {msg}").format( |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1161 service=service, node=node, msg=failure_))) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1162 else: |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1163 cache.clear() |
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1164 |
1322
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1165 # identities |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1166 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1167 async def fill_missing_identities( |
1322
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1168 self, |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1169 request: server.Request, |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1170 entities: List[Union[str, jid.JID, None]], |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1171 ) -> None: |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1172 """Check if all entities have an identity cache, get missing ones from backend |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1173 |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1174 @param request: request with a plugged profile |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1175 @param entities: entities to check, None or empty strings will be filtered |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1176 """ |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1177 entities = {str(e) for e in entities if e} |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1178 profile = self.get_profile(request) or C.SERVICE_PROFILE |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1179 identities = self.host.get_session_data( |
1322
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1180 request, |
1506 | 1181 session_iface.IWebSession |
1322
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1182 ).identities |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1183 for e in entities: |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1184 if e not in identities: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1185 id_raw = await self.host.bridge_call( |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1186 'identity_get', e, [], True, profile) |
1322
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1187 identities[e] = data_format.deserialise(id_raw) |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1188 |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1189 # signals, server => browser communication |
a0954b6610aa
pages: identities are not using `data_objects` anymore:
Goffi <goffi@goffi.org>
parents:
1319
diff
changeset
|
1190 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1191 def delegate_to_resource(self, request, resource): |
1062
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1192 """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
|
1193 buf = resource.render(request) |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1194 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
|
1195 pass |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1196 else: |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1197 request.write(buf) |
c80649cdadd5
pages: new delegateToResource method to continue workflow with a Twisted Resource
Goffi <goffi@goffi.org>
parents:
1061
diff
changeset
|
1198 request.finish() |
1216 | 1199 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
|
1200 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1201 def http_redirect(self, request, url): |
962 | 1202 """redirect to an URL using HTTP redirection |
1203 | |
1204 @param request(server.Request): current HTTP request | |
1205 @param url(unicode): url to redirect to | |
1206 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1207 web_util.redirectTo(url.encode("utf-8"), request) |
962 | 1208 request.finish() |
1216 | 1209 raise failure.Failure(exceptions.CancelError("HTTP redirection is used")) |
962 | 1210 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1211 def redirect_or_continue(self, request, redirect_arg="redirect_url"): |
1392
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1212 """Helper method to redirect a page to an url given as arg |
962 | 1213 |
1214 if the arg is not present, the page will continue normal workflow | |
1215 @param request(server.Request): current HTTP request | |
1216 @param redirect_arg(unicode): argument to use to get redirection URL | |
1217 @interrupt: redirect the page to requested URL | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1218 @interrupt page_error(C.HTTP_BAD_REQUEST): empty or non local URL is used |
962 | 1219 """ |
1216 | 1220 redirect_arg = redirect_arg.encode('utf-8') |
962 | 1221 try: |
1216 | 1222 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
|
1223 except (KeyError, IndexError): |
962 | 1224 pass |
1225 else: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1226 # a redirection is requested |
1216 | 1227 if not url or url[0] != "/": |
962 | 1228 # we only want local urls |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1229 self.page_error(request, C.HTTP_BAD_REQUEST) |
962 | 1230 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1231 self.http_redirect(request, url) |
962 | 1232 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1233 def page_redirect(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
|
1234 """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
|
1235 |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1236 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
|
1237 skipping named page's parse_url method if it exist. |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1238 If you want to do a HTTP redirection, use http_redirect |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1239 @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
|
1240 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
|
1241 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
|
1242 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1243 - 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
|
1244 - 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
|
1245 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
|
1246 "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
|
1247 "/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
|
1248 @param request(server.Request): current HTTP request |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1249 @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
|
1250 skipped |
1032
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1251 @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
|
1252 @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
|
1253 """ |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1254 # FIXME: render non LiberviaPage resources |
1216 | 1255 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
|
1256 if not path[0]: |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1257 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
|
1258 else: |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1259 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
|
1260 |
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1261 for subpage in path[1:]: |
1216 | 1262 subpage = subpage.encode('utf-8') |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1263 if redirect_page is self.vhost_root: |
962 | 1264 redirect_page = redirect_page.children[subpage] |
1265 else: | |
1266 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
|
1267 |
1032
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1268 if path_args is not None: |
1224
62bf4f87c249
server: some encoding fixes following python 3 port
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
1269 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
|
1270 request.postpath = args + request.postpath |
863cc6f97068
pages: path arguments can now be specified in pageRedirect
Goffi <goffi@goffi.org>
parents:
1031
diff
changeset
|
1271 |
1033
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1272 if self._do_cache: |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1273 # 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
|
1274 redirect_page._do_cache = self._do_cache |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1275 self._do_cache = None |
c34f08e05cdf
pages: cache is now working even after pageRedirect
Goffi <goffi@goffi.org>
parents:
1032
diff
changeset
|
1276 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1277 defer.ensureDeferred( |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1278 redirect_page.render_page(request, skip_parse_url=skip_parse_url) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1279 ) |
1216 | 1280 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
|
1281 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1282 def page_error(self, request, code=C.HTTP_NOT_FOUND, no_body=False): |
917 | 1283 """generate an error page and terminate the request |
1284 | |
1285 @param request(server.Request): HTTP request | |
1286 @param core(int): error code to use | |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1287 @param no_body: don't write body if True |
917 | 1288 """ |
1198
1211dbc3cca7
pages: don't put error pages in cache
Goffi <goffi@goffi.org>
parents:
1193
diff
changeset
|
1289 if self._do_cache is not None: |
1211dbc3cca7
pages: don't put error pages in cache
Goffi <goffi@goffi.org>
parents:
1193
diff
changeset
|
1290 # 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
|
1291 self._do_cache = None |
917 | 1292 request.setResponseCode(code) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1293 if no_body: |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1294 request.finish() |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1295 else: |
1216 | 1296 template = "error/" + str(code) + ".html" |
1165 | 1297 template_data = request.template_data |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1298 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
1165 | 1299 if session_data.locale is not None: |
1216 | 1300 template_data['locale'] = session_data.locale |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1301 if self.vhost_root.site_name: |
1216 | 1302 template_data['site'] = self.vhost_root.site_name |
917 | 1303 |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1304 rendered = self.host.renderer.render( |
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1305 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
|
1306 theme=session_data.theme or self.default_theme, |
1318
38875bc292ff
pages: add `media_path` and `build_path` in template data for error pages.
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
1307 media_path=f"/{C.MEDIA_DIR}", |
38875bc292ff
pages: add `media_path` and `build_path` in template data for error pages.
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
1308 build_path=f"/{C.BUILD_DIR}/", |
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
|
1309 site_themes=self.site_themes, |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1310 error_code=code, |
1165 | 1311 **template_data |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1312 ) |
917 | 1313 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1314 self.write_data(rendered, request) |
1216 | 1315 raise failure.Failure(exceptions.CancelError("error page is used")) |
917 | 1316 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1317 def write_data(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
|
1318 """write data to transport and finish the request""" |
917 | 1319 if data is None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1320 self.page_error(request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1321 data_encoded = data.encode("utf-8") |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1322 |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1323 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
|
1324 redirected_page = self._do_cache.pop(0) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1325 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
|
1326 page_cache = cache[redirected_page] = CachePage(data_encoded) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1327 self._set_cache_headers(request, page_cache) |
1216 | 1328 log.debug(_("{page} put in cache for [{profile}]") |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1329 .format( page=self, profile=self._do_cache[0])) |
985
64826e69f365
pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
1330 self._do_cache = None |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1331 self._check_cache_headers(request, page_cache) |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1332 |
1208
584e29d9510a
pages: display a warning instead or raising exception if write fails:
Goffi <goffi@goffi.org>
parents:
1205
diff
changeset
|
1333 try: |
584e29d9510a
pages: display a warning instead or raising exception if write fails:
Goffi <goffi@goffi.org>
parents:
1205
diff
changeset
|
1334 request.write(data_encoded) |
584e29d9510a
pages: display a warning instead or raising exception if write fails:
Goffi <goffi@goffi.org>
parents:
1205
diff
changeset
|
1335 except AttributeError: |
1216 | 1336 log.warning(_("Can't write page, the request has probably been cancelled " |
1337 "(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
|
1338 return |
1019
34240d08f682
pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents:
1018
diff
changeset
|
1339 request.finish() |
917 | 1340 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1341 def _subpages_handler(self, request): |
917 | 1342 """render subpage if suitable |
1343 | |
1344 this method checks if there is still an unmanaged part of the path | |
1345 and check if it corresponds to a subpage. If so, it render the subpage | |
1346 else it render a NoResource. | |
1347 If there is no unmanaged part of the segment, current page workflow is pursued | |
1348 """ | |
1349 if request.postpath: | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1350 subpage = self.next_path(request).encode('utf-8') |
917 | 1351 try: |
1352 child = self.children[subpage] | |
1353 except KeyError: | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1354 self.page_error(request) |
917 | 1355 else: |
1356 child.render(request) | |
1216 | 1357 raise failure.Failure(exceptions.CancelError("subpage page is used")) |
917 | 1358 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1359 def _prepare_dynamic(self, request): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1360 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
998
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1361 # 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
|
1362 # we set data for template, and create/register token |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
1363 # socket_token = str(uuid.uuid4()) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
1364 socket_url = self.host.get_websocket_url(request) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
1365 # as for CSRF, it is important to not let the socket token if we use the service |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
1366 # profile, as those pages can be cached, and then the token leaked. |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1494
diff
changeset
|
1367 socket_token = '' if session_data.profile is None else session_data.ws_token |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1368 socket_debug = C.bool_const(self.host.debug) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1369 request.template_data["websocket"] = WebsocketMeta( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1370 socket_url, socket_token, socket_debug |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1371 ) |
998
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1372 # 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
|
1373 request._signals_registered = [] |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1374 # 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
|
1375 request._signals_cache = [] |
0848b8b0188d
pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents:
995
diff
changeset
|
1376 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1377 def _render_template(self, request): |
962 | 1378 template_data = request.template_data |
1379 | |
1380 # if confirm variable is set in case of successfuly data post | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1381 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
1266
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1258
diff
changeset
|
1382 template_data['identities'] = session_data.identities |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1383 if session_data.pop_page_flag(self, C.FLAG_CONFIRM): |
1216 | 1384 template_data["confirm"] = True |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1385 notifs = session_data.pop_page_notifications(self) |
1186
352865f4a268
server: added a generic way to have notification messages in pages
Goffi <goffi@goffi.org>
parents:
1185
diff
changeset
|
1386 if notifs: |
1216 | 1387 template_data["notifications"] = notifs |
1266
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1258
diff
changeset
|
1388 if session_data.jid is not None: |
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1258
diff
changeset
|
1389 template_data["own_jid"] = session_data.jid |
1165 | 1390 if session_data.locale is not None: |
1216 | 1391 template_data['locale'] = session_data.locale |
1294
44da7b118b02
pages: set `guest_session` in template data when suitable
Goffi <goffi@goffi.org>
parents:
1293
diff
changeset
|
1392 if session_data.guest: |
44da7b118b02
pages: set `guest_session` in template data when suitable
Goffi <goffi@goffi.org>
parents:
1293
diff
changeset
|
1393 template_data['guest_session'] = True |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1394 if self.vhost_root.site_name: |
1216 | 1395 template_data['site'] = self.vhost_root.site_name |
1246 | 1396 if self.dyn_data: |
1397 for data in self.dyn_data.values(): | |
1398 try: | |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1255
diff
changeset
|
1399 scripts = data['scripts'] |
1246 | 1400 except KeyError: |
1401 pass | |
1402 else: | |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1277
diff
changeset
|
1403 template_data.setdefault('scripts', utils.OrderedSet()).update(scripts) |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1255
diff
changeset
|
1404 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
|
1405 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
|
1406 common_scripts = data_common['scripts'] |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1255
diff
changeset
|
1407 if common_scripts: |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1277
diff
changeset
|
1408 template_data.setdefault('scripts', utils.OrderedSet()).update(common_scripts) |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1267
diff
changeset
|
1409 if "template" in data_common: |
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1267
diff
changeset
|
1410 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
|
1411 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
|
1412 template_data[key] = value |
962 | 1413 |
1293
de3b15d68bb6
pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents:
1286
diff
changeset
|
1414 theme = session_data.theme or self.default_theme |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1415 self.expose_to_scripts( |
1293
de3b15d68bb6
pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents:
1286
diff
changeset
|
1416 request, |
1324
898442c4ff68
pages: expose `cache_path` to scripts + `session_uuid` to templates
Goffi <goffi@goffi.org>
parents:
1322
diff
changeset
|
1417 cache_path=session_data.cache_dir, |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1418 templates_root_url=str(self.vhost_root.get_front_url(theme)), |
1580
08342aca8c1e
pages: make `tz_data` always available as template data and exposed to scripts
Goffi <goffi@goffi.org>
parents:
1528
diff
changeset
|
1419 profile=session_data.profile, |
08342aca8c1e
pages: make `tz_data` always available as template data and exposed to scripts
Goffi <goffi@goffi.org>
parents:
1528
diff
changeset
|
1420 tz_name=template_data["tz_name"] |
08342aca8c1e
pages: make `tz_data` always available as template data and exposed to scripts
Goffi <goffi@goffi.org>
parents:
1528
diff
changeset
|
1421 ) |
1595
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1592
diff
changeset
|
1422 if session_data.jid is not None: |
1615
97ea776df74c
server (pages): fix `own_jid` exposition.
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
1423 self.expose_to_scripts(request, own_jid=session_data.jid.full()) |
1293
de3b15d68bb6
pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents:
1286
diff
changeset
|
1424 |
1417 | 1425 uri = request.uri.decode() |
1426 try: | |
1427 template_data["current_page"] = next( | |
1618 | 1428 m["name"] for m in self.main_menu if uri.startswith(m["url"]) |
1417 | 1429 ) |
1430 except StopIteration: | |
1431 pass | |
1432 | |
917 | 1433 return self.host.renderer.render( |
1434 self.template, | |
1293
de3b15d68bb6
pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents:
1286
diff
changeset
|
1435 theme=theme, |
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
|
1436 site_themes=self.site_themes, |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1437 page_url=self.get_url(), |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1255
diff
changeset
|
1438 media_path=f"/{C.MEDIA_DIR}", |
1318
38875bc292ff
pages: add `media_path` and `build_path` in template data for error pages.
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
1439 build_path=f"/{C.BUILD_DIR}/", |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1440 cache_path=session_data.cache_dir, |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1441 main_menu=self.main_menu, |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1442 **template_data) |
917 | 1443 |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1444 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
|
1445 """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
|
1446 |
968
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1447 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
|
1448 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
|
1449 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
|
1450 a list of unicode arguments to append to the url. |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1451 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
|
1452 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
|
1453 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
|
1454 this case. |
962 | 1455 @param ret(None, unicode, iterable): on_data_post return value |
1456 see LiberviaPage.__init__ on_data_post docstring | |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1457 """ |
962 | 1458 if ret is None: |
1459 ret = () | |
1216 | 1460 elif isinstance(ret, str): |
962 | 1461 ret = (ret,) |
1462 else: | |
1463 ret = tuple(ret) | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1464 raise NotImplementedError( |
1216 | 1465 _("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
|
1466 ) |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1467 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1468 request_data = self.get_r_data(request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1469 if "post_redirect_page" in request_data: |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1470 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
|
1471 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
|
1472 redirect_page = redirect_page_data[0] |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
1473 redirect_page_args = redirect_page_data[1:] |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1474 redirect_uri = redirect_page.get_url(*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
|
1475 else: |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1476 redirect_page = redirect_page_data |
972
c4e58c4dba75
server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents:
968
diff
changeset
|
1477 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
|
1478 else: |
4d37b23777c3
pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents:
966
diff
changeset
|
1479 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
|
1480 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
|
1481 |
962 | 1482 if not C.POST_NO_CONFIRM in ret: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1483 session_data.set_page_flag(redirect_page, C.FLAG_CONFIRM) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1484 request.setResponseCode(C.HTTP_SEE_OTHER) |
1216 | 1485 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
|
1486 request.finish() |
1216 | 1487 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
|
1488 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1489 async def _on_data_post(self, request): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1490 self.check_csrf(request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1491 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1492 ret = await as_deferred(self.on_data_post, self, request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1493 except exceptions.DataError as e: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1494 # 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
|
1495 # warning notification |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1496 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1497 session_data.set_page_notification(self, str(e), C.LVL_WARNING) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1498 request.setResponseCode(C.HTTP_SEE_OTHER) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1499 request.setHeader("location", request.uri) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1500 request.finish() |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1501 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
|
1502 else: |
1374
25104d31479f
pages: `on_data_post` can now return `"continue"` string:
Goffi <goffi@goffi.org>
parents:
1338
diff
changeset
|
1503 if ret != "continue": |
25104d31479f
pages: `on_data_post` can now return `"continue"` string:
Goffi <goffi@goffi.org>
parents:
1338
diff
changeset
|
1504 self._on_data_post_redirect(ret, request) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1505 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1506 def get_posted_data( |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1507 self, |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1508 request: server.Request, |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1509 keys, |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1510 multiple: bool = False, |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1511 raise_on_missing: bool = True, |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1512 strip: bool = True |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1513 ): |
1187
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1514 """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
|
1515 |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1516 @param request: request linked to the session |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1517 @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
|
1518 unicode to get one value |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1519 iterable to get more than one |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1520 @param multiple: True if multiple values are possible/expected |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1521 if False, the first value is returned |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1522 @param raise_on_missing: raise KeyError on missing key if True |
1187
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1523 else use None for missing values |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1524 @param strip: if True, apply "strip()" on values |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1525 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): |
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1526 values received for this(these) key(s) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1527 @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
|
1528 """ |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1127
diff
changeset
|
1529 # 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
|
1530 # double unquote |
1216 | 1531 if isinstance(keys, str): |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1532 keys = [keys] |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1533 |
1216 | 1534 keys = [k.encode('utf-8') for k in keys] |
1535 | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1536 ret = [] |
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1537 for key in keys: |
1216 | 1538 gen = (urllib.parse.unquote(v.decode("utf-8")) |
1539 for v in request.args.get(key, [])) | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1540 if multiple: |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1541 ret.append(gen.strip() if strip else gen) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1542 else: |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1543 try: |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1544 v = next(gen) |
956
dabecab10faa
server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents:
955
diff
changeset
|
1545 except StopIteration: |
1187
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1546 if raise_on_missing: |
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1547 raise KeyError(key) |
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1548 else: |
dab7a2b151ea
server (pages): added raise_on_missing arguments in getPostedData:
Goffi <goffi@goffi.org>
parents:
1186
diff
changeset
|
1549 ret.append(None) |
1393
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1550 else: |
d9a328ddef9c
pages: implements `strip` argument in `getPostedData`
Goffi <goffi@goffi.org>
parents:
1392
diff
changeset
|
1551 ret.append(v.strip() if strip else v) |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1552 |
1392
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1553 if len(keys) == 1: |
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1554 return ret[0] |
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1555 else: |
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1556 return ret |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1557 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1558 def get_all_posted_data(self, request, except_=(), multiple=True): |
959 | 1559 """get all posted data |
1560 | |
1561 @param request(server.Request): request linked to the session | |
1562 @param except_(iterable[unicode]): key of values to ignore | |
1563 csrf_token will always be ignored | |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1564 @param multiple(bool): if False, only the first values are returned |
959 | 1565 @return (dict[unicode, list[unicode]]): post values |
1566 """ | |
1216 | 1567 except_ = tuple(except_) + ("csrf_token",) |
959 | 1568 ret = {} |
1216 | 1569 for key, values in request.args.items(): |
1570 key = key.decode('utf-8') | |
1571 key = urllib.parse.unquote(key) | |
959 | 1572 if key in except_: |
1573 continue | |
1216 | 1574 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
|
1575 if not multiple: |
1216 | 1576 ret[key] = urllib.parse.unquote(values[0]) |
1009
b57f86bc1177
pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents:
1003
diff
changeset
|
1577 else: |
1216 | 1578 ret[key] = [urllib.parse.unquote(v) for v in values] |
959 | 1579 return ret |
1580 | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1581 def get_profile(self, request): |
1392
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1582 """Helper method to easily get current profile |
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
|
1583 |
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
|
1584 @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
|
1585 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
|
1586 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1587 web_session = self.host.get_session_data(request, session_iface.IWebSession) |
1506 | 1588 return web_session.profile |
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
|
1589 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1590 def get_jid(self, request): |
1392
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1591 """Helper method to easily get current jid |
1381
a53e22400dad
pages: helper method to get current profile's jid
Goffi <goffi@goffi.org>
parents:
1380
diff
changeset
|
1592 |
a53e22400dad
pages: helper method to get current profile's jid
Goffi <goffi@goffi.org>
parents:
1380
diff
changeset
|
1593 @return: current jid |
a53e22400dad
pages: helper method to get current profile's jid
Goffi <goffi@goffi.org>
parents:
1380
diff
changeset
|
1594 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1595 web_session = self.host.get_session_data(request, session_iface.IWebSession) |
1506 | 1596 return web_session.jid |
1381
a53e22400dad
pages: helper method to get current profile's jid
Goffi <goffi@goffi.org>
parents:
1380
diff
changeset
|
1597 |
a53e22400dad
pages: helper method to get current profile's jid
Goffi <goffi@goffi.org>
parents:
1380
diff
changeset
|
1598 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1599 def get_r_data(self, request): |
1392
e11a71a08a48
pages: fix `getPostedData` when a single value is requested and a list is used
Goffi <goffi@goffi.org>
parents:
1390
diff
changeset
|
1600 """Helper method to get request data dict |
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
|
1601 |
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
|
1602 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
|
1603 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
|
1604 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
|
1605 @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
|
1606 """ |
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
|
1607 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
|
1608 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
|
1609 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
|
1610 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
|
1611 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
|
1612 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1613 def get_page_data(self, request, key): |
1390
3e482795630c
pages: implement `getPageData` and `setPageData` helper methods
Goffi <goffi@goffi.org>
parents:
1382
diff
changeset
|
1614 """Helper method to retrieve reload resistant data""" |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1615 web_session = self.host.get_session_data(request, session_iface.IWebSession) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1616 return web_session.get_page_data(self, key) |
1390
3e482795630c
pages: implement `getPageData` and `setPageData` helper methods
Goffi <goffi@goffi.org>
parents:
1382
diff
changeset
|
1617 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1618 def set_page_data(self, request, key, value): |
1390
3e482795630c
pages: implement `getPageData` and `setPageData` helper methods
Goffi <goffi@goffi.org>
parents:
1382
diff
changeset
|
1619 """Helper method to set reload resistant data""" |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1620 web_session = self.host.get_session_data(request, session_iface.IWebSession) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1621 return web_session.set_page_data(self, key, value) |
1390
3e482795630c
pages: implement `getPageData` and `setPageData` helper methods
Goffi <goffi@goffi.org>
parents:
1382
diff
changeset
|
1622 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1623 def handle_search(self, request, extra): |
1376
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1624 """Manage Full-Text Search |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1625 |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1626 Check if "search" query argument is present, and add MAM filter for it if |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1627 necessary. |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1628 If used, the "search" variable will also be available in template data, thus |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1629 frontend can display some information about it. |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1630 """ |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1631 search = self.get_posted_data(request, 'search', raise_on_missing=False) |
1376
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1632 if search is not None: |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1633 search = search.strip() |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1634 if search: |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1635 try: |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1636 extra[f'mam_filter_{self.host.ns_map["fulltextmam"]}'] = search |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1637 except KeyError: |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1638 log.warning(_("Full-text search is not available")) |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1639 else: |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1640 request.template_data['search'] = search |
1b94a5ab155f
pages (blog/view): Full-Text Search is used when `search` query argument is present
Goffi <goffi@goffi.org>
parents:
1375
diff
changeset
|
1641 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1642 def _check_access(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
|
1643 """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
|
1644 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1645 if access is not granted, show a HTTP_FORBIDDEN page_error 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
|
1646 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
|
1647 """ |
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
|
1648 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
|
1649 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
|
1650 elif self.access == C.PAGES_ACCESS_PROFILE: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1651 profile = self.get_profile(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
|
1652 if not profile: |
1405
3dff555fe691
pages: do no send HTTP_FORBIDDEN when registration is not allowed and no profile is connected:
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
1653 # registration allowed, we redirect to login page |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1654 login_url = self.get_page_redirect_url(request) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1655 self.http_redirect(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
|
1656 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1657 def set_best_locale(self, request): |
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
|
1658 """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
|
1659 |
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
|
1660 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
|
1661 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
|
1662 """ |
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 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
|
1664 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
|
1665 return |
1455
e8c480b0078e
server: don't use unordered set for accepted languages:
Goffi <goffi@goffi.org>
parents:
1433
diff
changeset
|
1666 accepted = [a.strip() for a in accept_language.split(',')] |
1216 | 1667 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
|
1668 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
|
1669 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
|
1670 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
|
1671 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
|
1672 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
|
1673 if a.lower().startswith(lang): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1674 session_data = self.host.get_session_data(request, |
1506 | 1675 session_iface.IWebSession) |
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
|
1676 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
|
1677 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
|
1678 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1679 async def render_page(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
|
1680 """Main method to handle the workflow of a LiberviaPage""" |
923
edb322c87ea4
server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
1681 # template_data are the variables passed to template |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1682 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
|
1683 # 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
|
1684 # so we fill essential data |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1685 session_data = self.host.get_session_data(request, session_iface.IWebSession) |
1479
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1686 profile = session_data.profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1687 request.template_data = { |
1479
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1688 "profile": profile, |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1689 # it's important to not add CSRF token and session uuid if service profile |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1690 # is used because the page may be cached, and the token then leaked |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1691 "csrf_token": "" if profile is None else session_data.csrf_token, |
095e94ca6728
pages: disable CSRF token check when service profile is used:
Goffi <goffi@goffi.org>
parents:
1476
diff
changeset
|
1692 "session_uuid": "public" if profile is None else session_data.uuid, |
1580
08342aca8c1e
pages: make `tz_data` always available as template data and exposed to scripts
Goffi <goffi@goffi.org>
parents:
1528
diff
changeset
|
1693 "breadcrumbs": [], |
1618 | 1694 "tz_name": time.tzname[0], |
1695 "login_url": self.login_url | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1696 } |
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
|
1697 |
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
|
1698 # 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
|
1699 # at the beginning of the request hanling |
917 | 1700 if request.postpath and not request.postpath[-1]: |
1701 # we don't differenciate URLs finishing with '/' or not | |
1702 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
|
1703 |
1168
ea0caa7b1bcc
pages (i18n): check presence of u"/" in locale:
Goffi <goffi@goffi.org>
parents:
1167
diff
changeset
|
1704 # i18n |
1224
62bf4f87c249
server: some encoding fixes following python 3 port
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
1705 key_lang = C.KEY_LANG.encode() |
62bf4f87c249
server: some encoding fixes following python 3 port
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
1706 if key_lang in request.args: |
1165 | 1707 try: |
1224
62bf4f87c249
server: some encoding fixes following python 3 port
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
1708 locale = request.args.pop(key_lang)[0].decode() |
1165 | 1709 except IndexError: |
1216 | 1710 log.warning("empty lang received") |
1168
ea0caa7b1bcc
pages (i18n): check presence of u"/" in locale:
Goffi <goffi@goffi.org>
parents:
1167
diff
changeset
|
1711 else: |
1216 | 1712 if "/" in locale: |
1168
ea0caa7b1bcc
pages (i18n): check presence of u"/" in locale:
Goffi <goffi@goffi.org>
parents:
1167
diff
changeset
|
1713 # "/" 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
|
1714 # path, if localised documents are available for instance |
1216 | 1715 log.warning(_('illegal char found in locale ("/"), hack ' |
1716 'attempt? locale={locale}').format(locale=locale)) | |
1168
ea0caa7b1bcc
pages (i18n): check presence of u"/" in locale:
Goffi <goffi@goffi.org>
parents:
1167
diff
changeset
|
1717 locale = None |
ea0caa7b1bcc
pages (i18n): check presence of u"/" in locale:
Goffi <goffi@goffi.org>
parents:
1167
diff
changeset
|
1718 session_data.locale = locale |
1165 | 1719 |
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
|
1720 # 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
|
1721 if session_data.locale is None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1722 self.set_best_locale(request) |
1165 | 1723 |
1270
6dfcdbeb0d33
pages: handle themes using `theme` request argument
Goffi <goffi@goffi.org>
parents:
1268
diff
changeset
|
1724 # theme |
6dfcdbeb0d33
pages: handle themes using `theme` request argument
Goffi <goffi@goffi.org>
parents:
1268
diff
changeset
|
1725 key_theme = C.KEY_THEME.encode() |
6dfcdbeb0d33
pages: handle themes using `theme` request argument
Goffi <goffi@goffi.org>
parents:
1268
diff
changeset
|
1726 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
|
1727 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
|
1728 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
|
1729 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
|
1730 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
|
1731 "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
|
1732 .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
|
1733 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
|
1734 session_data.theme = theme |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1735 try: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1736 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1737 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1738 self._check_access(request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1739 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1740 if self.redirect is not None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1741 self.page_redirect(self.redirect, request, skip_parse_url=False) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1742 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1743 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
|
1744 if self.url_cache: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1745 profile = self.get_profile(request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1746 try: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1747 cache_url = self.cached_urls[profile][request.uri] |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1748 except KeyError: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1749 # no cache for this URI yet |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1750 # we do normal URL parsing, and then the cache |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1751 await as_deferred(self.parse_url, self, request) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1752 self._cache_url(request, profile) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1753 else: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1754 log.debug(f"using URI cache for {self}") |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1755 cache_url.use(request) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1756 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1757 await as_deferred(self.parse_url, self, request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1758 |
1417 | 1759 if self.add_breadcrumb is None: |
1760 label = ( | |
1761 self.label | |
1762 or self.name | |
1763 or self.url[self.url.rfind('/')+1:] | |
1764 ) | |
1765 breadcrumb = { | |
1766 "url": self.url, | |
1767 "label": label.title(), | |
1768 } | |
1769 request.template_data["breadcrumbs"].append(breadcrumb) | |
1770 else: | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1771 await as_deferred( |
1417 | 1772 self.add_breadcrumb, |
1773 self, | |
1774 request, | |
1775 request.template_data["breadcrumbs"] | |
1776 ) | |
1777 | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1778 self._subpages_handler(request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1779 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1780 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
|
1781 # only HTTP GET and POST are handled so far |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1782 self.page_error(request, C.HTTP_BAD_REQUEST) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1783 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1784 if request.method == C.HTTP_METHOD_POST: |
1284
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
1785 if self.on_data_post == 'continue': |
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
1786 pass |
65c43eec15ad
pages: `on_data_post` can be set to the string `continue` instead of a callable:
Goffi <goffi@goffi.org>
parents:
1283
diff
changeset
|
1787 elif self.on_data_post is None: |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1788 # 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
|
1789 # so we return an error |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1790 self.page_error(request, C.HTTP_BAD_REQUEST) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1791 else: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1792 await self._on_data_post(request) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1793 # 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
|
1794 # 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
|
1795 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1796 if self.dynamic: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1797 self._prepare_dynamic(request) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1798 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1799 if self.prepare_render: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1800 await as_deferred(self.prepare_render, self, request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1801 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1802 if self.template: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1803 rendered = self._render_template(request) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1804 elif self.render_method: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1805 rendered = await as_deferred(self.render_method, self, request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1806 else: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1807 raise exceptions.InternalError( |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1808 "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
|
1809 "render method" |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1810 ) |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1811 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1812 self.write_data(rendered, request) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1813 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1814 except failure.Failure as f: |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1815 # we have to unpack the Failure to catch the right Exception |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1816 raise f.value |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1817 |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1818 except exceptions.CancelError: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1819 pass |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1820 except BridgeException as e: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1821 if e.condition == 'not-allowed': |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1822 log.warning("not allowed exception catched") |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1823 self.page_error(request, C.HTTP_FORBIDDEN) |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1824 elif e.condition == 'item-not-found' or e.classname == 'NotFound': |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1825 self.page_error(request, C.HTTP_NOT_FOUND) |
1285
37a582b0fe53
pages: 404 is now returne if a remote server is not found
Goffi <goffi@goffi.org>
parents:
1284
diff
changeset
|
1826 elif e.condition == 'remote-server-not-found': |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1827 self.page_error(request, C.HTTP_NOT_FOUND) |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1828 elif e.condition == 'forbidden': |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1829 if self.get_profile(request) is None: |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1830 log.debug("access forbidden, we're redirecting to log-in page") |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1831 self.http_redirect(request, self.get_page_redirect_url(request)) |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1832 else: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1833 self.page_error(request, C.HTTP_FORBIDDEN) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1834 else: |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1835 log.error( |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1836 _("Uncatched bridge exception for HTTP request on {url}: {e}\n" |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1837 "page name: {name}\npath: {path}\nURL: {full_url}\n{tb}") |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1838 .format( |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1839 url=self.url, |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1840 e=e, |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1841 name=self.name or "", |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1842 path=self.root_dir, |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1843 full_url=request.URLPath(), |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1844 tb=traceback.format_exc(), |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1845 ) |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1846 ) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1847 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1848 self.page_error(request, C.HTTP_INTERNAL_ERROR) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1849 except exceptions.CancelError: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1850 pass |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1851 except Exception as e: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1852 log.error( |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1853 _("Uncatched error for HTTP request on {url}: {e}\npage name: " |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1854 "{name}\npath: {path}\nURL: {full_url}\n{tb}") |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1855 .format( |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1856 url=self.url, |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1857 e=e, |
1382
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1858 name=self.name or "", |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1859 path=self.root_dir, |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1860 full_url=request.URLPath(), |
21d30f5d582a
pages: better generic errors handling:
Goffi <goffi@goffi.org>
parents:
1381
diff
changeset
|
1861 tb=traceback.format_exc(), |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1862 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1110
diff
changeset
|
1863 ) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1864 try: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1865 self.page_error(request, C.HTTP_INTERNAL_ERROR) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1866 except exceptions.CancelError: |
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1867 pass |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1868 |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1869 def render_GET(self, request): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1870 defer.ensureDeferred(self.render_page(request)) |
917 | 1871 return server.NOT_DONE_YET |
1872 | |
931
8a393ae90f8c
server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents:
927
diff
changeset
|
1873 def render_POST(self, request): |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1506
diff
changeset
|
1874 defer.ensureDeferred(self.render_page(request)) |
1276
cad8f24e23d4
pages: use a coroutine for renderPage:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
1875 return server.NOT_DONE_YET |