web/request.py
changeset 6564 ff9f7c566464
parent 6543 66145280a7e6
child 6581 4a3b264589dc
equal deleted inserted replaced
6563:33343d6eae0a 6564:ff9f7c566464
    43 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
    43 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
    44                           RequestError, StatusResponse)
    44                           RequestError, StatusResponse)
    45 from cubicweb.web.http_headers import Headers
    45 from cubicweb.web.http_headers import Headers
    46 
    46 
    47 _MARKER = object()
    47 _MARKER = object()
       
    48 
       
    49 def build_cb_uid(seed):
       
    50     sha = hashlib.sha1('%s%s%s' % (time.time(), seed, random.random()))
       
    51     return 'cb_%s' % (sha.hexdigest())
    48 
    52 
    49 
    53 
    50 def list_form_param(form, param, pop=False):
    54 def list_form_param(form, param, pop=False):
    51     """get param from form parameters and return its value as a list,
    55     """get param from form parameters and return its value as a list,
    52     skipping internal markers if any
    56     skipping internal markers if any
   380         assert not kwargs, 'dunno what to do with remaining kwargs: %s' % kwargs
   384         assert not kwargs, 'dunno what to do with remaining kwargs: %s' % kwargs
   381         cbname = self.register_onetime_callback(cb, *cbargs)
   385         cbname = self.register_onetime_callback(cb, *cbargs)
   382         return "javascript: %s" % getattr(js, jsfunc)(cbname, *args)
   386         return "javascript: %s" % getattr(js, jsfunc)(cbname, *args)
   383 
   387 
   384     def register_onetime_callback(self, func, *args):
   388     def register_onetime_callback(self, func, *args):
   385         cbname = 'cb_%s' % (
   389         cbname = build_cb_uid(func.__name__)
   386             hashlib.sha1('%s%s%s%s' % (time.time(), func.__name__,
       
   387                                        random.random(),
       
   388                                        self.user.login)).hexdigest())
       
   389         def _cb(req):
   390         def _cb(req):
   390             try:
   391             try:
   391                 ret = func(req, *args)
   392                 ret = func(req, *args)
   392             except TypeError:
   393             except TypeError:
   393                 warn('[3.2] user callback should now take request as argument')
   394                 warn('[3.2] user callback should now take request as argument')
   605           - 'append' to append the generated HTML to the node's content
   606           - 'append' to append the generated HTML to the node's content
   606 
   607 
   607         Arbitrary extra named arguments may be given, they will be included as
   608         Arbitrary extra named arguments may be given, they will be included as
   608         parameters of the generated url.
   609         parameters of the generated url.
   609         """
   610         """
       
   611         # define a function in headers and use it in the link to avoid url
       
   612         # unescaping pb: browsers give the js expression to the interpreter
       
   613         # after having url unescaping the content. This may make appear some
       
   614         # quote or other special characters that will break the js expression.
   610         extraparams.setdefault('fname', 'view')
   615         extraparams.setdefault('fname', 'view')
   611         url = self.build_url('json', **extraparams)
   616         url = self.build_url('json', **extraparams)
   612         return "javascript: $('#%s').%s; $.noop()" % (
   617         cbname = build_cb_uid(url[:50])
   613             nodeid, js.loadxhtml(url, None, 'get', replacemode))
   618         jscode = 'function %s() { $("#%s").%s; }' % (
       
   619             cbname, nodeid, js.loadxhtml(url, None, 'get', replacemode))
       
   620         self.html_headers.add_post_inline_script(jscode)
       
   621         return "javascript: %s()" % cbname
   614 
   622 
   615     # urls/path management ####################################################
   623     # urls/path management ####################################################
   616 
   624 
   617     def url(self, includeparams=True):
   625     def url(self, includeparams=True):
   618         """return currently accessed url"""
   626         """return currently accessed url"""