dbapi.py
changeset 2293 7ded2a1416e4
parent 2266 efc6de279644
child 2476 1294a6bdf3bf
equal deleted inserted replaced
2235:d5987f75c97c 2293:7ded2a1416e4
    61                                   '(not registered in Pyro), '
    61                                   '(not registered in Pyro), '
    62                                   'you may have to restart your server-side '
    62                                   'you may have to restart your server-side '
    63                                   'application' % nsid)
    63                                   'application' % nsid)
    64         return core.getProxyForURI(uri)
    64         return core.getProxyForURI(uri)
    65 
    65 
    66 def repo_connect(repo, user, password, cnxprops=None):
    66 def repo_connect(repo, login, password, cnxprops=None):
    67     """Constructor to create a new connection to the CubicWeb repository.
    67     """Constructor to create a new connection to the CubicWeb repository.
    68 
    68 
    69     Returns a Connection instance.
    69     Returns a Connection instance.
    70     """
    70     """
    71     cnxprops = cnxprops or ConnectionProperties('inmemory')
    71     cnxprops = cnxprops or ConnectionProperties('inmemory')
    72     cnxid = repo.connect(unicode(user), password, cnxprops=cnxprops)
    72     cnxid = repo.connect(unicode(login), password, cnxprops=cnxprops)
    73     cnx = Connection(repo, cnxid, cnxprops)
    73     cnx = Connection(repo, cnxid, cnxprops)
    74     if cnxprops.cnxtype == 'inmemory':
    74     if cnxprops.cnxtype == 'inmemory':
    75         cnx.vreg = repo.vreg
    75         cnx.vreg = repo.vreg
    76     return cnx
    76     return cnx
    77 
    77 
    78 def connect(database=None, user=None, password=None, host=None,
    78 def connect(database=None, login=None, password=None, host=None,
    79             group=None, cnxprops=None, port=None, setvreg=True, mulcnx=True,
    79             group=None, cnxprops=None, port=None, setvreg=True, mulcnx=True,
    80             initlog=True):
    80             initlog=True):
    81     """Constructor for creating a connection to the CubicWeb repository.
    81     """Constructor for creating a connection to the CubicWeb repository.
    82     Returns a Connection object.
    82     Returns a Connection object.
    83 
    83 
   109                 print 'aliasing', newetype, 'to', oldetype
   109                 print 'aliasing', newetype, 'to', oldetype
   110                 schema._entities[newetype] = schema._entities[oldetype]
   110                 schema._entities[newetype] = schema._entities[oldetype]
   111         vreg.set_schema(schema)
   111         vreg.set_schema(schema)
   112     else:
   112     else:
   113         vreg = None
   113         vreg = None
   114     cnx = repo_connect(repo, user, password, cnxprops)
   114     cnx = repo_connect(repo, login, password, cnxprops)
   115     cnx.vreg = vreg
   115     cnx.vreg = vreg
   116     return cnx
   116     return cnx
   117 
   117 
   118 def in_memory_cnx(config, user, password):
   118 def in_memory_cnx(config, login, password):
   119     """usefull method for testing and scripting to get a dbapi.Connection
   119     """usefull method for testing and scripting to get a dbapi.Connection
   120     object connected to an in-memory repository instance
   120     object connected to an in-memory repository instance
   121     """
   121     """
   122     if isinstance(config, CubicWebRegistry):
   122     if isinstance(config, CubicWebRegistry):
   123         vreg = config
   123         vreg = config
   126         vreg = None
   126         vreg = None
   127     # get local access to the repository
   127     # get local access to the repository
   128     repo = get_repository('inmemory', config=config, vreg=vreg)
   128     repo = get_repository('inmemory', config=config, vreg=vreg)
   129     # connection to the CubicWeb repository
   129     # connection to the CubicWeb repository
   130     cnxprops = ConnectionProperties('inmemory')
   130     cnxprops = ConnectionProperties('inmemory')
   131     cnx = repo_connect(repo, user, password, cnxprops=cnxprops)
   131     cnx = repo_connect(repo, login, password, cnxprops=cnxprops)
   132     return repo, cnx
   132     return repo, cnx
   133 
   133 
   134 
   134 
   135 class DBAPIRequest(RequestSessionMixIn):
   135 class DBAPIRequest(RequestSessionMixIn):
   136 
   136 
   243     # server session compat layer #############################################
   243     # server session compat layer #############################################
   244 
   244 
   245     @property
   245     @property
   246     def user(self):
   246     def user(self):
   247         if self._user is None and self.cnx:
   247         if self._user is None and self.cnx:
   248             self.set_user(self.cnx.user(self))
   248             self.set_user(self.cnx.user(self, {'lang': self.lang}))
   249         return self._user
   249         return self._user
   250 
   250 
   251     def set_user(self, user):
   251     def set_user(self, user):
   252         self._user = user
   252         self._user = user
   253         if user:
   253         if user:
   364             pass
   364             pass
   365 
   365 
   366     def check(self):
   366     def check(self):
   367         """raise `BadSessionId` if the connection is no more valid"""
   367         """raise `BadSessionId` if the connection is no more valid"""
   368         self._repo.check_session(self.sessionid)
   368         self._repo.check_session(self.sessionid)
       
   369 
       
   370     def set_session_props(self, **props):
       
   371         """raise `BadSessionId` if the connection is no more valid"""
       
   372         self._repo.set_session_props(self.sessionid, props)
   369 
   373 
   370     def get_shared_data(self, key, default=None, pop=False):
   374     def get_shared_data(self, key, default=None, pop=False):
   371         """return value associated to `key` in shared data"""
   375         """return value associated to `key` in shared data"""
   372         return self._repo.get_shared_data(self.sessionid, key, default, pop)
   376         return self._repo.get_shared_data(self.sessionid, key, default, pop)
   373 
   377 
   432         return self._repo.source_defs()
   436         return self._repo.source_defs()
   433 
   437 
   434     def user(self, req=None, props=None):
   438     def user(self, req=None, props=None):
   435         """return the User object associated to this connection"""
   439         """return the User object associated to this connection"""
   436         # cnx validity is checked by the call to .user_info
   440         # cnx validity is checked by the call to .user_info
   437         eid, login, groups, properties = self._repo.user_info(self.sessionid, props)
   441         eid, login, groups, properties = self._repo.user_info(self.sessionid,
       
   442                                                               props)
   438         if req is None:
   443         if req is None:
   439             req = self.request()
   444             req = self.request()
   440         rset = req.eid_rset(eid, 'CWUser')
   445         rset = req.eid_rset(eid, 'CWUser')
   441         user = self.vreg.etype_class('CWUser')(req, rset, row=0, groups=groups,
   446         user = self.vreg.etype_class('CWUser')(req, rset, row=0, groups=groups,
   442                                                properties=properties)
   447                                                properties=properties)