35 from logilab.mtconverter import xml_escape |
35 from logilab.mtconverter import xml_escape |
36 |
36 |
37 from cubicweb.dbapi import DBAPIRequest |
37 from cubicweb.dbapi import DBAPIRequest |
38 from cubicweb.mail import header |
38 from cubicweb.mail import header |
39 from cubicweb.uilib import remove_html_tags |
39 from cubicweb.uilib import remove_html_tags |
40 from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid, json_dumps |
40 from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid, js |
41 from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT |
41 from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT |
42 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, |
42 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, |
43 RequestError, StatusResponse) |
43 RequestError, StatusResponse) |
44 from cubicweb.web.http_headers import Headers |
44 from cubicweb.web.http_headers import Headers |
45 |
45 |
340 breadcrumbs = self.session.data.get('breadcrumbs') |
340 breadcrumbs = self.session.data.get('breadcrumbs') |
341 if breadcrumbs: |
341 if breadcrumbs: |
342 return breadcrumbs.pop() |
342 return breadcrumbs.pop() |
343 return self.base_url() |
343 return self.base_url() |
344 |
344 |
345 def user_rql_callback(self, args, msg=None): |
345 def user_rql_callback(self, rqlargs, *args, **kwargs): |
346 """register a user callback to execute some rql query and return an url |
346 """register a user callback to execute some rql query and return an url |
347 to call it ready to be inserted in html |
347 to call it ready to be inserted in html. |
|
348 |
|
349 rqlargs should be a tuple containing argument to give to the execute function. |
|
350 |
|
351 For other allowed arguments, see :meth:`user_callback` method |
348 """ |
352 """ |
349 def rqlexec(req, rql, args=None, key=None): |
353 def rqlexec(req, rql, args=None, key=None): |
350 req.execute(rql, args, key) |
354 req.execute(rql, args, key) |
351 return self.user_callback(rqlexec, args, msg) |
355 return self.user_callback(rqlexec, rqlargs, *args, **kwargs) |
352 |
356 |
353 def user_callback(self, cb, args, msg=None, nonify=False): |
357 def user_callback(self, cb, cbargs, *args, **kwargs): |
354 """register the given user callback and return an url to call it ready to be |
358 """register the given user callback and return an url to call it ready |
355 inserted in html |
359 to be inserted in html. |
|
360 |
|
361 You can specify the underlying js function to call using a 'jsfunc' |
|
362 named args, to one of :func:`userCallback`, |
|
363 ':func:`userCallbackThenUpdateUI`, ':func:`userCallbackThenReloadPage` |
|
364 (the default). Take care arguments may vary according to the used |
|
365 function. |
356 """ |
366 """ |
357 self.add_js('cubicweb.ajax.js') |
367 self.add_js('cubicweb.ajax.js') |
358 cbname = self.register_onetime_callback(cb, *args) |
368 jsfunc = kwargs.pop('jsfunc', 'userCallbackThenReloadPage') |
359 msg = json_dumps(msg or '') |
369 if 'msg' in kwargs: |
360 return "javascript:userCallbackThenReloadPage('%s', %s)" % ( |
370 warn('[3.10] msg should be given as positional argument', |
361 cbname, msg) |
371 DeprecationWarning, stacklevel=2) |
|
372 args = (kwargs.pop('msg'),) + args |
|
373 assert not kwargs, 'dunno what to do with remaining kwargs: %s' % kwargs |
|
374 cbname = self.register_onetime_callback(cb, *cbargs) |
|
375 return "javascript: %s" % getattr(js, jsfunc)(cbname, *args) |
362 |
376 |
363 def register_onetime_callback(self, func, *args): |
377 def register_onetime_callback(self, func, *args): |
364 cbname = 'cb_%s' % ( |
378 cbname = 'cb_%s' % ( |
365 hashlib.sha1('%s%s%s%s' % (time.time(), func.__name__, |
379 hashlib.sha1('%s%s%s%s' % (time.time(), func.__name__, |
366 random.random(), |
380 random.random(), |
587 Arbitrary extra named arguments may be given, they will be included as |
601 Arbitrary extra named arguments may be given, they will be included as |
588 parameters of the generated url. |
602 parameters of the generated url. |
589 """ |
603 """ |
590 extraparams.setdefault('fname', 'view') |
604 extraparams.setdefault('fname', 'view') |
591 url = self.build_url('json', **extraparams) |
605 url = self.build_url('json', **extraparams) |
592 return "javascript: $('#%s').loadxhtml(%s, null, 'get', '%s'); noop()" % ( |
606 return "javascript: $('#%s').%s; noop()" % ( |
593 nodeid, json_dumps(url), replacemode) |
607 nodeid, js.loadxtml(url, None, 'get', replacemode) |
594 |
608 |
595 # urls/path management #################################################### |
609 # urls/path management #################################################### |
596 |
610 |
597 def url(self, includeparams=True): |
611 def url(self, includeparams=True): |
598 """return currently accessed url""" |
612 """return currently accessed url""" |