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