author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 29 Sep 2009 15:09:12 +0200 | |
branch | stable |
changeset 3521 | ad041dae15aa |
parent 3275 | 5247789df541 |
child 3293 | 69c0ba095536 |
child 3816 | 37b376bb4088 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract class for http request |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1801
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1801
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import Cookie |
|
11 |
import sha |
|
12 |
import time |
|
13 |
import random |
|
14 |
import base64 |
|
15 |
from urlparse import urlsplit |
|
16 |
from itertools import count |
|
17 |
||
18 |
from rql.utils import rqlvar_maker |
|
19 |
||
20 |
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
|
21 |
from logilab.common.deprecation import deprecated |
0 | 22 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2258
diff
changeset
|
23 |
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
|
24 |
|
0 | 25 |
from cubicweb.dbapi import DBAPIRequest |
26 |
from cubicweb.common.mail import header |
|
27 |
from cubicweb.common.uilib import remove_html_tags |
|
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
890
diff
changeset
|
28 |
from cubicweb.utils import SizeConstrainedList, HTMLHead |
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
29 |
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
|
30 |
from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, |
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
|
31 |
RequestError, StatusResponse) |
0 | 32 |
|
662
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
33 |
_MARKER = object() |
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
34 |
|
0 | 35 |
|
36 |
def list_form_param(form, param, pop=False): |
|
37 |
"""get param from form parameters and return its value as a list, |
|
38 |
skipping internal markers if any |
|
39 |
||
40 |
* if the parameter isn't defined, return an empty list |
|
41 |
* if the parameter is a single (unicode) value, return a list |
|
42 |
containing that value |
|
43 |
* if the parameter is already a list or tuple, just skip internal |
|
44 |
markers |
|
45 |
||
46 |
if pop is True, the parameter is removed from the form dictionnary |
|
47 |
""" |
|
48 |
if pop: |
|
49 |
try: |
|
50 |
value = form.pop(param) |
|
51 |
except KeyError: |
|
52 |
return [] |
|
53 |
else: |
|
54 |
value = form.get(param, ()) |
|
55 |
if value is None: |
|
56 |
value = () |
|
57 |
elif not isinstance(value, (list, tuple)): |
|
58 |
value = [value] |
|
59 |
return [v for v in value if v != INTERNAL_FIELD_VALUE] |
|
60 |
||
61 |
||
62 |
||
63 |
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
|
64 |
"""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
|
65 |
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
|
66 |
|
0 | 67 |
def __init__(self, vreg, https, form=None): |
68 |
super(CubicWebRequestBase, self).__init__(vreg) |
|
69 |
self.message = None |
|
70 |
self.authmode = vreg.config['auth-mode'] |
|
71 |
self.https = https |
|
72 |
# raw html headers that can be added from any view |
|
73 |
self.html_headers = HTMLHead() |
|
74 |
# form parameters |
|
75 |
self.setup_params(form) |
|
76 |
# dictionnary that may be used to store request data that has to be |
|
77 |
# shared among various components used to publish the request (views, |
|
78 |
# controller, application...) |
|
79 |
self.data = {} |
|
80 |
# search state: 'normal' or 'linksearch' (eg searching for an object |
|
81 |
# to create a relation with another) |
|
1426 | 82 |
self.search_state = ('normal',) |
0 | 83 |
# tabindex generator |
2556 | 84 |
self.tabindexgen = count(1) |
0 | 85 |
self.next_tabindex = self.tabindexgen.next |
86 |
# page id, set by htmlheader template |
|
87 |
self.pageid = None |
|
88 |
self.varmaker = rqlvar_maker() |
|
89 |
self.datadir_url = self._datadir_url() |
|
90 |
||
91 |
def set_connection(self, cnx, user=None): |
|
92 |
"""method called by the session handler when the user is authenticated |
|
93 |
or an anonymous connection is open |
|
94 |
""" |
|
95 |
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
|
96 |
# set request language |
0 | 97 |
vreg = self.vreg |
98 |
if self.user: |
|
99 |
try: |
|
100 |
# 1. user specified language |
|
101 |
lang = vreg.typed_value('ui.language', |
|
102 |
self.user.properties['ui.language']) |
|
103 |
self.set_language(lang) |
|
104 |
return |
|
105 |
except KeyError, ex: |
|
106 |
pass |
|
107 |
if vreg.config['language-negociation']: |
|
108 |
# 2. http negociated language |
|
109 |
for lang in self.header_accept_language(): |
|
110 |
if lang in self.translations: |
|
111 |
self.set_language(lang) |
|
112 |
return |
|
113 |
# 3. default language |
|
114 |
self.set_default_language(vreg) |
|
1426 | 115 |
|
0 | 116 |
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
|
117 |
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
|
118 |
self._ = self.__ = gettext |
0 | 119 |
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
|
120 |
self.cnx.set_session_props(lang=lang) |
0 | 121 |
self.debug('request language: %s', lang) |
1426 | 122 |
|
0 | 123 |
# input form parameters management ######################################## |
1426 | 124 |
|
0 | 125 |
# common form parameters which should be protected against html values |
126 |
# XXX can't add 'eid' for instance since it may be multivalued |
|
127 |
# dont put rql as well, if query contains < and > it will be corrupted! |
|
1426 | 128 |
no_script_form_params = set(('vid', |
129 |
'etype', |
|
0 | 130 |
'vtitle', 'title', |
131 |
'__message', |
|
132 |
'__redirectvid', '__redirectrql')) |
|
1426 | 133 |
|
0 | 134 |
def setup_params(self, params): |
135 |
"""WARNING: we're intentionaly leaving INTERNAL_FIELD_VALUE here |
|
136 |
||
1426 | 137 |
subclasses should overrides to |
0 | 138 |
""" |
139 |
if params is None: |
|
140 |
params = {} |
|
141 |
self.form = params |
|
142 |
encoding = self.encoding |
|
143 |
for k, v in params.items(): |
|
144 |
if isinstance(v, (tuple, list)): |
|
145 |
v = [unicode(x, encoding) for x in v] |
|
146 |
if len(v) == 1: |
|
147 |
v = v[0] |
|
148 |
if k in self.no_script_form_params: |
|
149 |
v = self.no_script_form_param(k, value=v) |
|
150 |
if isinstance(v, str): |
|
151 |
v = unicode(v, encoding) |
|
152 |
if k == '__message': |
|
153 |
self.set_message(v) |
|
154 |
del self.form[k] |
|
155 |
else: |
|
156 |
self.form[k] = v |
|
2202
cb374512949f
link to created entity when redirected to another page
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
157 |
# special key for created entity, added in controller's reset method |
2315
a1849f3ef4af
no link to created entity when no message is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
158 |
# if no message set, we don't want this neither |
a1849f3ef4af
no link to created entity when no message is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
159 |
if '__createdpath' in params and self.message: |
2202
cb374512949f
link to created entity when redirected to another page
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
self.message += ' (<a href="%s">%s</a>)' % ( |
cb374512949f
link to created entity when redirected to another page
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
161 |
self.build_url(params.pop('__createdpath')), |
cb374512949f
link to created entity when redirected to another page
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
162 |
self._('click here to see created entity')) |
1426 | 163 |
|
0 | 164 |
def no_script_form_param(self, param, default=None, value=None): |
165 |
"""ensure there is no script in a user form param |
|
166 |
||
167 |
by default return a cleaned string instead of raising a security |
|
168 |
exception |
|
169 |
||
170 |
this method should be called on every user input (form at least) fields |
|
171 |
that are at some point inserted in a generated html page to protect |
|
172 |
against script kiddies |
|
173 |
""" |
|
174 |
if value is None: |
|
175 |
value = self.form.get(param, default) |
|
176 |
if not value is default and value: |
|
177 |
# safety belt for strange urls like http://...?vtitle=yo&vtitle=yo |
|
178 |
if isinstance(value, (list, tuple)): |
|
179 |
self.error('no_script_form_param got a list (%s). Who generated the URL ?', |
|
180 |
repr(value)) |
|
181 |
value = value[0] |
|
182 |
return remove_html_tags(value) |
|
183 |
return value |
|
1426 | 184 |
|
0 | 185 |
def list_form_param(self, param, form=None, pop=False): |
186 |
"""get param from form parameters and return its value as a list, |
|
187 |
skipping internal markers if any |
|
1426 | 188 |
|
0 | 189 |
* if the parameter isn't defined, return an empty list |
190 |
* if the parameter is a single (unicode) value, return a list |
|
191 |
containing that value |
|
192 |
* if the parameter is already a list or tuple, just skip internal |
|
193 |
markers |
|
194 |
||
195 |
if pop is True, the parameter is removed from the form dictionnary |
|
196 |
""" |
|
197 |
if form is None: |
|
198 |
form = self.form |
|
1426 | 199 |
return list_form_param(form, param, pop) |
200 |
||
0 | 201 |
|
202 |
def reset_headers(self): |
|
203 |
"""used by AutomaticWebTest to clear html headers between tests on |
|
204 |
the same resultset |
|
205 |
""" |
|
206 |
self.html_headers = HTMLHead() |
|
207 |
return self |
|
208 |
||
209 |
# web state helpers ####################################################### |
|
1426 | 210 |
|
0 | 211 |
def set_message(self, msg): |
212 |
assert isinstance(msg, unicode) |
|
213 |
self.message = msg |
|
1426 | 214 |
|
0 | 215 |
def update_search_state(self): |
216 |
"""update the current search state""" |
|
217 |
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
|
218 |
if not searchstate and self.cnx is not None: |
0 | 219 |
searchstate = self.get_session_data('search_state', 'normal') |
220 |
self.set_search_state(searchstate) |
|
221 |
||
222 |
def set_search_state(self, searchstate): |
|
223 |
"""set a new search state""" |
|
224 |
if searchstate is None or searchstate == 'normal': |
|
225 |
self.search_state = (searchstate or 'normal',) |
|
226 |
else: |
|
227 |
self.search_state = ('linksearch', searchstate.split(':')) |
|
228 |
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
|
229 |
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
|
230 |
self.set_session_data('search_state', searchstate) |
0 | 231 |
|
1173
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
232 |
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
|
233 |
"""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
|
234 |
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
|
235 |
""" |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
236 |
try: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
237 |
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
|
238 |
except IndexError: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
239 |
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
|
240 |
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
|
241 |
if etype != searchedtype: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
242 |
return False |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
243 |
return True |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
244 |
|
0 | 245 |
def update_breadcrumbs(self): |
246 |
"""stores the last visisted page in session data""" |
|
247 |
searchstate = self.get_session_data('search_state') |
|
248 |
if searchstate == 'normal': |
|
249 |
breadcrumbs = self.get_session_data('breadcrumbs', None) |
|
250 |
if breadcrumbs is None: |
|
251 |
breadcrumbs = SizeConstrainedList(10) |
|
252 |
self.set_session_data('breadcrumbs', breadcrumbs) |
|
253 |
breadcrumbs.append(self.url()) |
|
254 |
||
255 |
def last_visited_page(self): |
|
256 |
breadcrumbs = self.get_session_data('breadcrumbs', None) |
|
257 |
if breadcrumbs: |
|
258 |
return breadcrumbs.pop() |
|
259 |
return self.base_url() |
|
260 |
||
261 |
def register_onetime_callback(self, func, *args): |
|
262 |
cbname = 'cb_%s' % ( |
|
263 |
sha.sha('%s%s%s%s' % (time.time(), func.__name__, |
|
1426 | 264 |
random.random(), |
0 | 265 |
self.user.login)).hexdigest()) |
266 |
def _cb(req): |
|
267 |
try: |
|
268 |
ret = func(req, *args) |
|
269 |
except TypeError: |
|
270 |
from warnings import warn |
|
271 |
warn('user callback should now take request as argument') |
|
1426 | 272 |
ret = func(*args) |
0 | 273 |
self.unregister_callback(self.pageid, cbname) |
274 |
return ret |
|
275 |
self.set_page_data(cbname, _cb) |
|
276 |
return cbname |
|
1426 | 277 |
|
0 | 278 |
def unregister_callback(self, pageid, cbname): |
279 |
assert pageid is not None |
|
280 |
assert cbname.startswith('cb_') |
|
281 |
self.info('unregistering callback %s for pageid %s', cbname, pageid) |
|
282 |
self.del_page_data(cbname) |
|
283 |
||
284 |
def clear_user_callbacks(self): |
|
285 |
if self.cnx is not None: |
|
286 |
sessdata = self.session_data() |
|
287 |
callbacks = [key for key in sessdata if key.startswith('cb_')] |
|
288 |
for callback in callbacks: |
|
289 |
self.del_session_data(callback) |
|
1426 | 290 |
|
0 | 291 |
# web edition helpers ##################################################### |
1426 | 292 |
|
0 | 293 |
@cached # so it's writed only once |
294 |
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
|
295 |
self.add_js('fckeditor/fckeditor.js') |
0 | 296 |
self.html_headers.define_var('fcklang', self.lang) |
297 |
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
|
298 |
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
|
299 |
def use_fckeditor(self): |
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
940
diff
changeset
|
300 |
return self.vreg.config.fckeditor_installed() and self.property_value('ui.fckeditor') |
0 | 301 |
|
302 |
def edited_eids(self, withtype=False): |
|
303 |
"""return a list of edited eids""" |
|
304 |
yielded = False |
|
305 |
# warning: use .keys since the caller may change `form` |
|
306 |
form = self.form |
|
307 |
try: |
|
308 |
eids = form['eid'] |
|
309 |
except KeyError: |
|
310 |
raise NothingToEdit(None, {None: self._('no selected entities')}) |
|
311 |
if isinstance(eids, basestring): |
|
312 |
eids = (eids,) |
|
313 |
for peid in eids: |
|
314 |
if withtype: |
|
315 |
typekey = '__type:%s' % peid |
|
316 |
assert typekey in form, 'no entity type specified' |
|
317 |
yield peid, form[typekey] |
|
318 |
else: |
|
319 |
yield peid |
|
320 |
yielded = True |
|
321 |
if not yielded: |
|
322 |
raise NothingToEdit(None, {None: self._('no selected entities')}) |
|
323 |
||
324 |
# minparams=3 by default: at least eid, __type, and some params to change |
|
325 |
def extract_entity_params(self, eid, minparams=3): |
|
326 |
"""extract form parameters relative to the given eid""" |
|
327 |
params = {} |
|
328 |
eid = str(eid) |
|
329 |
form = self.form |
|
330 |
for param in form: |
|
331 |
try: |
|
332 |
name, peid = param.split(':', 1) |
|
333 |
except ValueError: |
|
334 |
if not param.startswith('__') and param != "eid": |
|
335 |
self.warning('param %s mis-formatted', param) |
|
336 |
continue |
|
337 |
if peid == eid: |
|
338 |
value = form[param] |
|
339 |
if value == INTERNAL_FIELD_VALUE: |
|
340 |
value = None |
|
341 |
params[name] = value |
|
342 |
params['eid'] = eid |
|
343 |
if len(params) < minparams: |
|
344 |
raise RequestError(self._('missing parameters for entity %s') % eid) |
|
345 |
return params |
|
1426 | 346 |
|
0 | 347 |
def get_pending_operations(self, entity, relname, role): |
348 |
operations = {'insert' : [], 'delete' : []} |
|
349 |
for optype in ('insert', 'delete'): |
|
350 |
data = self.get_session_data('pending_%s' % optype) or () |
|
351 |
for eidfrom, rel, eidto in data: |
|
352 |
if relname == rel: |
|
353 |
if role == 'subject' and entity.eid == eidfrom: |
|
354 |
operations[optype].append(eidto) |
|
355 |
if role == 'object' and entity.eid == eidto: |
|
356 |
operations[optype].append(eidfrom) |
|
357 |
return operations |
|
1426 | 358 |
|
0 | 359 |
def get_pending_inserts(self, eid=None): |
360 |
"""shortcut to access req's pending_insert entry |
|
361 |
||
362 |
This is where are stored relations being added while editing |
|
363 |
an entity. This used to be stored in a temporary cookie. |
|
364 |
""" |
|
365 |
pending = self.get_session_data('pending_insert') or () |
|
366 |
return ['%s:%s:%s' % (subj, rel, obj) for subj, rel, obj in pending |
|
367 |
if eid is None or eid in (subj, obj)] |
|
368 |
||
369 |
def get_pending_deletes(self, eid=None): |
|
370 |
"""shortcut to access req's pending_delete entry |
|
371 |
||
372 |
This is where are stored relations being removed while editing |
|
373 |
an entity. This used to be stored in a temporary cookie. |
|
374 |
""" |
|
375 |
pending = self.get_session_data('pending_delete') or () |
|
376 |
return ['%s:%s:%s' % (subj, rel, obj) for subj, rel, obj in pending |
|
377 |
if eid is None or eid in (subj, obj)] |
|
378 |
||
379 |
def remove_pending_operations(self): |
|
380 |
"""shortcut to clear req's pending_{delete,insert} entries |
|
381 |
||
382 |
This is needed when the edition is completed (whether it's validated |
|
383 |
or cancelled) |
|
384 |
""" |
|
385 |
self.del_session_data('pending_insert') |
|
386 |
self.del_session_data('pending_delete') |
|
387 |
||
388 |
def cancel_edition(self, errorurl): |
|
389 |
"""remove pending operations and `errorurl`'s specific stored data |
|
390 |
""" |
|
391 |
self.del_session_data(errorurl) |
|
392 |
self.remove_pending_operations() |
|
1426 | 393 |
|
0 | 394 |
# high level methods for HTTP headers management ########################## |
395 |
||
396 |
# must be cached since login/password are popped from the form dictionary |
|
397 |
# and this method may be called multiple times during authentication |
|
398 |
@cached |
|
399 |
def get_authorization(self): |
|
400 |
"""Parse and return the Authorization header""" |
|
401 |
if self.authmode == "cookie": |
|
402 |
try: |
|
403 |
user = self.form.pop("__login") |
|
404 |
passwd = self.form.pop("__password", '') |
|
405 |
return user, passwd.encode('UTF8') |
|
406 |
except KeyError: |
|
407 |
self.debug('no login/password in form params') |
|
408 |
return None, None |
|
409 |
else: |
|
410 |
return self.header_authorization() |
|
1426 | 411 |
|
0 | 412 |
def get_cookie(self): |
413 |
"""retrieve request cookies, returns an empty cookie if not found""" |
|
414 |
try: |
|
415 |
return Cookie.SimpleCookie(self.get_header('Cookie')) |
|
416 |
except KeyError: |
|
417 |
return Cookie.SimpleCookie() |
|
418 |
||
419 |
def set_cookie(self, cookie, key, maxage=300): |
|
420 |
"""set / update a cookie key |
|
421 |
||
422 |
by default, cookie will be available for the next 5 minutes. |
|
423 |
Give maxage = None to have a "session" cookie expiring when the |
|
424 |
client close its browser |
|
425 |
""" |
|
426 |
morsel = cookie[key] |
|
427 |
if maxage is not None: |
|
428 |
morsel['Max-Age'] = maxage |
|
429 |
# make sure cookie is set on the correct path |
|
430 |
morsel['path'] = self.base_url_path() |
|
431 |
self.add_header('Set-Cookie', morsel.OutputString()) |
|
432 |
||
433 |
def remove_cookie(self, cookie, key): |
|
434 |
"""remove a cookie by expiring it""" |
|
435 |
morsel = cookie[key] |
|
436 |
morsel['Max-Age'] = 0 |
|
437 |
# The only way to set up cookie age for IE is to use an old "expired" |
|
438 |
# syntax. IE doesn't support Max-Age there is no library support for |
|
1426 | 439 |
# managing |
0 | 440 |
# ===> Do _NOT_ comment this line : |
441 |
morsel['expires'] = 'Thu, 01-Jan-1970 00:00:00 GMT' |
|
442 |
self.add_header('Set-Cookie', morsel.OutputString()) |
|
443 |
||
444 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
445 |
"""set output content type for this request. An optional filename |
|
446 |
may be given |
|
447 |
""" |
|
448 |
if content_type.startswith('text/'): |
|
449 |
content_type += ';charset=' + (encoding or self.encoding) |
|
450 |
self.set_header('content-type', content_type) |
|
451 |
if filename: |
|
452 |
if isinstance(filename, unicode): |
|
453 |
filename = header(filename).encode() |
|
454 |
self.set_header('content-disposition', 'inline; filename=%s' |
|
455 |
% filename) |
|
456 |
||
457 |
# high level methods for HTML headers management ########################## |
|
458 |
||
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
|
459 |
def add_onload(self, jscode): |
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
|
460 |
self.html_headers.add_onload(jscode, self.json_request) |
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
|
461 |
|
0 | 462 |
def add_js(self, jsfiles, localfile=True): |
463 |
"""specify a list of JS files to include in the HTML headers |
|
464 |
:param jsfiles: a JS filename or a list of JS filenames |
|
465 |
:param localfile: if True, the default data dir prefix is added to the |
|
466 |
JS filename |
|
467 |
""" |
|
468 |
if isinstance(jsfiles, basestring): |
|
469 |
jsfiles = (jsfiles,) |
|
470 |
for jsfile in jsfiles: |
|
471 |
if localfile: |
|
472 |
jsfile = self.datadir_url + jsfile |
|
473 |
self.html_headers.add_js(jsfile) |
|
474 |
||
475 |
def add_css(self, cssfiles, media=u'all', localfile=True, ieonly=False): |
|
476 |
"""specify a CSS file to include in the HTML headers |
|
477 |
:param cssfiles: a CSS filename or a list of CSS filenames |
|
478 |
:param media: the CSS's media if necessary |
|
479 |
:param localfile: if True, the default data dir prefix is added to the |
|
480 |
CSS filename |
|
481 |
""" |
|
482 |
if isinstance(cssfiles, basestring): |
|
483 |
cssfiles = (cssfiles,) |
|
484 |
if ieonly: |
|
485 |
if self.ie_browser(): |
|
486 |
add_css = self.html_headers.add_ie_css |
|
487 |
else: |
|
488 |
return # no need to do anything on non IE browsers |
|
489 |
else: |
|
490 |
add_css = self.html_headers.add_css |
|
491 |
for cssfile in cssfiles: |
|
492 |
if localfile: |
|
493 |
cssfile = self.datadir_url + cssfile |
|
494 |
add_css(cssfile, media) |
|
1426 | 495 |
|
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
|
496 |
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
|
497 |
**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
|
498 |
"""builds an ajax url that will replace `nodeid`s content |
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
|
499 |
: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
|
500 |
: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
|
501 |
: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
|
502 |
:param replacemode: defines how the replacement should be done. |
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
|
503 |
Possible values are : |
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
|
504 |
- 'replace' to replace the node's content with the generated HTML |
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
|
505 |
- 'swap' to replace the node itself with the generated HTML |
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
|
506 |
- 'append' to append the generated HTML to the node's content |
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
|
507 |
""" |
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
|
508 |
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
|
509 |
**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
|
510 |
return "javascript: loadxhtml('%s', '%s', '%s')" % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2258
diff
changeset
|
511 |
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
|
512 |
|
0 | 513 |
# urls/path management #################################################### |
1426 | 514 |
|
0 | 515 |
def url(self, includeparams=True): |
516 |
"""return currently accessed url""" |
|
517 |
return self.base_url() + self.relative_path(includeparams) |
|
518 |
||
519 |
def _datadir_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2315
diff
changeset
|
520 |
"""return url of the instance's data directory""" |
0 | 521 |
return self.base_url() + 'data%s/' % self.vreg.config.instance_md5_version() |
1426 | 522 |
|
0 | 523 |
def selected(self, url): |
524 |
"""return True if the url is equivalent to currently accessed url""" |
|
525 |
reqpath = self.relative_path().lower() |
|
526 |
baselen = len(self.base_url()) |
|
527 |
return (reqpath == url[baselen:].lower()) |
|
528 |
||
529 |
def base_url_prepend_host(self, hostname): |
|
530 |
protocol, roothost = urlsplit(self.base_url())[:2] |
|
531 |
if roothost.startswith('www.'): |
|
532 |
roothost = roothost[4:] |
|
533 |
return '%s://%s.%s' % (protocol, hostname, roothost) |
|
534 |
||
535 |
def base_url_path(self): |
|
536 |
"""returns the absolute path of the base url""" |
|
537 |
return urlsplit(self.base_url())[2] |
|
1426 | 538 |
|
0 | 539 |
@cached |
540 |
def from_controller(self): |
|
541 |
"""return the id (string) of the controller issuing the request""" |
|
542 |
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
|
543 |
registered_controllers = self.vreg['controllers'].keys() |
0 | 544 |
if controller in registered_controllers: |
545 |
return controller |
|
546 |
return 'view' |
|
1426 | 547 |
|
0 | 548 |
def external_resource(self, rid, default=_MARKER): |
549 |
"""return a path to an external resource, using its identifier |
|
550 |
||
551 |
raise KeyError if the resource is not defined |
|
552 |
""" |
|
553 |
try: |
|
554 |
value = self.vreg.config.ext_resources[rid] |
|
555 |
except KeyError: |
|
556 |
if default is _MARKER: |
|
557 |
raise |
|
558 |
return default |
|
559 |
if value is None: |
|
560 |
return None |
|
561 |
baseurl = self.datadir_url[:-1] # remove trailing / |
|
562 |
if isinstance(value, list): |
|
563 |
return [v.replace('DATADIR', baseurl) for v in value] |
|
564 |
return value.replace('DATADIR', baseurl) |
|
565 |
external_resource = cached(external_resource, keyarg=1) |
|
566 |
||
567 |
def validate_cache(self): |
|
568 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
569 |
exists and is still usable. |
|
570 |
||
571 |
calls the client-dependant implementation of `_validate_cache` |
|
572 |
""" |
|
573 |
self._validate_cache() |
|
574 |
if self.http_method() == 'HEAD': |
|
575 |
raise StatusResponse(200, '') |
|
1426 | 576 |
|
0 | 577 |
# abstract methods to override according to the web front-end ############# |
1426 | 578 |
|
0 | 579 |
def http_method(self): |
580 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
581 |
raise NotImplementedError() |
|
582 |
||
583 |
def _validate_cache(self): |
|
584 |
"""raise a `DirectResponse` exception if a cached page along the way |
|
585 |
exists and is still usable |
|
586 |
""" |
|
587 |
raise NotImplementedError() |
|
1426 | 588 |
|
0 | 589 |
def relative_path(self, includeparams=True): |
590 |
"""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
|
591 |
to the instance's root, but some other normalization may be needed |
0 | 592 |
so that the returned path may be used to compare to generated urls |
593 |
||
594 |
:param includeparams: |
|
595 |
boolean indicating if GET form parameters should be kept in the path |
|
596 |
""" |
|
597 |
raise NotImplementedError() |
|
598 |
||
599 |
def get_header(self, header, default=None): |
|
600 |
"""return the value associated with the given input HTTP header, |
|
601 |
raise KeyError if the header is not set |
|
602 |
""" |
|
603 |
raise NotImplementedError() |
|
604 |
||
605 |
def set_header(self, header, value): |
|
606 |
"""set an output HTTP header""" |
|
607 |
raise NotImplementedError() |
|
608 |
||
609 |
def add_header(self, header, value): |
|
610 |
"""add an output HTTP header""" |
|
611 |
raise NotImplementedError() |
|
1426 | 612 |
|
0 | 613 |
def remove_header(self, header): |
614 |
"""remove an output HTTP header""" |
|
615 |
raise NotImplementedError() |
|
1426 | 616 |
|
0 | 617 |
def header_authorization(self): |
618 |
"""returns a couple (auth-type, auth-value)""" |
|
619 |
auth = self.get_header("Authorization", None) |
|
620 |
if auth: |
|
621 |
scheme, rest = auth.split(' ', 1) |
|
622 |
scheme = scheme.lower() |
|
623 |
try: |
|
624 |
assert scheme == "basic" |
|
625 |
user, passwd = base64.decodestring(rest).split(":", 1) |
|
626 |
# XXX HTTP header encoding: use email.Header? |
|
627 |
return user.decode('UTF8'), passwd |
|
628 |
except Exception, ex: |
|
629 |
self.debug('bad authorization %s (%s: %s)', |
|
630 |
auth, ex.__class__.__name__, ex) |
|
631 |
return None, None |
|
632 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2559
diff
changeset
|
633 |
@deprecated("use parse_accept_header('Accept-Language')") |
0 | 634 |
def header_accept_language(self): |
635 |
"""returns an ordered list of preferred languages""" |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
636 |
return [value.split('-')[0] for value in |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
637 |
self.parse_accept_header('Accept-Language')] |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
638 |
|
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
639 |
def parse_accept_header(self, header): |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
640 |
"""returns an ordered list of preferred languages""" |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
641 |
accepteds = self.get_header(header, '') |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
642 |
values = [] |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
643 |
for info in accepteds.split(','): |
0 | 644 |
try: |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
645 |
value, scores = info.split(';', 1) |
0 | 646 |
except ValueError: |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
647 |
value = info |
0 | 648 |
score = 1.0 |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
649 |
else: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
650 |
for score in scores.split(';'): |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
651 |
try: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
652 |
scorekey, scoreval = score.split('=') |
1717
d2c4d3bd0602
correct wrong condition and missing import
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
653 |
if scorekey == 'q': # XXX 'level' |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
654 |
score = float(score[2:]) # remove 'q=' |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
655 |
except ValueError: |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
656 |
continue |
1718
26ff2d292183
correct the values list append
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1717
diff
changeset
|
657 |
values.append((score, value)) |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
658 |
values.sort(reverse=True) |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
659 |
return (value for (score, value) in values) |
0 | 660 |
|
661 |
def header_if_modified_since(self): |
|
662 |
"""If the HTTP header If-modified-since is set, return the equivalent |
|
663 |
mx date time value (GMT), else return None |
|
664 |
""" |
|
665 |
raise NotImplementedError() |
|
1426 | 666 |
|
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
667 |
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
|
668 |
"""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
|
669 |
|
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
670 |
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
|
671 |
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
|
672 |
""" |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
673 |
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
|
674 |
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
|
675 |
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
|
676 |
|
0 | 677 |
# page data management #################################################### |
678 |
||
679 |
def get_page_data(self, key, default=None): |
|
680 |
"""return value associated to `key` in curernt page data""" |
|
681 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
682 |
return page_data.get(key, default) |
|
1426 | 683 |
|
0 | 684 |
def set_page_data(self, key, value): |
685 |
"""set value associated to `key` in current page data""" |
|
686 |
self.html_headers.add_unload_pagedata() |
|
687 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
688 |
page_data[key] = value |
|
689 |
return self.cnx.set_session_data(self.pageid, page_data) |
|
1426 | 690 |
|
0 | 691 |
def del_page_data(self, key=None): |
692 |
"""remove value associated to `key` in current page data |
|
693 |
if `key` is None, all page data will be cleared |
|
694 |
""" |
|
695 |
if key is None: |
|
696 |
self.cnx.del_session_data(self.pageid) |
|
697 |
else: |
|
698 |
page_data = self.cnx.get_session_data(self.pageid, {}) |
|
699 |
page_data.pop(key, None) |
|
700 |
self.cnx.set_session_data(self.pageid, page_data) |
|
701 |
||
702 |
# user-agent detection #################################################### |
|
703 |
||
704 |
@cached |
|
705 |
def useragent(self): |
|
706 |
return self.get_header('User-Agent', None) |
|
707 |
||
708 |
def ie_browser(self): |
|
709 |
useragent = self.useragent() |
|
710 |
return useragent and 'MSIE' in useragent |
|
1426 | 711 |
|
0 | 712 |
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
|
713 |
"""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
|
714 |
|
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
715 |
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
|
716 |
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
|
717 |
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
|
718 |
""" |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
719 |
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
|
720 |
return False |
0 | 721 |
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
|
722 |
# * 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
|
723 |
# * 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
|
724 |
# 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
|
725 |
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
|
726 |
or 'Opera' in useragent): |
0 | 727 |
return False |
728 |
return True |
|
729 |
||
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
730 |
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
|
731 |
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
|
732 |
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
|
733 |
return 'text/html' |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
734 |
|
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
|
735 |
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
|
736 |
if self.xhtml_browser(): |
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
|
737 |
return (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE + |
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
|
738 |
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
|
739 |
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
|
740 |
|
0 | 741 |
from cubicweb import set_log_methods |
742 |
set_log_methods(CubicWebRequestBase, LOGGER) |