annotate src/server/pages.py @ 1018:78af5457d3f8

Pages: added url_cache setting: when url_cache is set, result of parse_url is cached (per profile, because in some pages date retrieved may differ between profiles), and then reused. This is useful if some data are fetched during parse_url (e.g. bridge/XMPP call).
author Goffi <goffi@goffi.org>
date Sun, 21 Jan 2018 13:08:54 +0100
parents 16d52917666c
children 34240d08f682
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1 #!/usr/bin/python
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
2 # -*- coding: utf-8 -*-
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
3
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
4 # Libervia: a Salut à Toi frontend
964
fd4eae654182 misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents: 962
diff changeset
5 # Copyright (C) 2011-2017 Jérôme Poisson <goffi@goffi.org>
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
6
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
10 # (at your option) any later version.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
11
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
12 # This program is distributed in the hope that it will be useful,
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
15 # GNU Affero General Public License for more details.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
16
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
19 from twisted.web import server
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
20 from twisted.web import resource as web_resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
21 from twisted.web import util as web_util
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
22 from twisted.internet import defer
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
23 from twisted.words.protocols.jabber import jid
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
24 from twisted.python import failure
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
25
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
26 from sat.core.i18n import _
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
27 from sat.core import exceptions
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
28 from sat.tools.common import uri as common_uri
438
582c435dab6b server side: new log system is used
Goffi <goffi@goffi.org>
parents: 435
diff changeset
29 from sat.core.log import getLogger
582c435dab6b server side: new log system is used
Goffi <goffi@goffi.org>
parents: 435
diff changeset
30 log = getLogger(__name__)
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
31 from libervia.server.constants import Const as C
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
32 from libervia.server import session_iface
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
33 from libervia.server.utils import quote
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
34 import libervia
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
35
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
36 from collections import namedtuple
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
37 import uuid
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
38 import os.path
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
39 import urllib
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
40 import time
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
41
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
42 WebsocketMeta = namedtuple("WebsocketMeta", ('url', 'token', 'debug'))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
43
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
44
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
45
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
46 class CacheBase(object):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
47
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
48 def __init__(self):
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
49 self._created = time.time()
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
50 self._last_access = self._created
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
51
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
52 @property
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
53 def created(self):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
54 return self._created
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
55
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
56 @property
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
57 def last_access(self):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
58 return self._last_access
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
59
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
60 @last_access.setter
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
61 def last_access(self, timestamp):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
62 self._last_access = timestamp
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
63
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
64
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
65 class CachePage(CacheBase):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
66
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
67 def __init__(self, rendered):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
68 super(CachePage, self).__init__()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
69 self._created = time.time()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
70 self._last_access = self._created
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
71 self._rendered = rendered
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
72
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
73 @property
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
74 def rendered(self):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
75 return self._rendered
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
76
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
77
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
78
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
79 class CacheURL(CacheBase):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
80
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
81 def __init__(self, request):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
82 super(CacheURL, self).__init__()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
83 try:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
84 self._data = request.data.copy()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
85 except AttributeError:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
86 self._data = {}
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
87 self._template_data = request.template_data.copy()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
88 self._prepath = request.prepath[:]
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
89 self._postpath = request.postpath[:]
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
90 del self._template_data['csrf_token']
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
91
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
92 def use(self, request):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
93 self.last_access = time.time()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
94 request.data = self._data.copy()
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
95 request.template_data.update(self._template_data)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
96 request.prepath = self._prepath[:]
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
97 request.postpath = self._postpath[:]
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
98
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
99
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
100 class LiberviaPage(web_resource.Resource):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
101 isLeaf = True # we handle subpages ourself
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
102 named_pages = {}
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
103 uri_callbacks = {}
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
104 signals_handlers = {}
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
105 pages_redirects = {}
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
106 cache = {}
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
107 cached_urls = {}
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
108 # Set of tuples (service/node/sub_id) of nodes subscribed for caching
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
109 # 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
110 cache_pubsub_sub = set()
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
111 main_menu = None
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
112
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
113 def __init__(self, host, root_dir, url, name=None, redirect=None, access=None, dynamic=False, parse_url=None,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
114 prepare_render=None, render=None, template=None,
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
115 on_data_post=None, on_data=None, on_signal=None,
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
116 url_cache=False):
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
117 """initiate LiberviaPages
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
118
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
119 LiberviaPages are the main resources of Libervia, using easy to set python files
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
120 The arguments are the variables found in page_meta.py
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
121 @param host(Libervia): the running instance of Libervia
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
122 @param root_dir(unicode): aboslute file path of the page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
123 @param url(unicode): relative URL to the page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
124 this URL may not be valid, as pages may require path arguments
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
125 @param name(unicode, None): if not None, a unique name to identify the page
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
126 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
127 "/" is not allowed in names (as it can be used to construct URL paths)
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
128 @param redirect(unicode, None): if not None, this page will be redirected. A redirected
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
129 parameter is used as in self.pageRedirect. parse_url will not be skipped
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
130 using this redirect parameter is called "full redirection"
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
131 using self.pageRedirect is called "partial redirection" (because some rendering method
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
132 can still be used, e.g. parse_url)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
133 @param access(unicode, None): permission needed to access the page
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
134 None means public access.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
135 Pages inherit from parent pages: e.g. if a "settings" page is restricted to admins,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
136 and if "settings/blog" is public, it still can only be accessed by admins.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
137 see C.PAGES_ACCESS_* for details
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
138 @param dynamic(bool): if True, activate websocket for bidirectional communication
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
139 @param parse_url(callable, None): if set it will be called to handle the URL path
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
140 after this method, the page will be rendered if noting is left in path (request.postpath)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
141 else a the request will be transmitted to a subpage
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
142 @param prepare_render(callable, None): if set, will be used to prepare the rendering
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
143 that often means gathering data using the bridge
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
144 @param render(callable, None): if not template is set, this method will be called and
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
145 what it returns will be rendered.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
146 This method is mutually exclusive with template and must return a unicode string.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
147 @param template(unicode, None): path to the template to render.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
148 This method is mutually exclusive with render
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
149 @param on_data_post(callable, None): method to call when data is posted
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
150 None if not post is handled
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
151 on_data_post can return a string with following value:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
152 - C.POST_NO_CONFIRM: confirm flag will not be set
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
153 @param on_data(callable, None): method to call when dynamic data is sent
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
154 this method is used with Libervia's websocket mechanism
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
155 @param on_signal(callable, None): method to call when a registered signal is received
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
156 this method is used with Libervia's websocket mechanism
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
157 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
158
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
159 web_resource.Resource.__init__(self)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
160 self.host = host
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
161 self.root_dir = root_dir
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
162 self.url = url
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
163 self.name = name
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
164 if name is not None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
165 if name in self.named_pages:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
166 raise exceptions.ConflictError(_(u'a Libervia page named "{}" already exists'.format(name)))
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
167 if u'/' in name:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
168 raise ValueError(_(u'"/" is not allowed in page names'))
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
169 if not name:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
170 raise ValueError(_(u"a page name can't be empty"))
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
171 self.named_pages[name] = self
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
172 if access is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
173 access = C.PAGES_ACCESS_PUBLIC
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
174 if access not in (C.PAGES_ACCESS_PUBLIC, C.PAGES_ACCESS_PROFILE, 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
175 raise NotImplementedError(_(u"{} access is not implemented yet").format(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
176 self.access = access
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
177 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
178 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
179 # 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
180 # so we check that rendering methods/values are not set
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
181 if not all(lambda x: x is not None
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
182 for x in (parse_url, prepare_render, render, template)):
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
183 raise ValueError(_(u"you can't use full page redirection with other rendering method,"
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
184 u"check self.pageRedirect if you need to use them"))
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
185 self.redirect = redirect
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
186 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
187 self.redirect = None
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
188 self.parse_url = parse_url
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
189 self.prepare_render = prepare_render
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
190 self.template = template
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
191 self.render_method = render
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
192 self.on_data_post = on_data_post
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
193 self.on_data = on_data
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
194 self.on_signal = on_signal
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
195 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
196 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
197 # 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
198 return
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
199 if template is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
200 if not callable(render):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
201 log.error(_(u"render must be implemented and callable if template is not set"))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
202 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
203 if render is not None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
204 log.error(_(u"render can't be used at the same time as template"))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
205 if parse_url is not None and not callable(parse_url):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
206 log.error(_(u"parse_url must be a callable"))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
207
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
208 # if not None, next rendering will be cached
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
209 # it must then contain a list of the the keys to use (without the page instance)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
210 # e.g. [C.SERVICE_PROFILE, "pubsub", server@example.tld, pubsub_node] 
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
211 self._do_cache = None
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
212
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
213 def __unicode__(self):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
214 return u'LiberviaPage {name} at {url}'.format(
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
215 name = self.name or u'<anonymous>',
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
216 url = self.url)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
217
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
218 def __str__(self):
1013
841a595bee5a pages: fixed str representation of LiberviaPage
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
219 return self.__unicode__().encode('utf-8')
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
220
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
221 @classmethod
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
222 def importPages(cls, host, parent=None, path=None):
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
223 """Recursively import Libervia pages"""
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
224 if path is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
225 path = []
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
226 if parent is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
227 root_dir = os.path.join(os.path.dirname(libervia.__file__), C.PAGES_DIR)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
228 parent = host
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
229 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
230 root_dir = parent.root_dir
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
231 for d in os.listdir(root_dir):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
232 dir_path = os.path.join(root_dir, d)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
233 if not os.path.isdir(dir_path):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
234 continue
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
235 meta_path = os.path.join(dir_path, C.PAGES_META_FILE)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
236 if os.path.isfile(meta_path):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
237 page_data = {}
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
238 new_path = path + [d]
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
239 # we don't want to force the presence of __init__.py
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
240 # so we use execfile instead of import.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
241 # TODO: when moved to Python 3, __init__.py is not mandatory anymore
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
242 # so we can switch to import
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
243 execfile(meta_path, page_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
244 resource = LiberviaPage(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
245 host,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
246 dir_path,
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
247 u'/' + u'/'.join(new_path),
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
248 name=page_data.get('name'),
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
249 redirect=page_data.get('redirect'),
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
250 access=page_data.get('access'),
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
251 dynamic=page_data.get('dynamic', False),
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
252 parse_url=page_data.get('parse_url'),
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
253 prepare_render=page_data.get('prepare_render'),
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
254 render=page_data.get('render'),
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
255 template=page_data.get('template'),
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
256 on_data_post=page_data.get('on_data_post'),
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
257 on_data=page_data.get('on_data'),
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
258 on_signal=page_data.get('on_signal'),
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
259 url_cache=page_data.get('url_cache', False),
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
260 )
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
261 parent.putChild(d, resource)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
262 log.info(u"Added /{path} page".format(path=u'[...]/'.join(new_path)))
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
263 if 'uri_handlers' in page_data:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
264 if not isinstance(page_data, dict):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
265 log.error(_(u'uri_handlers must be a dict'))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
266 else:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
267 for uri_tuple, cb_name in page_data['uri_handlers'].iteritems():
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
268 if len(uri_tuple) != 2 or not isinstance(cb_name, basestring):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
269 log.error(_(u"invalid uri_tuple"))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
270 continue
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
271 log.info(_(u'setting {}/{} URIs handler').format(*uri_tuple))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
272 try:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
273 cb = page_data[cb_name]
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
274 except KeyError:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
275 log.error(_(u'missing {name} method to handle {1}/{2}').format(
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
276 name = cb_name, *uri_tuple))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
277 continue
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
278 else:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
279 cls.registerURI(uri_tuple, cb, new_path)
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
280
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
281 LiberviaPage.importPages(host, resource, new_path)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
282
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
283 @classmethod
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
284 def setMenu(cls, menus):
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
285 main_menu = []
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
286 for menu in menus:
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
287 if not menu:
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
288 msg = _(u"menu item can't be empty")
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
289 log.error(msg)
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
290 raise ValueError(msg)
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
291 elif isinstance(menu, list):
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
292 if len(menu) != 2:
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
293 msg = _(u"menu item as list must be in the form [page_name, absolue URL]")
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
294 log.error(msg)
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
295 raise ValueError(msg)
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
296 page_name, url = menu
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
297 else:
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
298 page_name = menu
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
299 try:
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
300 url = cls.getPageByName(page_name).url
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
301 except KeyError as e:
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
302 log.error(_(u"Can'find a named page ({msg}), please check menu_json in configuration.").format(msg=e))
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 998
diff changeset
303 raise e
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
304 main_menu.append((page_name, url))
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
305 cls.main_menu = main_menu
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
306
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
307 @classmethod
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
308 def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
309 """register a URI handler
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
310
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
311 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
312 type/subtype as returned by tools/common/parseXMPPUri
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
313 @param get_uri_cb(callable): method which take uri_data dict as only argument
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
314 and return path with correct arguments relative to page itself
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
315 @param pre_path(list[unicode]): prefix path to reference the handler page
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
316 """
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
317 if uri_tuple in cls.uri_callbacks:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
318 log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
319 cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb,
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
320 u'pre_path': pre_path}
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
321
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
322 def registerSignal(self, request, signal, check_profile=True):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
323 r"""register a signal handler
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
324
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
325 the page must be dynamic
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
326 when signal is received, self.on_signal will be called with:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
327 - request
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
328 - signal name
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
329 - signal arguments
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
330 signal handler will be removed when connection with dynamic page will be lost
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
331 @param signal(unicode): name of the signal
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
332 last arg of signal must be profile, as it will be checked to filter signals
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
333 @param check_profile(bool): if True, signal profile (which MUST be last arg) will be
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
334 checked against session profile.
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
335 /!\ if False, profile will not be checked/filtered, be sure to know what you are doing
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
336 if you unset this option /!\
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
337 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
338 # FIXME: add a timeout, if socket is not opened before it, signal handler must be removed
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
339 if not self.dynamic:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
340 log.error(_(u"You can't register signal if page is not dynamic"))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
341 return
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
342 LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = (self, request, check_profile)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
343 request._signals_registered.append(signal)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
344
927
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
345 def getPagePathFromURI(self, uri):
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
346 """Retrieve page URL from xmpp: URI
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
347
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
348 @param uri(unicode): URI with a xmpp: scheme
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
349 @return (unicode,None): absolute path (starting from root "/") to page handling the URI
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
350 None is returned if not page has been registered for this URI
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
351 """
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
352 uri_data = common_uri.parseXMPPUri(uri)
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
353 try:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
354 callback_data = self.uri_callbacks[uri_data['type'], uri_data.get('sub_type')]
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
355 except KeyError:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
356 return
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
357 else:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
358 url = os.path.join(u'/', u'/'.join(callback_data['pre_path']), callback_data['callback'](self, uri_data))
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
359 return url
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
360
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
361 @classmethod
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
362 def getPageByName(cls, name):
936
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
363 """retrieve page instance from its name
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
364
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
365 @param name(unicode): name of the page
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
366 @return (LiberviaPage): page instance
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
367 @raise KeyError: the page doesn't exist
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
368 """
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
369 return cls.named_pages[name]
936
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
370
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
371 def getPageRedirectURL(self, request, page_name=u'login', url=None):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
372 """generate URL for a page with redirect_url parameter set
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
373
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
374 mainly used for login page with redirection to current page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
375 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
376 @param page_name(unicode): name of the page to go
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
377 @param url(None, unicode): url to redirect to
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
378 None to use request path (i.e. current page)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
379 @return (unicode): URL to use
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
380 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
381 return u'{root_url}?redirect_url={redirect_url}'.format(
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
382 root_url = self.getPageByName(page_name).url,
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
383 redirect_url=urllib.quote_plus(request.uri) if url is None else url.encode('utf-8'))
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
384
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
385 def getURL(self, *args):
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
386 """retrieve URL of the page set arguments
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
387
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
388 *args(list[unicode]): argument to add to the URL as path elements
1014
dfced7992460 pages: ignore empty or unset arguments in getURL
Goffi <goffi@goffi.org>
parents: 1013
diff changeset
389 empty or None arguments will be ignored
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
390 """
1014
dfced7992460 pages: ignore empty or unset arguments in getURL
Goffi <goffi@goffi.org>
parents: 1013
diff changeset
391 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
392
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
393 if self.name is not None and self.name in self.pages_redirects:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
394 # we check for redirection
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
395 redirect_data = self.pages_redirects[self.name]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
396 args_hash = tuple(args)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
397 for limit in xrange(len(args)+1):
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
398 current_hash = args_hash[:limit]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
399 if current_hash in redirect_data:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
400 url_base = redirect_data[current_hash]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
401 remaining = args[limit:]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
402 remaining_url = '/'.join(remaining)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
403 return os.path.join('/', url_base, remaining_url)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
404
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
405 return os.path.join(self.url, *url_args)
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
406
1010
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
407 def getCurrentURL(self, request):
1015
16d52917666c pages: fixed redirection
Goffi <goffi@goffi.org>
parents: 1014
diff changeset
408 """retrieve URL used to access this page
1010
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
409
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
410 @return(unicode): current URL
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
411 """
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
412 # 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
413 # request.prepath) because request.prepath may have been modified by
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
414 # redirection (if redirection args have been specified), while path reflect
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
415 # the real request
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
416
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
417 # we ignore empty path elements (i.e. double '/' or '/' at the end)
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
418 path_elts = [p for p in request.path.split('/') if p]
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
419
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
420 if request.postpath:
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
421 if not request.postpath[-1]:
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
422 # we remove trailing slash
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
423 request.postpath = request.postpath[:-1]
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
424 if request.postpath:
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
425 # getSubPageURL must return subpage from the point where
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
426 # the it is called, so we have to remove remanining
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
427 # path elements
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
428 path_elts = path_elts[:-len(request.postpath)]
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
429
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
430 return u'/' + '/'.join(path_elts).decode('utf-8')
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
431
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
432 def getParamURL(self, request, **kwargs):
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
433 """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
434
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
435 **kwargs(dict[str, unicode]): argument to use as query parameters
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
436 @return (unicode): constructed URL
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
437 """
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
438 current_url = self.getCurrentURL(request)
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
439 if kwargs:
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
440 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8')
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
441 current_url = current_url + u'?' + encoded
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
442 return current_url
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
443
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
444 def getSubPageURL(self, request, page_name, *args):
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
445 """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
446
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
447 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
448 i.e. it's more prepath than path).
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
449 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
450 by the resulting combination.
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
451 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
452 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
453 names of directories (i.e. relative URL will break if subdirectory is renamed
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
454 while getSubPageURL won't as long as page_name is consistent).
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
455 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
456 and potential redirections.
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
457 @param request(server.Request): current HTTP request
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
458 @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
459 it must be a direct children of current page
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
460 @param *args(list[unicode]): arguments to add as path elements
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
461 @return unicode: absolute URL to the sub page
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
462 """
1010
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
463 current_url = self.getCurrentURL(request)
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
464
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
465 for path, child in self.children.iteritems():
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
466 try:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
467 child_name = child.name
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
468 except AttributeError:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
469 # LiberviaPage have a name, but maybe this is an other Resource
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
470 continue
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
471 if child_name == page_name:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
472 return os.path.join(u'/', current_url, path, *args)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
473 raise exceptions.NotFound(_(u'requested sub page has not been found'))
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
474
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
475 def getChildWithDefault(self, path, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
476 # we handle children ourselves
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
477 raise exceptions.InternalError(u"this method should not be used with LiberviaPage")
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
478
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
479 def nextPath(self, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
480 """get next URL path segment, and update request accordingly
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
481
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
482 will move first segment of postpath in prepath
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
483 @param request(server.Request): current HTTP request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
484 @return (unicode): unquoted segment
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
485 @raise IndexError: there is no segment left
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
486 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
487 pathElement = request.postpath.pop(0)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
488 request.prepath.append(pathElement)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
489 return urllib.unquote(pathElement).decode('utf-8')
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
490
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
491 def checkCacheSubscribeCb(self, sub_id, service, node):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
492 self.cache_pubsub_sub.add((service, node, sub_id))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
493
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
494 def checkCacheSubscribeEb(self, failure_, service, node):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
495 log.warning(_(u"Can't subscribe to node: {msg}").format(msg=failure_))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
496 # FIXME: cache must be marked as unusable here
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
497
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
498 def psNodeWatchAddEb(self, failure_, service, node):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
499 log.warning(_(u"Can't add node watched: {msg}").format(msg=failure_))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
500
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
501 def checkCache(self, request, cache_type, **kwargs):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
502 """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
503
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
504 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
505 pubsub node)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
506 @param request(server.Request): current HTTP request
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
507 @param cache_type(int): on of C.CACHE_* const.
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
508 @param **kwargs: args according to cache_type:
1010
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
509 C.CACHE_PUBSUB:
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
510 service: pubsub service
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
511 node: pubsub node
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
512 short: short name of feature (needed if node is empty)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
513
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
514 """
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
515 if request.postpath:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
516 # we are not on the final page, no need to go further
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
517 return
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
518 profile = self.getProfile(request) or C.SERVICE_PROFILE
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
519
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
520 if cache_type == C.CACHE_PUBSUB:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
521 service, node = kwargs['service'], kwargs['node']
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
522 if not node:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
523 try:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
524 short = kwargs['short']
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
525 node = self.host.ns_map[short]
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
526 except KeyError:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
527 log.warning(_(u"Can't use cache for empty node without namespace set, please ensure to set \"short\" and that it is registered"))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
528 return
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
529 if profile != C.SERVICE_PROFILE:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
530 # only service profile is cache for now
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
531 return
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
532 try:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
533 cache = self.cache[profile][cache_type][service][node][self]
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
534 except KeyError:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
535 # no cache yet, let's subscribe to the pubsub node
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
536 d1 = self.host.bridgeCall('psSubscribe', service.full(), node, {}, profile)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
537 d1.addCallback(self.checkCacheSubscribeCb, service, node)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
538 d1.addErrback(self.checkCacheSubscribeEb, service, node)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
539 d2 = self.host.bridgeCall('psNodeWatchAdd', service.full(), node, profile)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
540 d2.addErrback(self.psNodeWatchAddEb, service, node)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
541 self._do_cache = [profile, cache_type, service, node]
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
542 # we don't return the Deferreds as it is not needed to wait for
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
543 # the subscription to continue with page rendering
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
544 return
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
545
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
546 else:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
547 raise exceptions.InternalError(u'Unknown cache_type')
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
548 log.debug(u'using cache for {page}'.format(page=self))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
549 cache.last_access = time.time()
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
550 request.write(cache.rendered)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
551 request.finish()
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
552 raise failure.Failure(exceptions.CancelError(u'cache is used'))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
553
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
554 def _cacheURL(self, dummy, request, profile):
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
555 self.cached_urls.setdefault(profile, {})[request.uri] = CacheURL(request)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
556
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
557 @classmethod
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
558 def onNodeEvent(cls, host, service, node, event_type, items, profile):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
559 """Invalidate cache for all pages linked to this node"""
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
560 try:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
561 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
562 except KeyError:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
563 log.info(_(u'Removing subscription for {service}/{node}: '
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
564 u'the page is not cached').format(service=service, node=node))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
565 d1 = host.bridgeCall('psUnsubscribe', service, node, profile)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
566 d1.addErrback(lambda failure_:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
567 log.warning(_(u"Can't unsubscribe from {service}/{node}: {msg}").format(
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
568 service=service, node=node, msg=failure_)))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
569 d2 = host.bridgeCall('psNodeWatchAdd', service, node, profile)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
570 # TODO: check why the page is not in cache, remove subscription?
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
571 d2.addErrback(lambda failure_:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
572 log.warning(_(u"Can't remove watch for {service}/{node}: {msg}").format(
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
573 service=service, node=node, msg=failure_)))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
574 else:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
575 cache.clear()
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
576
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
577 @classmethod
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
578 def onSignal(cls, host, signal, *args):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
579 """Generic method which receive registered signals
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
580
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
581 if a callback is registered for this signal, call it
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
582 @param host: Libervia instance
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
583 @param signal(unicode): name of the signal
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
584 @param *args: args of the signals
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
585 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
586 for page, request, check_profile in cls.signals_handlers.get(signal, {}).itervalues():
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
587 if check_profile:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
588 signal_profile = args[-1]
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
589 request_profile = page.getProfile(request)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
590 if not request_profile:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
591 # if you want to use signal without session, unset check_profile
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
592 # (be sure to know what you are doing)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
593 log.error(_(u"no session started, signal can't be checked"))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
594 continue
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
595 if signal_profile != request_profile:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
596 # we ignore the signal, it's not for our profile
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
597 continue
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
598 if request._signals_cache is not None:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
599 # socket is not yet opened, we cache the signal
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
600 request._signals_cache.append((request, signal, args))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
601 log.debug(u"signal [{signal}] cached: {args}".format(
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
602 signal = signal,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
603 args = args))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
604 else:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
605 page.on_signal(page, request, signal, *args)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
606
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
607 def onSocketOpen(self, request):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
608 """Called for dynamic pages when socket has just been opened
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
609
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
610 we send all cached signals
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
611 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
612 assert request._signals_cache is not None
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
613 cache = request._signals_cache
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
614 request._signals_cache = None
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
615 for request, signal, args in cache:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
616 self.on_signal(self, request, signal, *args)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
617
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
618 def onSocketClose(self, request):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
619 """Called for dynamic pages when socket has just been closed
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
620
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
621 we remove signal handler
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
622 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
623 for signal in request._signals_registered:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
624 try:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
625 del LiberviaPage.signals_handlers[signal][id(request)]
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
626 except KeyError:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
627 log.error(_(u"Can't find signal handler for [{signal}], this should not happen").format(
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
628 signal = signal))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
629 else:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
630 log.debug(_(u"Removed signal handler"))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
631
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
632 def HTTPRedirect(self, request, url):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
633 """redirect to an URL using HTTP redirection
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
634
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
635 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
636 @param url(unicode): url to redirect to
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
637 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
638
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
639 web_util.redirectTo(url.encode('utf-8'), request)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
640 request.finish()
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
641 raise failure.Failure(exceptions.CancelError(u'HTTP redirection is used'))
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
642
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
643 def redirectOrContinue(self, request, redirect_arg=u'redirect_url'):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
644 """helper method to redirect a page to an url given as arg
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
645
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
646 if the arg is not present, the page will continue normal workflow
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
647 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
648 @param redirect_arg(unicode): argument to use to get redirection URL
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
649 @interrupt: redirect the page to requested URL
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
650 @interrupt pageError(C.HTTP_BAD_REQUEST): empty or non local URL is used
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
651 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
652 try:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
653 url = self.getPostedData(request, 'redirect_url')
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
654 except KeyError:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
655 pass
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
656 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
657 # a redirection is requested
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
658 if not url or url[0] != u'/':
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
659 # we only want local urls
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
660 self.pageError(request, C.HTTP_BAD_REQUEST)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
661 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
662 self.HTTPRedirect(request, url)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
663
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
664 def pageRedirect(self, page_path, request, skip_parse_url=True):
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
665 """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
666
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
667 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
668 skipping named page's parse_url method if it exist.
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
669 If you want to do a HTTP redirection, use HTTPRedirect
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
670 @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
671 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
672 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
673 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
674 - 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
675 - 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
676 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
677 "blog/atom.xml" redirect to atom.xml subpage of "blog"
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
678 "/common/blog/atom.xml" redirect to the page at the fiven full path
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
679 @param request(server.Request): current HTTP request
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
680 @param skip_parse_url(bool): if True, parse_url method on redirect page will be skipped
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
681 @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
682 """
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
683 # FIXME: render non LiberviaPage resources
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
684 path = page_path.rstrip(u'/').split(u'/')
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
685 if not path[0]:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
686 redirect_page = self.host.root
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
687 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
688 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
689
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
690 for subpage in path[1:]:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
691 if redirect_page is self.host.root:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
692 redirect_page = redirect_page.children[subpage]
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
693 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
694 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
695
1015
16d52917666c pages: fixed redirection
Goffi <goffi@goffi.org>
parents: 1014
diff changeset
696 redirect_page.renderPage(request, skip_parse_url=skip_parse_url)
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
697 raise failure.Failure(exceptions.CancelError(u'page redirection is used'))
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
698
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
699 def pageError(self, request, code=C.HTTP_NOT_FOUND):
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
700 """generate an error page and terminate the request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
701
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
702 @param request(server.Request): HTTP request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
703 @param core(int): error code to use
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
704 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
705 template = u'error/' + unicode(code) + '.html'
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
706
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
707 request.setResponseCode(code)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
708
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
709 rendered = self.host.renderer.render(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
710 template,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
711 root_path = '/templates/',
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
712 error_code = code,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
713 **request.template_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
714
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
715 self.writeData(rendered, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
716 raise failure.Failure(exceptions.CancelError(u'error page is used'))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
717
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
718 def writeData(self, data, request):
937
00236973e138 server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents: 936
diff changeset
719 """write data to transport and finish the request"""
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
720 if data is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
721 self.pageError(request)
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
722 data_encoded = data.encode('utf-8')
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
723 request.write(data_encoded)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
724 request.finish()
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
725 if self._do_cache is not None:
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
726 cache = reduce(lambda d, k: d.setdefault(k, {}), self._do_cache, self.cache)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
727 cache[self] = Cache(data_encoded)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
728 log.debug(_(u'{page} put in cache for [{profile}]').format(
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
729 page=self,
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
730 profile=self._do_cache[0]))
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
731 self._do_cache = None
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
732
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
733 def _subpagesHandler(self, dummy, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
734 """render subpage if suitable
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
735
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
736 this method checks if there is still an unmanaged part of the path
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
737 and check if it corresponds to a subpage. If so, it render the subpage
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
738 else it render a NoResource.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
739 If there is no unmanaged part of the segment, current page workflow is pursued
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
740 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
741 if request.postpath:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
742 subpage = self.nextPath(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
743 try:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
744 child = self.children[subpage]
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
745 except KeyError:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
746 self.pageError(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
747 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
748 child.render(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
749 raise failure.Failure(exceptions.CancelError(u'subpage page is used'))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
750
998
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
751 def _prepare_dynamic(self, dummy, request):
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
752 # 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
753 # we set data for template, and create/register token
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
754 socket_token = unicode(uuid.uuid4())
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
755 socket_url = self.host.getWebsocketURL(request)
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
756 socket_debug = C.boolConst(self.host.debug)
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
757 request.template_data['websocket'] = WebsocketMeta(socket_url, socket_token, socket_debug)
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
758 self.host.registerWSToken(socket_token, self, request)
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
759 # 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
760 request._signals_registered = []
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
761 # 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
762 request._signals_cache = []
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
763
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
764 def _prepare_render(self, dummy, request):
926
612e33fd32a8 server (pages): fixed _prepare_render handling
Goffi <goffi@goffi.org>
parents: 925
diff changeset
765 return defer.maybeDeferred(self.prepare_render, self, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
766
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
767 def _render_method(self, dummy, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
768 return defer.maybeDeferred(self.render_method, self, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
769
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
770 def _render_template(self, dummy, request):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
771 template_data = request.template_data
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
772
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
773 # if confirm variable is set in case of successfuly data post
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
774 session_data = self.host.getSessionData(request, session_iface.ISATSession)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
775 if session_data.popPageFlag(self, C.FLAG_CONFIRM):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
776 template_data[u'confirm'] = True
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
777
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
778 return self.host.renderer.render(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
779 self.template,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
780 root_path = '/templates/',
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
781 media_path = '/' + C.MEDIA_DIR,
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
782 cache_path = session_data.cache_dir,
990
6daa59d44ee2 pages: menu implementation, first draft:
Goffi <goffi@goffi.org>
parents: 985
diff changeset
783 main_menu = LiberviaPage.main_menu,
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
784 **template_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
785
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
786 def _renderEb(self, failure_, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
787 """don't raise error on CancelError"""
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
788 failure_.trap(exceptions.CancelError)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
789
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
790 def _internalError(self, failure_, request):
00236973e138 server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents: 936
diff changeset
791 """called if an error is not catched"""
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
792 log.error(_(u"Uncatched error for HTTP request on {url}: {msg}").format(
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
793 url = request.URLPath(),
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
794 msg = failure_))
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
795 self.pageError(request, C.HTTP_INTERNAL_ERROR)
00236973e138 server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents: 936
diff changeset
796
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
797 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
798 """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
799
968
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
800 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
801 this method redirect to the same page or to request.data['post_redirect_page']
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
802 post_redirect_page can be either a page or a tuple with page as first item, then a list of unicode arguments to append to the url.
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
803 if post_redirect_page is not used, initial request.uri (i.e. the same page as where the data have been posted) will be used for redirection.
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
804 HTTP status code "See Other" (303) is used as it is the recommanded code in this case.
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
805 @param ret(None, unicode, iterable): on_data_post return value
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
806 see LiberviaPage.__init__ on_data_post docstring
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
807 """
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
808 if ret is None:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
809 ret = ()
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
810 elif isinstance(ret, basestring):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
811 ret = (ret,)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
812 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
813 ret = tuple(ret)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
814 raise NotImplementedError(_(u'iterable in on_data_post return value is not used yet'))
957
67bf14c91d5c server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
815 session_data = self.host.getSessionData(request, session_iface.ISATSession)
974
4aa38c49bff7 pages: fixed use of request data in _on_data_post_redirect
Goffi <goffi@goffi.org>
parents: 972
diff changeset
816 request_data = self.getRData(request)
4aa38c49bff7 pages: fixed use of request data in _on_data_post_redirect
Goffi <goffi@goffi.org>
parents: 972
diff changeset
817 if 'post_redirect_page' in request_data:
4aa38c49bff7 pages: fixed use of request data in _on_data_post_redirect
Goffi <goffi@goffi.org>
parents: 972
diff changeset
818 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
819 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
820 redirect_page = redirect_page_data[0]
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
821 redirect_page_args = redirect_page_data[1:]
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
822 redirect_uri = redirect_page.getURL(*redirect_page_args)
968
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
823 else:
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
824 redirect_page = redirect_page_data
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
825 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
826 else:
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
827 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
828 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
829
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
830 if not C.POST_NO_CONFIRM in ret:
968
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
831 session_data.setPageFlag(redirect_page, C.FLAG_CONFIRM)
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
832 request.setResponseCode(C.HTTP_SEE_OTHER)
968
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
833 request.setHeader("location", redirect_uri)
955
4f7cb6335a33 server(pages): do Post/Redirect/Get pattern when on_data_post is used (avoid double posting on refresh)
Goffi <goffi@goffi.org>
parents: 950
diff changeset
834 request.finish()
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
835 raise failure.Failure(exceptions.CancelError(u'Post/Redirect/Get is used'))
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
836
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
837 def _on_data_post(self, dummy, request):
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
838 csrf_token = self.host.getSessionData(request, session_iface.ISATSession).csrf_token
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
839 try:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
840 given_csrf = self.getPostedData(request, u'csrf_token')
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
841 except KeyError:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
842 given_csrf = None
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
843 if given_csrf is None or given_csrf != csrf_token:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
844 log.warning(_(u"invalid CSRF token, hack attempt? URL: {url}, IP: {ip}").format(
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
845 url=request.uri,
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
846 ip=request.getClientIP()))
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
847 self.pageError(request, C.HTTP_UNAUTHORIZED)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
848 d = defer.maybeDeferred(self.on_data_post, self, request)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
849 d.addCallback(self._on_data_post_redirect, request)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
850 return d
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
851
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
852 def getPostedData(self, request, keys, multiple=False):
1010
4de970de87d7 pages: added getCurrentURL and getParamURL:
Goffi <goffi@goffi.org>
parents: 1009
diff changeset
853 """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
854
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
855 @param request(server.Request): request linked to the session
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
856 @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
857 unicode to get one value
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
858 iterable to get more than one
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
859 @param multiple(bool): True if multiple values are possible/expected
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
860 if False, the first value is returned
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
861 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): values received for this(these) key(s)
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
862 @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
863 """
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
864 if isinstance(keys, basestring):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
865 keys = [keys]
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
866 get_first = True
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
867 else:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
868 get_first = False
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
869
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
870 ret = []
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
871 for key in keys:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
872 gen = (urllib.unquote(v).decode('utf-8') for v in request.args.get(key,[]))
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
873 if multiple:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
874 ret.append(gen)
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
875 else:
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
876 try:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
877 ret.append(next(gen))
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
878 except StopIteration:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
879 raise KeyError(key)
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
880
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
881 return ret[0] if get_first else ret
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
882
1009
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
883 def getAllPostedData(self, request, except_=(), multiple=True):
959
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
884 """get all posted data
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
885
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
886 @param request(server.Request): request linked to the session
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
887 @param except_(iterable[unicode]): key of values to ignore
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
888 csrf_token will always be ignored
1009
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
889 @param multiple(bool): if False, only the first values are returned
959
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
890 @return (dict[unicode, list[unicode]]): post values
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
891 """
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
892 except_ = tuple(except_) + (u'csrf_token',)
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
893 ret = {}
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
894 for key, values in request.args.iteritems():
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
895 key = urllib.unquote(key).decode('utf-8')
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
896 if key in except_:
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
897 continue
1009
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
898 if not multiple:
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
899 ret[key] = urllib.unquote(values[0]).decode('utf-8')
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
900 else:
b57f86bc1177 pages: added "multiple" argument to getAllPostedData
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
901 ret[key] = [urllib.unquote(v).decode('utf-8') for v in values]
959
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
902 return ret
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
903
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
904 def getProfile(self, request):
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
905 """helper method to easily get current profile
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
906
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
907 @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
908 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
909 """
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
910 sat_session = self.host.getSessionData(request, session_iface.ISATSession)
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
911 return sat_session.profile
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
912
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
913 def getRData(self, request):
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
914 """helper method to get request data dict
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
915
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
916 this dictionnary if for the request only, it is not saved in session
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
917 It is mainly used to pass data between pages/methods called during request workflow
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
918 @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
919 """
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
920 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
921 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
922 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
923 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
924 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
925
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
926 def _checkAccess(self, data, request):
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
927 """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
928
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
929 if access is not granted, show a HTTP_UNAUTHORIZED pageError and stop request,
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
930 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
931 """
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
932 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
933 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
934 elif self.access == C.PAGES_ACCESS_PROFILE:
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
935 profile = self.getProfile(request)
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
936 if not profile:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
937 # no session started
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
938 if not self.host.options["allow_registration"]:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
939 # registration not allowed, access is not granted
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
940 self.pageError(request, C.HTTP_UNAUTHORIZED)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
941 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
942 # registration allowed, we redirect to login page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
943 login_url = self.getPageRedirectURL(request)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
944 self.HTTPRedirect(request, login_url)
922
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
945
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
946 return data
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
947
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
948 def renderPartial(self, request, template, template_data):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
949 """Render a template to be inserted in dynamic page
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
950
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
951 this is NOT the normal page rendering method, it is used only to update
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
952 dynamic pages
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
953 @param template(unicode): path of the template to render
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
954 @param template_data(dict): template_data to use
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
955 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
956 if not self.dynamic:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
957 raise exceptions.InternalError(_(u"renderPartial must only be used with dynamic pages"))
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
958 session_data = self.host.getSessionData(request, session_iface.ISATSession)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
959
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
960 return self.host.renderer.render(
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
961 template,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
962 root_path = '/templates/',
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
963 media_path = '/' + C.MEDIA_DIR,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
964 cache_path = session_data.cache_dir,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
965 main_menu = LiberviaPage.main_menu,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
966 **template_data)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
967
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
968 def renderAndUpdate(self, request, template, selectors, template_data_update, update_type="append"):
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
969 """Helper method to render a partial page element and update the page
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
970
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
971 this is NOT the normal page rendering method, it is used only to update
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
972 dynamic pages
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
973 @param request(server.Request): current HTTP request
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
974 @param template: same as for [renderPartial]
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
975 @param selectors: CSS selectors to use
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
976 @param template_data_update: template data to use
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
977 template data cached in request will be copied then updated
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
978 with this data
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
979 @parap update_type(unicode): one of:
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
980 append: append rendered element to selected element
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
981 """
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
982 template_data = request.template_data.copy()
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
983 template_data.update(template_data_update)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
984 html = self.renderPartial(request, template, template_data)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
985 request.sendData(u'dom',
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
986 selectors=selectors,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
987 update_type=update_type,
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
988 html=html)
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
989
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
990 def renderPage(self, request, skip_parse_url=False):
937
00236973e138 server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents: 936
diff changeset
991 """Main method to handle the workflow of a LiberviaPage"""
994
b92b06f023cb pages: profile is now always set in template_data, and None if there is no user logged
Goffi <goffi@goffi.org>
parents: 990
diff changeset
992
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
993 # template_data are the variables passed to template
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
994 if not hasattr(request, 'template_data'):
957
67bf14c91d5c server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
995 session_data = self.host.getSessionData(request, session_iface.ISATSession)
67bf14c91d5c server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
996 csrf_token = session_data.csrf_token
994
b92b06f023cb pages: profile is now always set in template_data, and None if there is no user logged
Goffi <goffi@goffi.org>
parents: 990
diff changeset
997 request.template_data = {u'profile': session_data.profile,
b92b06f023cb pages: profile is now always set in template_data, and None if there is no user logged
Goffi <goffi@goffi.org>
parents: 990
diff changeset
998 u'csrf_token': csrf_token}
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
999
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
1000 # 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
1001 # at the beginning of the request hanling
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1002 if request.postpath and not request.postpath[-1]:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1003 # we don't differenciate URLs finishing with '/' or not
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1004 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
1005
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1006 d = defer.Deferred()
922
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
1007 d.addCallback(self._checkAccess, request)
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
1008
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1009 if self.redirect is not None:
1015
16d52917666c pages: fixed redirection
Goffi <goffi@goffi.org>
parents: 1014
diff changeset
1010 d.addCallback(lambda dummy: self.pageRedirect(self.redirect, request, skip_parse_url=False))
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1011
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1012 if self.parse_url is not None and not skip_parse_url:
1018
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1013 if self.url_cache:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1014 profile = self.getProfile(request)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1015 try:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1016 cache_url = self.cached_urls[profile][request.uri]
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1017 except KeyError:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1018 # no cache for this URI yet
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1019 # we do normal URL parsing, and then the cache
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1020 d.addCallback(self.parse_url, request)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1021 d.addCallback(self._cacheURL, request, profile)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1022 else:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1023 log.debug(_(u"using URI cache for {page}").format(page=self))
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1024 cache_url.use(request)
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1025 else:
78af5457d3f8 Pages: added url_cache setting:
Goffi <goffi@goffi.org>
parents: 1015
diff changeset
1026 d.addCallback(self.parse_url, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1027
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1028 d.addCallback(self._subpagesHandler, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1029
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1030 if request.method not in (C.HTTP_METHOD_GET, C.HTTP_METHOD_POST):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1031 # only HTTP GET and POST are handled so far
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1032 d.addCallback(lambda dummy: self.pageError(request, C.HTTP_BAD_REQUEST))
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1033
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1034 if request.method == C.HTTP_METHOD_POST:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1035 if self.on_data_post is None:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1036 # if we don't have on_data_post, the page was not expecting POST
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1037 # so we return an error
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1038 d.addCallback(lambda dummy: self.pageError(request, C.HTTP_BAD_REQUEST))
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1039 else:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1040 d.addCallback(self._on_data_post, request)
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1041 # by default, POST follow normal behaviour after on_data_post is called
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1042 # this can be changed by a redirection or other method call in on_data_post
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1043
998
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
1044 if self.dynamic:
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
1045 d.addCallback(self._prepare_dynamic, request)
0848b8b0188d pages: dynamic set up is now done just before prepare_render call:
Goffi <goffi@goffi.org>
parents: 995
diff changeset
1046
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1047 if self.prepare_render:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1048 d.addCallback(self._prepare_render, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1049
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1050 if self.template:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
1051 d.addCallback(self._render_template, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1052 elif self.render_method:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1053 d.addCallback(self._render_method, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1054
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1055 d.addCallback(self.writeData, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1056 d.addErrback(self._renderEb, request)
937
00236973e138 server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
Goffi <goffi@goffi.org>
parents: 936
diff changeset
1057 d.addErrback(self._internalError, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1058 d.callback(self)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1059 return server.NOT_DONE_YET
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1060
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1061 def render_GET(self, request):
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1062 return self.renderPage(request)
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
1063
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1064 def render_POST(self, request):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
1065 return self.renderPage(request)