author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:15:52 +0200 | |
branch | oldstable |
changeset 5424 | 8ecbcbff9777 |
parent 5421 | 8167de96c523 |
child 5426 | 0d4853a6e5ee |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""abstract class for http request |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import Cookie |
|
24 |
import sha |
|
25 |
import time |
|
26 |
import random |
|
27 |
import base64 |
|
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
28 |
from datetime import date |
0 | 29 |
from urlparse import urlsplit |
30 |
from itertools import count |
|
31 |
||
32 |
from rql.utils import rqlvar_maker |
|
33 |
||
34 |
from logilab.common.decorators import cached |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2559
diff
changeset
|
35 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2258
diff
changeset
|
36 |
from logilab.mtconverter import xml_escape |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
37 |
|
0 | 38 |
from cubicweb.dbapi import DBAPIRequest |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4004
diff
changeset
|
39 |
from cubicweb.mail import header |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4004
diff
changeset
|
40 |
from cubicweb.uilib import remove_html_tags |
3816
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
41 |
from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid |
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
42 |
from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
43 |
from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
44 |
RequestError, StatusResponse, json) |
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
45 |
dumps = json.dumps |
0 | 46 |
|
662
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
47 |
_MARKER = object() |
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
48 |
|
0 | 49 |
|
50 |
def list_form_param(form, param, pop=False): |
|
51 |
"""get param from form parameters and return its value as a list, |
|
52 |
skipping internal markers if any |
|
53 |
||
54 |
* if the parameter isn't defined, return an empty list |
|
55 |
* if the parameter is a single (unicode) value, return a list |
|
56 |
containing that value |
|
57 |
* if the parameter is already a list or tuple, just skip internal |
|
58 |
markers |
|
59 |
||
60 |
if pop is True, the parameter is removed from the form dictionnary |
|
61 |
""" |
|
62 |
if pop: |
|
63 |
try: |
|
64 |
value = form.pop(param) |
|
65 |
except KeyError: |
|
66 |
return [] |
|
67 |
else: |
|
68 |
value = form.get(param, ()) |
|
69 |
if value is None: |
|
70 |
value = () |
|
71 |
elif not isinstance(value, (list, tuple)): |
|
72 |
value = [value] |
|
73 |
return [v for v in value if v != INTERNAL_FIELD_VALUE] |
|
74 |
||
75 |
||
76 |
||
77 |
class CubicWebRequestBase(DBAPIRequest): |
|
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
78 |
"""abstract HTTP request, should be extended according to the HTTP backend""" |
2255
c346af0727ca
more generic way to detect json requests (not yet perfect though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
79 |
json_request = False # to be set to True by json controllers |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
80 |
|
0 | 81 |
def __init__(self, vreg, https, form=None): |
82 |
super(CubicWebRequestBase, self).__init__(vreg) |
|
83 |
self.authmode = vreg.config['auth-mode'] |
|
84 |
self.https = https |
|
85 |
# raw html headers that can be added from any view |
|
86 |
self.html_headers = HTMLHead() |
|
87 |
# form parameters |
|
88 |
self.setup_params(form) |
|
89 |
# dictionnary that may be used to store request data that has to be |
|
90 |
# shared among various components used to publish the request (views, |
|
91 |
# controller, application...) |
|
92 |
self.data = {} |
|
93 |
# search state: 'normal' or 'linksearch' (eg searching for an object |
|
94 |
# to create a relation with another) |
|
1426 | 95 |
self.search_state = ('normal',) |
0 | 96 |
# tabindex generator |
2556 | 97 |
self.tabindexgen = count(1) |
0 | 98 |
self.next_tabindex = self.tabindexgen.next |
99 |
# page id, set by htmlheader template |
|
100 |
self.pageid = None |
|
101 |
self.datadir_url = self._datadir_url() |
|
3816
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
102 |
self._set_pageid() |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
103 |
|
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
104 |
def _set_pageid(self): |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
105 |
"""initialize self.pageid |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
106 |
if req.form provides a specific pageid, use it, otherwise build a |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
107 |
new one. |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
108 |
""" |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
109 |
pid = self.form.get('pageid') |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
110 |
if pid is None: |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
111 |
pid = make_uid(id(self)) |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
112 |
self.pageid = pid |
3838
9cc134372bf8
[web] safety belt to avoid overriding pageid with loadxhtml()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3816
diff
changeset
|
113 |
self.html_headers.define_var('pageid', pid, override=False) |
0 | 114 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
115 |
@property |
2801
7ef4c1c9266b
fix syntax error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
116 |
def varmaker(self): |
4366
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
117 |
"""the rql varmaker is exposed both as a property and as the |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
118 |
set_varmaker function since we've two use cases: |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
119 |
|
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
120 |
* accessing the req.varmaker property to get a new variable name |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
121 |
|
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
122 |
* calling req.set_varmaker() to ensure a varmaker is set for later ajax |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
123 |
calls sharing our .pageid |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
124 |
""" |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
125 |
return self.set_varmaker() |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
126 |
|
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
127 |
def set_varmaker(self): |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
128 |
varmaker = self.get_page_data('rql_varmaker') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
129 |
if varmaker is None: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
130 |
varmaker = rqlvar_maker() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
131 |
self.set_page_data('rql_varmaker', varmaker) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
132 |
return varmaker |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
133 |
|
0 | 134 |
def set_connection(self, cnx, user=None): |
135 |
"""method called by the session handler when the user is authenticated |
|
136 |
or an anonymous connection is open |
|
137 |
""" |
|
138 |
super(CubicWebRequestBase, self).set_connection(cnx, user) |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
139 |
# set request language |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
140 |
vreg = self.vreg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
141 |
if self.user: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
142 |
try: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
143 |
# 1. user specified language |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
144 |
lang = vreg.typed_value('ui.language', |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
145 |
self.user.properties['ui.language']) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
146 |
self.set_language(lang) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
147 |
return |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
148 |
except KeyError: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
149 |
pass |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
150 |
if vreg.config['language-negociation']: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
151 |
# 2. http negociated language |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
152 |
for lang in self.header_accept_language(): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
153 |
if lang in self.translations: |
0 | 154 |
self.set_language(lang) |
155 |
return |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
156 |
# 3. default language |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
157 |
self.set_default_language(vreg) |
1426 | 158 |
|
0 | 159 |
def set_language(self, lang): |
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3094
diff
changeset
|
160 |
gettext, self.pgettext = self.translations[lang] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3094
diff
changeset
|
161 |
self._ = self.__ = gettext |
0 | 162 |
self.lang = lang |
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
163 |
self.cnx.set_session_props(lang=lang) |
0 | 164 |
self.debug('request language: %s', lang) |
1426 | 165 |
|
0 | 166 |
# input form parameters management ######################################## |
1426 | 167 |
|
0 | 168 |
# common form parameters which should be protected against html values |
169 |
# XXX can't add 'eid' for instance since it may be multivalued |
|
170 |
# dont put rql as well, if query contains < and > it will be corrupted! |
|
1426 | 171 |
no_script_form_params = set(('vid', |
172 |
'etype', |
|
0 | 173 |
'vtitle', 'title', |
174 |
'__message', |
|
175 |
'__redirectvid', '__redirectrql')) |
|
1426 | 176 |
|
0 | 177 |
def setup_params(self, params): |
178 |
"""WARNING: we're intentionaly leaving INTERNAL_FIELD_VALUE here |
|
179 |
||
1426 | 180 |
subclasses should overrides to |
0 | 181 |
""" |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
182 |
self.form = {} |
0 | 183 |
if params is None: |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
184 |
return |
0 | 185 |
encoding = self.encoding |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
186 |
for param, val in params.iteritems(): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
187 |
if isinstance(val, (tuple, list)): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
188 |
val = [unicode(x, encoding) for x in val] |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
189 |
if len(val) == 1: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
190 |
val = val[0] |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
191 |
elif isinstance(val, str): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
192 |
val = unicode(val, encoding) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
193 |
if param in self.no_script_form_params and val: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
194 |
val = self.no_script_form_param(param, val) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
195 |
if param == '_cwmsgid': |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
196 |
self.set_message_id(val) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
197 |
elif param == '__message': |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
198 |
self.set_message(val) |
0 | 199 |
else: |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
200 |
self.form[param] = val |
1426 | 201 |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
202 |
def no_script_form_param(self, param, value): |
0 | 203 |
"""ensure there is no script in a user form param |
204 |
||
205 |
by default return a cleaned string instead of raising a security |
|
206 |
exception |
|
207 |
||
208 |
this method should be called on every user input (form at least) fields |
|
209 |
that are at some point inserted in a generated html page to protect |
|
210 |
against script kiddies |
|
211 |
""" |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
212 |
# safety belt for strange urls like http://...?vtitle=yo&vtitle=yo |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
213 |
if isinstance(value, (list, tuple)): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
214 |
self.error('no_script_form_param got a list (%s). Who generated the URL ?', |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
215 |
repr(value)) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
216 |
value = value[0] |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
217 |
return remove_html_tags(value) |
1426 | 218 |
|
0 | 219 |
def list_form_param(self, param, form=None, pop=False): |
220 |
"""get param from form parameters and return its value as a list, |
|
221 |
skipping internal markers if any |
|
1426 | 222 |
|
0 | 223 |
* if the parameter isn't defined, return an empty list |
224 |
* if the parameter is a single (unicode) value, return a list |
|
225 |
containing that value |
|
226 |
* if the parameter is already a list or tuple, just skip internal |
|
227 |
markers |
|
228 |
||
229 |
if pop is True, the parameter is removed from the form dictionnary |
|
230 |
""" |
|
231 |
if form is None: |
|
232 |
form = self.form |
|
1426 | 233 |
return list_form_param(form, param, pop) |
234 |
||
0 | 235 |
|
236 |
def reset_headers(self): |
|
237 |
"""used by AutomaticWebTest to clear html headers between tests on |
|
238 |
the same resultset |
|
239 |
""" |
|
240 |
self.html_headers = HTMLHead() |
|
241 |
return self |
|
242 |
||
243 |
# web state helpers ####################################################### |
|
1426 | 244 |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
245 |
@property |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
246 |
def message(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
247 |
try: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
248 |
return self.get_session_data(self._msgid, default=u'', pop=True) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
249 |
except AttributeError: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
250 |
try: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
251 |
return self._msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
252 |
except AttributeError: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
253 |
return None |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
254 |
|
0 | 255 |
def set_message(self, msg): |
256 |
assert isinstance(msg, unicode) |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
257 |
self._msg = msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
258 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
259 |
def set_message_id(self, msgid): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
260 |
self._msgid = msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
261 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
262 |
@cached |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
263 |
def redirect_message_id(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
264 |
return make_uid() |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
265 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
266 |
def set_redirect_message(self, msg): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
267 |
assert isinstance(msg, unicode) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
268 |
msgid = self.redirect_message_id() |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
269 |
self.set_session_data(msgid, msg) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
270 |
return msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
271 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
272 |
def append_to_redirect_message(self, msg): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
273 |
msgid = self.redirect_message_id() |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
274 |
currentmsg = self.get_session_data(msgid) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
275 |
if currentmsg is not None: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
276 |
currentmsg = '%s %s' % (currentmsg, msg) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
277 |
else: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
278 |
currentmsg = msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
279 |
self.set_session_data(msgid, currentmsg) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
280 |
return msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
281 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
282 |
def reset_message(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
283 |
if hasattr(self, '_msg'): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
284 |
del self._msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
285 |
if hasattr(self, '_msgid'): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
286 |
del self._msgid |
1426 | 287 |
|
0 | 288 |
def update_search_state(self): |
289 |
"""update the current search state""" |
|
290 |
searchstate = self.form.get('__mode') |
|
610
30cb5e29a416
take care, cnx may be None in which case we can't get/set session data
sylvain.thenault@logilab.fr
parents:
495
diff
changeset
|
291 |
if not searchstate and self.cnx is not None: |
0 | 292 |
searchstate = self.get_session_data('search_state', 'normal') |
293 |
self.set_search_state(searchstate) |
|
294 |
||
295 |
def set_search_state(self, searchstate): |
|
296 |
"""set a new search state""" |
|
297 |
if searchstate is None or searchstate == 'normal': |
|
298 |
self.search_state = (searchstate or 'normal',) |
|
299 |
else: |
|
300 |
self.search_state = ('linksearch', searchstate.split(':')) |
|
301 |
assert len(self.search_state[-1]) == 4 |
|
610
30cb5e29a416
take care, cnx may be None in which case we can't get/set session data
sylvain.thenault@logilab.fr
parents:
495
diff
changeset
|
302 |
if self.cnx is not None: |
30cb5e29a416
take care, cnx may be None in which case we can't get/set session data
sylvain.thenault@logilab.fr
parents:
495
diff
changeset
|
303 |
self.set_session_data('search_state', searchstate) |
0 | 304 |
|
1173
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
305 |
def match_search_state(self, rset): |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
306 |
"""when searching an entity to create a relation, return True if entities in |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
307 |
the given rset may be used as relation end |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
308 |
""" |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
309 |
try: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
310 |
searchedtype = self.search_state[1][-1] |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
311 |
except IndexError: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
312 |
return False # no searching for association |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
313 |
for etype in rset.column_types(0): |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
314 |
if etype != searchedtype: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
315 |
return False |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
316 |
return True |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
317 |
|
0 | 318 |
def update_breadcrumbs(self): |
319 |
"""stores the last visisted page in session data""" |
|
320 |
searchstate = self.get_session_data('search_state') |
|
321 |
if searchstate == 'normal': |
|
322 |
breadcrumbs = self.get_session_data('breadcrumbs', None) |
|
323 |
if breadcrumbs is None: |
|
324 |
breadcrumbs = SizeConstrainedList(10) |
|
325 |
self.set_session_data('breadcrumbs', breadcrumbs) |
|
4864
e77d3e95872d
[web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
326 |
breadcrumbs.append(self.url()) |
e77d3e95872d
[web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
327 |
else: |
e77d3e95872d
[web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
328 |
url = self.url() |
4974
025a491bad0c
take care to empty breadcrumbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4868
diff
changeset
|
329 |
if breadcrumbs and breadcrumbs[-1] != url: |
4864
e77d3e95872d
[web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
330 |
breadcrumbs.append(url) |
0 | 331 |
|
332 |
def last_visited_page(self): |
|
333 |
breadcrumbs = self.get_session_data('breadcrumbs', None) |
|
334 |
if breadcrumbs: |
|
335 |
return breadcrumbs.pop() |
|
336 |
return self.base_url() |
|
337 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
338 |
def user_rql_callback(self, args, msg=None): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
339 |
"""register a user callback to execute some rql query and return an url |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
340 |
to call it ready to be inserted in html |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
341 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
342 |
def rqlexec(req, rql, args=None, key=None): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
343 |
req.execute(rql, args, key) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
344 |
return self.user_callback(rqlexec, args, msg) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
345 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
346 |
def user_callback(self, cb, args, msg=None, nonify=False): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
347 |
"""register the given user callback and return an url to call it ready to be |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
348 |
inserted in html |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
349 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
350 |
self.add_js('cubicweb.ajax.js') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
351 |
cbname = self.register_onetime_callback(cb, *args) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
352 |
msg = dumps(msg or '') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
353 |
return "javascript:userCallbackThenReloadPage('%s', %s)" % ( |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
354 |
cbname, msg) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
355 |
|
0 | 356 |
def register_onetime_callback(self, func, *args): |
357 |
cbname = 'cb_%s' % ( |
|
358 |
sha.sha('%s%s%s%s' % (time.time(), func.__name__, |
|
1426 | 359 |
random.random(), |
0 | 360 |
self.user.login)).hexdigest()) |
361 |
def _cb(req): |
|
362 |
try: |
|
363 |
ret = func(req, *args) |
|
364 |
except TypeError: |
|
365 |
from warnings import warn |
|
366 |
warn('user callback should now take request as argument') |
|
1426 | 367 |
ret = func(*args) |
0 | 368 |
self.unregister_callback(self.pageid, cbname) |
369 |
return ret |
|
370 |
self.set_page_data(cbname, _cb) |
|
371 |
return cbname |
|
1426 | 372 |
|
0 | 373 |
def unregister_callback(self, pageid, cbname): |
374 |
assert pageid is not None |
|
375 |
assert cbname.startswith('cb_') |
|
376 |
self.info('unregistering callback %s for pageid %s', cbname, pageid) |
|
377 |
self.del_page_data(cbname) |
|
378 |
||
379 |
def clear_user_callbacks(self): |
|
380 |
if self.cnx is not None: |
|
381 |
sessdata = self.session_data() |
|
382 |
callbacks = [key for key in sessdata if key.startswith('cb_')] |
|
383 |
for callback in callbacks: |
|
384 |
self.del_session_data(callback) |
|
1426 | 385 |
|
0 | 386 |
# web edition helpers ##################################################### |
1426 | 387 |
|
0 | 388 |
@cached # so it's writed only once |
389 |
def fckeditor_config(self): |
|
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
662
diff
changeset
|
390 |
self.add_js('fckeditor/fckeditor.js') |
0 | 391 |
self.html_headers.define_var('fcklang', self.lang) |
392 |
self.html_headers.define_var('fckconfigpath', |
|
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
662
diff
changeset
|
393 |
self.build_url('data/cubicweb.fckcwconfig.js')) |
1013
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
940
diff
changeset
|
394 |
def use_fckeditor(self): |
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
940
diff
changeset
|
395 |
return self.vreg.config.fckeditor_installed() and self.property_value('ui.fckeditor') |
0 | 396 |
|
397 |
def edited_eids(self, withtype=False): |
|
398 |
"""return a list of edited eids""" |
|
399 |
yielded = False |
|
400 |
# warning: use .keys since the caller may change `form` |
|
401 |
form = self.form |
|
402 |
try: |
|
403 |
eids = form['eid'] |
|
404 |
except KeyError: |
|
4155
80cc9c6ddcf0
NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4154
diff
changeset
|
405 |
raise NothingToEdit(self._('no selected entities')) |
0 | 406 |
if isinstance(eids, basestring): |
407 |
eids = (eids,) |
|
408 |
for peid in eids: |
|
409 |
if withtype: |
|
410 |
typekey = '__type:%s' % peid |
|
411 |
assert typekey in form, 'no entity type specified' |
|
412 |
yield peid, form[typekey] |
|
413 |
else: |
|
414 |
yield peid |
|
415 |
yielded = True |
|
416 |
if not yielded: |
|
4155
80cc9c6ddcf0
NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4154
diff
changeset
|
417 |
raise NothingToEdit(self._('no selected entities')) |
0 | 418 |
|
419 |
# minparams=3 by default: at least eid, __type, and some params to change |
|
420 |
def extract_entity_params(self, eid, minparams=3): |
|
421 |
"""extract form parameters relative to the given eid""" |
|
422 |
params = {} |
|
423 |
eid = str(eid) |
|
424 |
form = self.form |
|
425 |
for param in form: |
|
426 |
try: |
|
427 |
name, peid = param.split(':', 1) |
|
428 |
except ValueError: |
|
429 |
if not param.startswith('__') and param != "eid": |
|
430 |
self.warning('param %s mis-formatted', param) |
|
431 |
continue |
|
432 |
if peid == eid: |
|
433 |
value = form[param] |
|
434 |
if value == INTERNAL_FIELD_VALUE: |
|
435 |
value = None |
|
436 |
params[name] = value |
|
437 |
params['eid'] = eid |
|
438 |
if len(params) < minparams: |
|
439 |
raise RequestError(self._('missing parameters for entity %s') % eid) |
|
440 |
return params |
|
1426 | 441 |
|
4277
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
442 |
# XXX this should go to the GenericRelationsField. missing edition cancel protocol. |
0 | 443 |
|
444 |
def remove_pending_operations(self): |
|
445 |
"""shortcut to clear req's pending_{delete,insert} entries |
|
446 |
||
447 |
This is needed when the edition is completed (whether it's validated |
|
448 |
or cancelled) |
|
449 |
""" |
|
450 |
self.del_session_data('pending_insert') |
|
451 |
self.del_session_data('pending_delete') |
|
452 |
||
453 |
def cancel_edition(self, errorurl): |
|
454 |
"""remove pending operations and `errorurl`'s specific stored data |
|
455 |
""" |
|
456 |
self.del_session_data(errorurl) |
|
457 |
self.remove_pending_operations() |
|
1426 | 458 |
|
0 | 459 |
# high level methods for HTTP headers management ########################## |
460 |
||
461 |
# must be cached since login/password are popped from the form dictionary |
|
462 |
# and this method may be called multiple times during authentication |
|
463 |
@cached |
|
464 |
def get_authorization(self): |
|
465 |
"""Parse and return the Authorization header""" |
|
466 |
if self.authmode == "cookie": |
|
467 |
try: |
|
468 |
user = self.form.pop("__login") |
|
469 |
passwd = self.form.pop("__password", '') |
|
470 |
return user, passwd.encode('UTF8') |
|
471 |
except KeyError: |
|
472 |
self.debug('no login/password in form params') |
|
473 |
return None, None |
|
474 |
else: |
|
475 |
return self.header_authorization() |
|
1426 | 476 |
|
0 | 477 |
def get_cookie(self): |
478 |
"""retrieve request cookies, returns an empty cookie if not found""" |
|
479 |
try: |
|
480 |
return Cookie.SimpleCookie(self.get_header('Cookie')) |
|
481 |
except KeyError: |
|
482 |
return Cookie.SimpleCookie() |
|
483 |
||
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
484 |
def set_cookie(self, cookie, key, maxage=300, expires=None): |
0 | 485 |
"""set / update a cookie key |
486 |
||
487 |
by default, cookie will be available for the next 5 minutes. |
|
488 |
Give maxage = None to have a "session" cookie expiring when the |
|
489 |
client close its browser |
|
490 |
""" |
|
491 |
morsel = cookie[key] |
|
492 |
if maxage is not None: |
|
493 |
morsel['Max-Age'] = maxage |
|
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
494 |
if expires: |
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
495 |
morsel['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S %z') |
0 | 496 |
# make sure cookie is set on the correct path |
497 |
morsel['path'] = self.base_url_path() |
|
498 |
self.add_header('Set-Cookie', morsel.OutputString()) |
|
499 |
||
500 |
def remove_cookie(self, cookie, key): |
|
501 |
"""remove a cookie by expiring it""" |
|
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
502 |
self.set_cookie(cookie, key, maxage=0, expires=date(1970, 1, 1)) |
0 | 503 |
|
504 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
505 |
"""set output content type for this request. An optional filename |
|
506 |
may be given |
|
507 |
""" |
|
508 |
if content_type.startswith('text/'): |
|
509 |
content_type += ';charset=' + (encoding or self.encoding) |
|
510 |
self.set_header('content-type', content_type) |
|
511 |
if filename: |
|
512 |
if isinstance(filename, unicode): |
|
513 |
filename = header(filename).encode() |
|
514 |
self.set_header('content-disposition', 'inline; filename=%s' |
|
515 |
% filename) |
|
516 |
||
517 |
# high level methods for HTML headers management ########################## |
|
518 |
||
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2255
diff
changeset
|
519 |
def add_onload(self, jscode): |
4851
e55bdd10421e
remove deprecation warning introduced by add_onload api change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
520 |
self.html_headers.add_onload(jscode) |
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2255
diff
changeset
|
521 |
|
0 | 522 |
def add_js(self, jsfiles, localfile=True): |
523 |
"""specify a list of JS files to include in the HTML headers |
|
524 |
:param jsfiles: a JS filename or a list of JS filenames |
|
525 |
:param localfile: if True, the default data dir prefix is added to the |
|
526 |
JS filename |
|
527 |
""" |
|
528 |
if isinstance(jsfiles, basestring): |
|
529 |
jsfiles = (jsfiles,) |
|
530 |
for jsfile in jsfiles: |
|
531 |
if localfile: |
|
532 |
jsfile = self.datadir_url + jsfile |
|
533 |
self.html_headers.add_js(jsfile) |
|
534 |
||
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
535 |
def add_css(self, cssfiles, media=u'all', localfile=True, ieonly=False, |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
536 |
iespec=u'[if lt IE 8]'): |
0 | 537 |
"""specify a CSS file to include in the HTML headers |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
538 |
|
0 | 539 |
:param cssfiles: a CSS filename or a list of CSS filenames |
540 |
:param media: the CSS's media if necessary |
|
541 |
:param localfile: if True, the default data dir prefix is added to the |
|
542 |
CSS filename |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
543 |
:param ieonly: True if this css is specific to IE |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
544 |
:param iespec: conditional expression that will be used around |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
545 |
the css inclusion. cf: |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
546 |
http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx |
0 | 547 |
""" |
548 |
if isinstance(cssfiles, basestring): |
|
549 |
cssfiles = (cssfiles,) |
|
550 |
if ieonly: |
|
551 |
if self.ie_browser(): |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
552 |
extraargs = [iespec] |
0 | 553 |
add_css = self.html_headers.add_ie_css |
554 |
else: |
|
555 |
return # no need to do anything on non IE browsers |
|
556 |
else: |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
557 |
extraargs = [] |
0 | 558 |
add_css = self.html_headers.add_css |
559 |
for cssfile in cssfiles: |
|
560 |
if localfile: |
|
561 |
cssfile = self.datadir_url + cssfile |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
562 |
add_css(cssfile, media, *extraargs) |
1426 | 563 |
|
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
564 |
def build_ajax_replace_url(self, nodeid, rql, vid, replacemode='replace', |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
565 |
**extraparams): |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
566 |
"""builds an ajax url that will replace nodeid's content |
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
567 |
|
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
568 |
:param nodeid: the dom id of the node to replace |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
569 |
:param rql: rql to execute |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
570 |
:param vid: the view to apply on the resultset |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
571 |
:param replacemode: defines how the replacement should be done. |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
572 |
|
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
573 |
Possible values are : |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
574 |
- 'replace' to replace the node's content with the generated HTML |
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
575 |
- 'swap' to replace the node itself with the generated HTML |
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
576 |
- 'append' to append the generated HTML to the node's content |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
577 |
""" |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
578 |
url = self.build_url('view', rql=rql, vid=vid, __notemplate=1, |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
579 |
**extraparams) |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
580 |
return "javascript: loadxhtml('%s', '%s', '%s')" % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2258
diff
changeset
|
581 |
nodeid, xml_escape(url), replacemode) |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
582 |
|
0 | 583 |
# urls/path management #################################################### |
1426 | 584 |
|
0 | 585 |
def url(self, includeparams=True): |
586 |
"""return currently accessed url""" |
|
587 |
return self.base_url() + self.relative_path(includeparams) |
|
588 |
||
589 |
def _datadir_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2315
diff
changeset
|
590 |
"""return url of the instance's data directory""" |
0 | 591 |
return self.base_url() + 'data%s/' % self.vreg.config.instance_md5_version() |
1426 | 592 |
|
0 | 593 |
def selected(self, url): |
594 |
"""return True if the url is equivalent to currently accessed url""" |
|
595 |
reqpath = self.relative_path().lower() |
|
596 |
baselen = len(self.base_url()) |
|
597 |
return (reqpath == url[baselen:].lower()) |
|
598 |
||
599 |
def base_url_prepend_host(self, hostname): |
|
600 |
protocol, roothost = urlsplit(self.base_url())[:2] |
|
601 |
if roothost.startswith('www.'): |
|
602 |
roothost = roothost[4:] |
|
603 |
return '%s://%s.%s' % (protocol, hostname, roothost) |
|
604 |
||
605 |
def base_url_path(self): |
|
606 |
"""returns the absolute path of the base url""" |
|
607 |
return urlsplit(self.base_url())[2] |
|
1426 | 608 |
|
0 | 609 |
@cached |
610 |
def from_controller(self): |
|
611 |
"""return the id (string) of the controller issuing the request""" |
|
612 |
controller = self.relative_path(False).split('/', 1)[0] |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
613 |
registered_controllers = self.vreg['controllers'].keys() |
0 | 614 |
if controller in registered_controllers: |
615 |
return controller |
|
616 |
return 'view' |
|
1426 | 617 |
|
0 | 618 |
def external_resource(self, rid, default=_MARKER): |
619 |
"""return a path to an external resource, using its identifier |
|
620 |
||
621 |
raise KeyError if the resource is not defined |
|
622 |
""" |
|
623 |
try: |
|
624 |
value = self.vreg.config.ext_resources[rid] |
|
625 |
except KeyError: |
|
626 |
if default is _MARKER: |
|
627 |
raise |
|
628 |
return default |
|
629 |
if value is None: |
|
630 |
return None |
|
631 |
baseurl = self.datadir_url[:-1] # remove trailing / |
|
632 |
if isinstance(value, list): |
|
633 |
return [v.replace('DATADIR', baseurl) for v in value] |
|
634 |
return value.replace('DATADIR', baseurl) |
|
635 |
external_resource = cached(external_resource, keyarg=1) |
|
636 |
||
637 |
def validate_cache(self): |
|
638 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
639 |
exists and is still usable. |
|
640 |
||
641 |
calls the client-dependant implementation of `_validate_cache` |
|
642 |
""" |
|
643 |
self._validate_cache() |
|
644 |
if self.http_method() == 'HEAD': |
|
645 |
raise StatusResponse(200, '') |
|
1426 | 646 |
|
0 | 647 |
# abstract methods to override according to the web front-end ############# |
1426 | 648 |
|
0 | 649 |
def http_method(self): |
650 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
651 |
raise NotImplementedError() |
|
652 |
||
653 |
def _validate_cache(self): |
|
654 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
655 |
exists and is still usable |
|
656 |
""" |
|
657 |
raise NotImplementedError() |
|
1426 | 658 |
|
0 | 659 |
def relative_path(self, includeparams=True): |
660 |
"""return the normalized path of the request (ie at least relative |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2315
diff
changeset
|
661 |
to the instance's root, but some other normalization may be needed |
0 | 662 |
so that the returned path may be used to compare to generated urls |
663 |
||
664 |
:param includeparams: |
|
665 |
boolean indicating if GET form parameters should be kept in the path |
|
666 |
""" |
|
667 |
raise NotImplementedError() |
|
668 |
||
669 |
def get_header(self, header, default=None): |
|
670 |
"""return the value associated with the given input HTTP header, |
|
671 |
raise KeyError if the header is not set |
|
672 |
""" |
|
673 |
raise NotImplementedError() |
|
674 |
||
675 |
def set_header(self, header, value): |
|
676 |
"""set an output HTTP header""" |
|
677 |
raise NotImplementedError() |
|
678 |
||
679 |
def add_header(self, header, value): |
|
680 |
"""add an output HTTP header""" |
|
681 |
raise NotImplementedError() |
|
1426 | 682 |
|
0 | 683 |
def remove_header(self, header): |
684 |
"""remove an output HTTP header""" |
|
685 |
raise NotImplementedError() |
|
1426 | 686 |
|
0 | 687 |
def header_authorization(self): |
688 |
"""returns a couple (auth-type, auth-value)""" |
|
689 |
auth = self.get_header("Authorization", None) |
|
690 |
if auth: |
|
691 |
scheme, rest = auth.split(' ', 1) |
|
692 |
scheme = scheme.lower() |
|
693 |
try: |
|
694 |
assert scheme == "basic" |
|
695 |
user, passwd = base64.decodestring(rest).split(":", 1) |
|
696 |
# XXX HTTP header encoding: use email.Header? |
|
697 |
return user.decode('UTF8'), passwd |
|
698 |
except Exception, ex: |
|
699 |
self.debug('bad authorization %s (%s: %s)', |
|
700 |
auth, ex.__class__.__name__, ex) |
|
701 |
return None, None |
|
702 |
||
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
703 |
@deprecated("[3.4] use parse_accept_header('Accept-Language')") |
0 | 704 |
def header_accept_language(self): |
705 |
"""returns an ordered list of preferred languages""" |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
706 |
return [value.split('-')[0] for value in |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
707 |
self.parse_accept_header('Accept-Language')] |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
708 |
|
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
709 |
def parse_accept_header(self, header): |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
710 |
"""returns an ordered list of preferred languages""" |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
711 |
accepteds = self.get_header(header, '') |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
712 |
values = [] |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
713 |
for info in accepteds.split(','): |
0 | 714 |
try: |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
715 |
value, scores = info.split(';', 1) |
0 | 716 |
except ValueError: |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
717 |
value = info |
0 | 718 |
score = 1.0 |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
719 |
else: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
720 |
for score in scores.split(';'): |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
721 |
try: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
722 |
scorekey, scoreval = score.split('=') |
1717
d2c4d3bd0602
correct wrong condition and missing import
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
723 |
if scorekey == 'q': # XXX 'level' |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4529
diff
changeset
|
724 |
score = float(scoreval) |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
725 |
except ValueError: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
726 |
continue |
1718
26ff2d292183
correct the values list append
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1717
diff
changeset
|
727 |
values.append((score, value)) |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
728 |
values.sort(reverse=True) |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
729 |
return (value for (score, value) in values) |
0 | 730 |
|
731 |
def header_if_modified_since(self): |
|
732 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
733 |
mx date time value (GMT), else return None |
|
734 |
""" |
|
735 |
raise NotImplementedError() |
|
1426 | 736 |
|
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
737 |
def demote_to_html(self): |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
738 |
"""helper method to dynamically set request content type to text/html |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
739 |
|
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
740 |
The global doctype and xmldec must also be changed otherwise the browser |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
741 |
will display '<[' at the beginning of the page |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
742 |
""" |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
743 |
self.set_content_type('text/html') |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
744 |
self.main_stream.doctype = TRANSITIONAL_DOCTYPE_NOEXT |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
745 |
self.main_stream.xmldecl = u'' |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
746 |
|
0 | 747 |
# page data management #################################################### |
748 |
||
749 |
def get_page_data(self, key, default=None): |
|
750 |
"""return value associated to `key` in curernt page data""" |
|
751 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
752 |
return page_data.get(key, default) |
|
1426 | 753 |
|
0 | 754 |
def set_page_data(self, key, value): |
755 |
"""set value associated to `key` in current page data""" |
|
756 |
self.html_headers.add_unload_pagedata() |
|
757 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
758 |
page_data[key] = value |
|
759 |
return self.cnx.set_session_data(self.pageid, page_data) |
|
1426 | 760 |
|
0 | 761 |
def del_page_data(self, key=None): |
762 |
"""remove value associated to `key` in current page data |
|
763 |
if `key` is None, all page data will be cleared |
|
764 |
""" |
|
765 |
if key is None: |
|
766 |
self.cnx.del_session_data(self.pageid) |
|
767 |
else: |
|
768 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
769 |
page_data.pop(key, None) |
|
770 |
self.cnx.set_session_data(self.pageid, page_data) |
|
771 |
||
772 |
# user-agent detection #################################################### |
|
773 |
||
774 |
@cached |
|
775 |
def useragent(self): |
|
776 |
return self.get_header('User-Agent', None) |
|
777 |
||
778 |
def ie_browser(self): |
|
779 |
useragent = self.useragent() |
|
780 |
return useragent and 'MSIE' in useragent |
|
1426 | 781 |
|
0 | 782 |
def xhtml_browser(self): |
2558
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
783 |
"""return True if the browser is considered as xhtml compatible. |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
784 |
|
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
785 |
If the instance is configured to always return text/html and not |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
786 |
application/xhtml+xml, this method will always return False, even though |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
787 |
this is semantically different |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
788 |
""" |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
789 |
if self.vreg.config['force-html-content-type']: |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
790 |
return False |
0 | 791 |
useragent = self.useragent() |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
792 |
# * MSIE/Konqueror does not support xml content-type |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
793 |
# * Opera supports xhtml and handles namespaces properly but it breaks |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
794 |
# jQuery.attr() |
495
f8b1edfe9621
[#80966] Opera supports xhtml and handles namespaces properly but it breaks jQuery.attr(), so xhtml_browser return False if the webbrowser is opera
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
795 |
if useragent and ('MSIE' in useragent or 'KHTML' in useragent |
f8b1edfe9621
[#80966] Opera supports xhtml and handles namespaces properly but it breaks jQuery.attr(), so xhtml_browser return False if the webbrowser is opera
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
796 |
or 'Opera' in useragent): |
0 | 797 |
return False |
798 |
return True |
|
799 |
||
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
800 |
def html_content_type(self): |
2558
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
801 |
if self.xhtml_browser(): |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
802 |
return 'application/xhtml+xml' |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
803 |
return 'text/html' |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
804 |
|
2559
46859078c866
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2558
diff
changeset
|
805 |
def document_surrounding_div(self): |
46859078c866
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2558
diff
changeset
|
806 |
if self.xhtml_browser(): |
4454
aba1b563705b
[request] add a note about the encoding mgmt (or lack thereof)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
807 |
return (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE + # XXX encoding ? |
2559
46859078c866
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2558
diff
changeset
|
808 |
u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">') |
46859078c866
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2558
diff
changeset
|
809 |
return u'<div>' |
46859078c866
[R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2558
diff
changeset
|
810 |
|
0 | 811 |
from cubicweb import set_log_methods |
812 |
set_log_methods(CubicWebRequestBase, LOGGER) |