annotate src/server/pages.py @ 984:f0fc28b3bd1e

server: moved LiberviaPage code in its own module
author Goffi <goffi@goffi.org>
date Fri, 17 Nov 2017 12:10:56 +0100
parents src/server/server.py@bcacf970f970
children 64826e69f365
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
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
23 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
24
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
25 from sat.core.i18n import _
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
26 from sat.core import exceptions
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
27 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
28 from sat.core.log import getLogger
582c435dab6b server side: new log system is used
Goffi <goffi@goffi.org>
parents: 435
diff changeset
29 log = getLogger(__name__)
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
30 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
31 from libervia.server import session_iface
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
32 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
33 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
34
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
35 import os.path
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
36 import urllib
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
37
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
38
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
39 class LiberviaPage(web_resource.Resource):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
40 isLeaf = True # we handle subpages ourself
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
41 named_pages = {}
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
42 uri_callbacks = {}
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
43 pages_redirects = {}
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
44
978
c8cafe316f6f pages: removed trailing_slash option:
Goffi <goffi@goffi.org>
parents: 974
diff changeset
45 def __init__(self, host, root_dir, url, name=None, redirect=None, access=None, parse_url=None,
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
46 prepare_render=None, render=None, template=None, on_data_post=None):
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
47 """initiate LiberviaPages
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
48
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
49 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
50 The arguments are the variables found in page_meta.py
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
51 @param host(Libervia): the running instance of Libervia
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
52 @param root_dir(unicode): aboslute file path of the page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
53 @param url(unicode): relative URL to the page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
54 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
55 @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
56 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
57 "/" 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
58 @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
59 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
60 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
61 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
62 can still be used, e.g. parse_url)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
63 @param access(unicode, None): permission needed to access the page
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
64 None means public access.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
65 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
66 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
67 see C.PAGES_ACCESS_* for details
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
68 @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
69 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
70 else a the request will be transmitted to a subpage
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
71 @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
72 that often means gathering data using the bridge
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
73 @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
74 what it returns will be rendered.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
75 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
76 @param template(unicode, None): path to the template to render.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
77 This method is mutually exclusive with render
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
78 @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
79 None if not post is handled
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
80 on_data_post can return a string with following value:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
81 - C.POST_NO_CONFIRM: confirm flag will not be set
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
82 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
83
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
84 web_resource.Resource.__init__(self)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
85 self.host = host
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
86 self.root_dir = root_dir
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
87 self.url = url
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
88 self.name = name
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
89 if name is not None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
90 if name in self.named_pages:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
91 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
92 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
93 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
94 if not name:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
95 raise ValueError(_(u"a page name can't be empty"))
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
96 self.named_pages[name] = self
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
97 if access is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
98 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
99 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
100 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
101 self.access = access
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
102 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
103 # 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
104 # 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
105 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
106 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
107 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
108 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
109 self.redirect = redirect
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
110 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
111 self.redirect = None
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
112 self.parse_url = parse_url
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
113 self.prepare_render = prepare_render
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
114 self.template = template
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
115 self.render_method = render
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
116 self.on_data_post = on_data_post
922
16d1084d1371 server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
Goffi <goffi@goffi.org>
parents: 921
diff changeset
117 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
118 # 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
119 return
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
120 if template is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
121 if not callable(render):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
122 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
123 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
124 if render is not None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
125 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
126 if parse_url is not None and not callable(parse_url):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
127 log.error(_(u"parse_url must be a callable"))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
128
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
129 @classmethod
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
130 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
131 """Recursively import Libervia pages"""
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
132 if path is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
133 path = []
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
134 if parent is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
135 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
136 parent = host
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
137 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
138 root_dir = parent.root_dir
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
139 for d in os.listdir(root_dir):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
140 dir_path = os.path.join(root_dir, d)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
141 if not os.path.isdir(dir_path):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
142 continue
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
143 meta_path = os.path.join(dir_path, C.PAGES_META_FILE)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
144 if os.path.isfile(meta_path):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
145 page_data = {}
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
146 new_path = path + [d]
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
147 # we don't want to force the presence of __init__.py
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
148 # so we use execfile instead of import.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
149 # TODO: when moved to Python 3, __init__.py is not mandatory anymore
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
150 # so we can switch to import
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
151 execfile(meta_path, page_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
152 resource = LiberviaPage(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
153 host,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
154 dir_path,
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
155 u'/' + u'/'.join(new_path),
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
156 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
157 redirect=page_data.get('redirect'),
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
158 access=page_data.get('access'),
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
159 parse_url=page_data.get('parse_url'),
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
160 prepare_render=page_data.get('prepare_render'),
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
161 render=page_data.get('render'),
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
162 template=page_data.get('template'),
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
163 on_data_post=page_data.get('on_data_post'))
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
164 parent.putChild(d, resource)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
165 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
166 if 'uri_handlers' in page_data:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
167 if not isinstance(page_data, dict):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
168 log.error(_(u'uri_handlers must be a dict'))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
169 else:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
170 for uri_tuple, cb_name in page_data['uri_handlers'].iteritems():
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
171 if len(uri_tuple) != 2 or not isinstance(cb_name, basestring):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
172 log.error(_(u"invalid uri_tuple"))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
173 continue
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
174 log.info(_(u'setting {}/{} URIs handler').format(*uri_tuple))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
175 try:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
176 cb = page_data[cb_name]
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
177 except KeyError:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
178 log.error(_(u'missing {name} method to handle {1}/{2}').format(
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
179 name = cb_name, *uri_tuple))
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
180 continue
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
181 else:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
182 cls.registerURI(uri_tuple, cb, new_path)
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
183
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
184 LiberviaPage.importPages(host, resource, new_path)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
185
925
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
186 @classmethod
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
187 def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
188 """register a URI handler
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
189
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
190 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
191 type/subtype as returned by tools/common/parseXMPPUri
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
192 @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
193 and return path with correct arguments relative to page itself
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
194 @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
195 """
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
196 if uri_tuple in cls.uri_callbacks:
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
197 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
198 cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb,
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
199 u'pre_path': pre_path}
e00151140f77 server (pages): URIs handling:
Goffi <goffi@goffi.org>
parents: 924
diff changeset
200
927
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
201 def getPagePathFromURI(self, uri):
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
202 """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
203
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
204 @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
205 @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
206 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
207 """
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
208 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
209 try:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
210 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
211 except KeyError:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
212 return
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
213 else:
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
214 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
215 return url
bb4dfc2802c0 server (pages): added getPagePathFromURI method to retrieve page handling an URI
Goffi <goffi@goffi.org>
parents: 926
diff changeset
216
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
217 @classmethod
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
218 def getPageByName(cls, name):
936
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
219 """retrieve page instance from its name
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
220
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
221 @param name(unicode): name of the page
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
222 @return (LiberviaPage): page instance
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
223 @raise KeyError: the page doesn't exist
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
224 """
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
225 return cls.named_pages[name]
936
78692d47340d server (pages): added getPageByName
Goffi <goffi@goffi.org>
parents: 935
diff changeset
226
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
227 def getPageRedirectURL(self, request, page_name=u'login', url=None):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
228 """generate URL for a page with redirect_url parameter set
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
229
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
230 mainly used for login page with redirection to current page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
231 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
232 @param page_name(unicode): name of the page to go
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
233 @param url(None, unicode): url to redirect to
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
234 None to use request path (i.e. current page)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
235 @return (unicode): URL to use
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
236 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
237 return u'{root_url}?redirect_url={redirect_url}'.format(
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
238 root_url = self.getPageByName(page_name).url,
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
239 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
240
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
241 def getURL(self, *args):
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
242 """retrieve URL of the page set arguments
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
243
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
244 *args(list[unicode]): argument to add to the URL as path elements
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
245 """
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
246 url_args = [quote(a) for a in args]
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
247
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
248 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
249 # we check for redirection
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
250 redirect_data = self.pages_redirects[self.name]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
251 args_hash = tuple(args)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
252 for limit in xrange(len(args)+1):
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
253 current_hash = args_hash[:limit]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
254 if current_hash in redirect_data:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
255 url_base = redirect_data[current_hash]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
256 remaining = args[limit:]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
257 remaining_url = '/'.join(remaining)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
258 return os.path.join('/', url_base, remaining_url)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
259
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
260 return os.path.join(self.url, *url_args)
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
261
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
262 def getSubPageURL(self, request, page_name, *args):
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
263 """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
264
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
265 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
266 i.e. it's more prepath than path).
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
267 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
268 by the resulting combination.
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
269 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
270 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
271 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
272 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
273 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
274 and potential redirections.
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
275 @param request(server.Request): current HTTP request
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
276 @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
277 it must be a direct children of current page
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
278 @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
279 @return unicode: absolute URL to the sub page
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
280 """
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
281 # we get url in the following way (splitting request.path instead of using
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
282 # request.prepath) because request.prepath may have been modified by
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
283 # redirection (if redirection args have been specified), while path reflect
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
284 # the real request
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
285
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
286 # we ignore empty path elements (i.e. double '/' or '/' at the end)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
287 path_elts = [p for p in request.path.split('/') if p]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
288
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
289 if request.postpath:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
290 if not request.postpath[-1]:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
291 # we remove trailing slash
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
292 request.postpath = request.postpath[:-1]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
293 if request.postpath:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
294 # getSubPageURL must return subpage from the point where
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
295 # the it is called, so we have to remove remanining
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
296 # path elements
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
297 path_elts = path_elts[:-len(request.postpath)]
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
298
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
299 current_url = '/' + '/'.join(path_elts).decode('utf-8')
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
300
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
301 for path, child in self.children.iteritems():
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
302 try:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
303 child_name = child.name
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
304 except AttributeError:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
305 # 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
306 continue
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
307 if child_name == page_name:
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
308 return os.path.join(u'/', current_url, path, *args)
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
309 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
310
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
311 def getChildWithDefault(self, path, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
312 # we handle children ourselves
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
313 raise exceptions.InternalError(u"this method should not be used with LiberviaPage")
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
314
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
315 def nextPath(self, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
316 """get next URL path segment, and update request accordingly
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
317
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
318 will move first segment of postpath in prepath
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
319 @param request(server.Request): current HTTP request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
320 @return (unicode): unquoted segment
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
321 @raise IndexError: there is no segment left
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
322 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
323 pathElement = request.postpath.pop(0)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
324 request.prepath.append(pathElement)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
325 return urllib.unquote(pathElement).decode('utf-8')
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
326
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
327 def HTTPRedirect(self, request, url):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
328 """redirect to an URL using HTTP redirection
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
329
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
330 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
331 @param url(unicode): url to redirect to
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
332 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
333
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
334 web_util.redirectTo(url.encode('utf-8'), request)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
335 request.finish()
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
336 raise failure.Failure(exceptions.CancelError(u'HTTP redirection is used'))
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
337
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
338 def redirectOrContinue(self, request, redirect_arg=u'redirect_url'):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
339 """helper method to redirect a page to an url given as arg
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
340
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
341 if the arg is not present, the page will continue normal workflow
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
342 @param request(server.Request): current HTTP request
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
343 @param redirect_arg(unicode): argument to use to get redirection URL
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
344 @interrupt: redirect the page to requested URL
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
345 @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
346 """
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
347 try:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
348 url = self.getPostedData(request, 'redirect_url')
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
349 except KeyError:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
350 pass
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
351 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
352 # a redirection is requested
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
353 if not url or url[0] != u'/':
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
354 # we only want local urls
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
355 self.pageError(request, C.HTTP_BAD_REQUEST)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
356 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
357 self.HTTPRedirect(request, url)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
358
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
359 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
360 """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
361
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
362 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
363 skipping named page's parse_url method if it exist.
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
364 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
365 @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
366 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
367 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
368 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
369 - 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
370 - 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
371 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
372 "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
373 "/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
374 @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
375 @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
376 @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
377 """
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
378 # 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
379 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
380 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
381 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
382 else:
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
383 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
384
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
385 for subpage in path[1:]:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
386 if redirect_page is self.host.root:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
387 redirect_page = redirect_page.children[subpage]
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
388 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
389 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
390
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
391 redirect_page.renderPage(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
392 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
393
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
394 def pageError(self, request, code=C.HTTP_NOT_FOUND):
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
395 """generate an error page and terminate the request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
396
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
397 @param request(server.Request): HTTP request
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
398 @param core(int): error code to use
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
399 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
400 template = u'error/' + unicode(code) + '.html'
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
401
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
402 request.setResponseCode(code)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
403
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
404 rendered = self.host.renderer.render(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
405 template,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
406 root_path = '/templates/',
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
407 error_code = code,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
408 **request.template_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
409
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
410 self.writeData(rendered, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
411 raise failure.Failure(exceptions.CancelError(u'error page is used'))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
412
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
413 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
414 """write data to transport and finish the request"""
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
415 if data is None:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
416 self.pageError(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
417 request.write(data.encode('utf-8'))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
418 request.finish()
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
419
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
420 def _subpagesHandler(self, dummy, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
421 """render subpage if suitable
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
422
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
423 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
424 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
425 else it render a NoResource.
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
426 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
427 """
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
428 if request.postpath:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
429 subpage = self.nextPath(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
430 try:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
431 child = self.children[subpage]
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
432 except KeyError:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
433 self.pageError(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
434 else:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
435 child.render(request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
436 raise failure.Failure(exceptions.CancelError(u'subpage page is used'))
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
437
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
438 def _prepare_render(self, dummy, request):
926
612e33fd32a8 server (pages): fixed _prepare_render handling
Goffi <goffi@goffi.org>
parents: 925
diff changeset
439 return defer.maybeDeferred(self.prepare_render, self, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
440
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
441 def _render_method(self, dummy, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
442 return defer.maybeDeferred(self.render_method, self, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
443
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
444 def _render_template(self, dummy, request):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
445 template_data = request.template_data
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
446
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
447 # if confirm variable is set in case of successfuly data post
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
448 session_data = self.host.getSessionData(request, session_iface.ISATSession)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
449 if session_data.popPageFlag(self, C.FLAG_CONFIRM):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
450 template_data[u'confirm'] = True
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
451
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
452 return self.host.renderer.render(
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
453 self.template,
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
454 root_path = '/templates/',
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
455 media_path = '/' + C.MEDIA_DIR,
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
456 **template_data)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
457
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
458 def _renderEb(self, failure_, request):
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
459 """don't raise error on CancelError"""
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
460 failure_.trap(exceptions.CancelError)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
461
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
462 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
463 """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
464 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
465 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
466 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
467 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
468
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
469 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
470 """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
471
968
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
472 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
473 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
474 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
475 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
476 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
477 @param ret(None, unicode, iterable): on_data_post return value
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
478 see LiberviaPage.__init__ on_data_post docstring
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
479 """
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
480 if ret is None:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
481 ret = ()
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
482 elif isinstance(ret, basestring):
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
483 ret = (ret,)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
484 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
485 ret = tuple(ret)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
486 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
487 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
488 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
489 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
490 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
491 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
492 redirect_page = redirect_page_data[0]
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
493 redirect_page_args = redirect_page_data[1:]
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
494 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
495 else:
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
496 redirect_page = redirect_page_data
972
c4e58c4dba75 server: getURL + minor improvments:
Goffi <goffi@goffi.org>
parents: 968
diff changeset
497 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
498 else:
4d37b23777c3 pages (core, tickets/new): replaced post_redirect_uri mechanism by post_redirect_page:
Goffi <goffi@goffi.org>
parents: 966
diff changeset
499 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
500 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
501
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
502 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
503 session_data.setPageFlag(redirect_page, C.FLAG_CONFIRM)
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
504 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
505 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
506 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
507 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
508
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
509 def _on_data_post(self, dummy, request):
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
510 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
511 try:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
512 given_csrf = self.getPostedData(request, u'csrf_token')
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
513 except KeyError:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
514 given_csrf = None
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
515 if given_csrf is None or given_csrf != csrf_token:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
516 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
517 url=request.uri,
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
518 ip=request.getClientIP()))
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
519 self.pageError(request, C.HTTP_UNAUTHORIZED)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
520 d = defer.maybeDeferred(self.on_data_post, self, request)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
521 d.addCallback(self._on_data_post_redirect, request)
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
522 return d
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
523
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
524 def getPostedData(self, request, keys, multiple=False):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
525 """get data from a POST request and decode it
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
526
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
527 @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
528 @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
529 unicode to get one value
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
530 iterable to get more than one
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
531 @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
532 if False, the first value is returned
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
533 @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
534 @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
535 """
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
536 if isinstance(keys, basestring):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
537 keys = [keys]
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
538 get_first = True
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
539 else:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
540 get_first = False
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
541
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
542 ret = []
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
543 for key in keys:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
544 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
545 if multiple:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
546 ret.append(gen)
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
547 else:
956
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
548 try:
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
549 ret.append(next(gen))
dabecab10faa server (pages): impleted CSRF protection:
Goffi <goffi@goffi.org>
parents: 955
diff changeset
550 except StopIteration:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
551 raise KeyError(key)
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
552
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
553 return ret[0] if get_first else ret
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
554
959
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
555 def getAllPostedData(self, request, except_=()):
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
556 """get all posted data
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
557
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
558 @param request(server.Request): request linked to the session
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
559 @param except_(iterable[unicode]): key of values to ignore
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
560 csrf_token will always be ignored
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
561 @return (dict[unicode, list[unicode]]): post values
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
562 """
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
563 except_ = tuple(except_) + (u'csrf_token',)
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
564 ret = {}
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
565 for key, values in request.args.iteritems():
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
566 key = urllib.unquote(key).decode('utf-8')
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
567 if key in except_:
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
568 continue
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
569 ret[key] = [urllib.unquote(v).decode('utf-8') for v in values]
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
570 return ret
968eda9e982a server: added getAllPostedData
Goffi <goffi@goffi.org>
parents: 957
diff changeset
571
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
572 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
573 """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
574
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
575 @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
576 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
577 """
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
578 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
579 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
580
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
581 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
582 """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
583
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
584 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
585 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
586 @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
587 """
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
588 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
589 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
590 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
591 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
592 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
593
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
594 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
595 """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
596
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
597 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
598 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
599 """
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
600 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
601 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
602 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
603 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
604 if not profile:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
605 # no session started
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
606 if not self.host.options["allow_registration"]:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
607 # registration not allowed, access is not granted
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
608 self.pageError(request, C.HTTP_UNAUTHORIZED)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
609 else:
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
610 # registration allowed, we redirect to login page
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
611 login_url = self.getPageRedirectURL(request)
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
612 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
613
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
614 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
615
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
616 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
617 """Main method to handle the workflow of a LiberviaPage"""
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
618 # template_data are the variables passed to template
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
619 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
620 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
621 csrf_token = session_data.csrf_token
67bf14c91d5c server (pages): added a confirm flag on successful post:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
622 request.template_data = {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
623
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
624 # 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
625 # at the beginning of the request hanling
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
626 if request.postpath and not request.postpath[-1]:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
627 # we don't differenciate URLs finishing with '/' or not
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
628 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
629
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
630 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
631 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
632
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
633 if self.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
634 self.pageRedirect(self.redirect, request, skip_parse_url=False)
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
635
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
636 if self.parse_url is not None and not skip_parse_url:
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
637 d.addCallback(self.parse_url, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
638
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
639 d.addCallback(self._subpagesHandler, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
640
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
641 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
642 # 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
643 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
644
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
645 if request.method == C.HTTP_METHOD_POST:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
646 if self.on_data_post is None:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
647 # 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
648 # so we return an error
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
649 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
650 else:
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
651 d.addCallback(self._on_data_post, request)
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
652 # 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
653 # 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
654
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
655 if self.prepare_render:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
656 d.addCallback(self._prepare_render, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
657
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
658 if self.template:
962
c7fba7709d05 Pages: various improvments:
Goffi <goffi@goffi.org>
parents: 961
diff changeset
659 d.addCallback(self._render_template, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
660 elif self.render_method:
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
661 d.addCallback(self._render_method, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
662
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
663 d.addCallback(self.writeData, request)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
664 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
665 d.addErrback(self._internalError, request)
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
666 d.callback(self)
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
667 return server.NOT_DONE_YET
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
668
923
edb322c87ea4 server (pages): pages now handle redirection, check self.pageRedirect docstring for details
Goffi <goffi@goffi.org>
parents: 922
diff changeset
669 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
670 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
671
931
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
672 def render_POST(self, request):
8a393ae90f8c server (pages): post requests are now handled:
Goffi <goffi@goffi.org>
parents: 927
diff changeset
673 return self.renderPage(request)