web/request.py
changeset 2792 135580d15d42
parent 2788 8d3dbe577d3a
child 2801 7ef4c1c9266b
equal deleted inserted replaced
2791:7ef2b08fbe28 2792:135580d15d42
    13 import random
    13 import random
    14 import base64
    14 import base64
    15 from urlparse import urlsplit
    15 from urlparse import urlsplit
    16 from itertools import count
    16 from itertools import count
    17 
    17 
       
    18 from simplejson import dumps
       
    19 
    18 from rql.utils import rqlvar_maker
    20 from rql.utils import rqlvar_maker
    19 
    21 
    20 from logilab.common.decorators import cached
    22 from logilab.common.decorators import cached
    21 from logilab.common.deprecation import deprecated
    23 from logilab.common.deprecation import deprecated
    22 
       
    23 from logilab.mtconverter import xml_escape
    24 from logilab.mtconverter import xml_escape
    24 
    25 
    25 from cubicweb.dbapi import DBAPIRequest
    26 from cubicweb.dbapi import DBAPIRequest
    26 from cubicweb.common.mail import header
    27 from cubicweb.common.mail import header
    27 from cubicweb.common.uilib import remove_html_tags
    28 from cubicweb.common.uilib import remove_html_tags
    83         # tabindex generator
    84         # tabindex generator
    84         self.tabindexgen = count(1)
    85         self.tabindexgen = count(1)
    85         self.next_tabindex = self.tabindexgen.next
    86         self.next_tabindex = self.tabindexgen.next
    86         # page id, set by htmlheader template
    87         # page id, set by htmlheader template
    87         self.pageid = None
    88         self.pageid = None
    88         self.varmaker = rqlvar_maker()
       
    89         self.datadir_url = self._datadir_url()
    89         self.datadir_url = self._datadir_url()
       
    90 
       
    91     @property
       
    92     def varmaker(self)
       
    93         varmaker = self.get_page_data('rql_varmaker')
       
    94         if varmaker is None:
       
    95             varmaker = rqlvar_maker()
       
    96             self.set_page_data('rql_varmaker', varmaker)
       
    97         return varmaker
    90 
    98 
    91     def set_connection(self, cnx, user=None):
    99     def set_connection(self, cnx, user=None):
    92         """method called by the session handler when the user is authenticated
   100         """method called by the session handler when the user is authenticated
    93         or an anonymous connection is open
   101         or an anonymous connection is open
    94         """
   102         """
   255         breadcrumbs = self.get_session_data('breadcrumbs', None)
   263         breadcrumbs = self.get_session_data('breadcrumbs', None)
   256         if breadcrumbs:
   264         if breadcrumbs:
   257             return breadcrumbs.pop()
   265             return breadcrumbs.pop()
   258         return self.base_url()
   266         return self.base_url()
   259 
   267 
       
   268     def user_rql_callback(self, args, msg=None):
       
   269         """register a user callback to execute some rql query and return an url
       
   270         to call it ready to be inserted in html
       
   271         """
       
   272         def rqlexec(req, rql, args=None, key=None):
       
   273             req.execute(rql, args, key)
       
   274         return self.user_callback(rqlexec, args, msg)
       
   275 
       
   276     def user_callback(self, cb, args, msg=None, nonify=False):
       
   277         """register the given user callback and return an url to call it ready to be
       
   278         inserted in html
       
   279         """
       
   280         self.add_js('cubicweb.ajax.js')
       
   281         cbname = self.register_onetime_callback(cb, *args)
       
   282         msg = dumps(msg or '')
       
   283         return "javascript:userCallbackThenReloadPage('%s', %s)" % (
       
   284             cbname, msg)
       
   285 
   260     def register_onetime_callback(self, func, *args):
   286     def register_onetime_callback(self, func, *args):
   261         cbname = 'cb_%s' % (
   287         cbname = 'cb_%s' % (
   262             sha.sha('%s%s%s%s' % (time.time(), func.__name__,
   288             sha.sha('%s%s%s%s' % (time.time(), func.__name__,
   263                                   random.random(),
   289                                   random.random(),
   264                                   self.user.login)).hexdigest())
   290                                   self.user.login)).hexdigest())