server/session.py
changeset 9404 3e3e9b37e177
parent 9403 d81207fb9499
child 9458 e2dfdd313dfe
equal deleted inserted replaced
9403:d81207fb9499 9404:3e3e9b37e177
   626                 self.cnxset = None
   626                 self.cnxset = None
   627             finally:
   627             finally:
   628                 cnxset.cnxset_freed()
   628                 cnxset.cnxset_freed()
   629                 self.repo._free_cnxset(cnxset)
   629                 self.repo._free_cnxset(cnxset)
   630 
   630 
   631     @deprecated('[4.0] cnxset are automatically managed now.'
   631     @deprecated('[3.19] cnxset are automatically managed now.'
   632                 ' stop using explicit set and free.')
   632                 ' stop using explicit set and free.')
   633     def set_cnxset(self):
   633     def set_cnxset(self):
   634         self._auto_free_cnx_set = False
   634         self._auto_free_cnx_set = False
   635         return self._set_cnxset()
   635         return self._set_cnxset()
   636 
   636 
   637     @deprecated('[4.0] cnxset are automatically managed now.'
   637     @deprecated('[3.19] cnxset are automatically managed now.'
   638                 ' stop using explicit set and free.')
   638                 ' stop using explicit set and free.')
   639     def free_cnxset(self, ignoremode=False):
   639     def free_cnxset(self, ignoremode=False):
   640         self._auto_free_cnx_set = True
   640         self._auto_free_cnx_set = True
   641         return self._free_cnxset(ignoremode=ignoremode)
   641         return self._free_cnxset(ignoremode=ignoremode)
   642 
   642 
  1186 def cnx_attr(attr_name, writable=False):
  1186 def cnx_attr(attr_name, writable=False):
  1187     """return a property to forward attribute access to connection.
  1187     """return a property to forward attribute access to connection.
  1188 
  1188 
  1189     This is to be used by session"""
  1189     This is to be used by session"""
  1190     args = {}
  1190     args = {}
  1191     @deprecated('[4.0] use a Connection object instead')
  1191     @deprecated('[3.19] use a Connection object instead')
  1192     def attr_from_cnx(session):
  1192     def attr_from_cnx(session):
  1193         return getattr(session._cnx, attr_name)
  1193         return getattr(session._cnx, attr_name)
  1194     args['fget'] = attr_from_cnx
  1194     args['fget'] = attr_from_cnx
  1195     if writable:
  1195     if writable:
  1196         @deprecated('[4.0] use a Connection object instead')
  1196         @deprecated('[3.19] use a Connection object instead')
  1197         def write_attr(session, value):
  1197         def write_attr(session, value):
  1198             return setattr(session._cnx, attr_name, value)
  1198             return setattr(session._cnx, attr_name, value)
  1199         args['fset'] = write_attr
  1199         args['fset'] = write_attr
  1200     return property(**args)
  1200     return property(**args)
  1201 
  1201 
  1202 def cnx_meth(meth_name):
  1202 def cnx_meth(meth_name):
  1203     """return a function forwarding calls to connection.
  1203     """return a function forwarding calls to connection.
  1204 
  1204 
  1205     This is to be used by session"""
  1205     This is to be used by session"""
  1206     @deprecated('[4.0] use a Connection object instead')
  1206     @deprecated('[3.19] use a Connection object instead')
  1207     def meth_from_cnx(session, *args, **kwargs):
  1207     def meth_from_cnx(session, *args, **kwargs):
  1208         result = getattr(session._cnx, meth_name)(*args, **kwargs)
  1208         result = getattr(session._cnx, meth_name)(*args, **kwargs)
  1209         if getattr(result, '_cw', None) is not None:
  1209         if getattr(result, '_cw', None) is not None:
  1210             result._cw = session
  1210             result._cw = session
  1211         return result
  1211         return result
  1370     @property
  1370     @property
  1371     def timestamp(self):
  1371     def timestamp(self):
  1372         return float(self._timestamp)
  1372         return float(self._timestamp)
  1373 
  1373 
  1374     @property
  1374     @property
  1375     @deprecated('[4.0] session.id is deprecated. use session.sessionid')
  1375     @deprecated('[3.19] session.id is deprecated. use session.sessionid')
  1376     def id(self):
  1376     def id(self):
  1377         return self.sessionid
  1377         return self.sessionid
  1378 
  1378 
  1379     @property
  1379     @property
  1380     def login(self):
  1380     def login(self):
  1434             return self.__threaddata.cnx
  1434             return self.__threaddata.cnx
  1435         except AttributeError:
  1435         except AttributeError:
  1436             self.set_cnx()
  1436             self.set_cnx()
  1437             return self.__threaddata.cnx
  1437             return self.__threaddata.cnx
  1438 
  1438 
  1439     @deprecated('[4.0] use a Connection object instead')
  1439     @deprecated('[3.19] use a Connection object instead')
  1440     def get_option_value(self, option, foreid=None):
  1440     def get_option_value(self, option, foreid=None):
  1441         return self.repo.get_option_value(option, foreid)
  1441         return self.repo.get_option_value(option, foreid)
  1442 
  1442 
  1443     @deprecated('[4.0] use a Connection object instead')
  1443     @deprecated('[3.19] use a Connection object instead')
  1444     def transaction(self, free_cnxset=True):
  1444     def transaction(self, free_cnxset=True):
  1445         """return context manager to enter a transaction for the session: when
  1445         """return context manager to enter a transaction for the session: when
  1446         exiting the `with` block on exception, call `session.rollback()`, else
  1446         exiting the `with` block on exception, call `session.rollback()`, else
  1447         call `session.commit()` on normal exit.
  1447         call `session.commit()` on normal exit.
  1448 
  1448 
  1467     added_in_transaction = cnx_meth('added_in_transaction')
  1467     added_in_transaction = cnx_meth('added_in_transaction')
  1468     rtype_eids_rdef = cnx_meth('rtype_eids_rdef')
  1468     rtype_eids_rdef = cnx_meth('rtype_eids_rdef')
  1469 
  1469 
  1470     # security control #########################################################
  1470     # security control #########################################################
  1471 
  1471 
  1472     @deprecated('[4.0] use a Connection object instead')
  1472     @deprecated('[3.19] use a Connection object instead')
  1473     def security_enabled(self, read=None, write=None):
  1473     def security_enabled(self, read=None, write=None):
  1474         return _session_security_enabled(self, read=read, write=write)
  1474         return _session_security_enabled(self, read=read, write=write)
  1475 
  1475 
  1476     read_security = cnx_attr('read_security', writable=True)
  1476     read_security = cnx_attr('read_security', writable=True)
  1477     write_security = cnx_attr('write_security', writable=True)
  1477     write_security = cnx_attr('write_security', writable=True)
  1479 
  1479 
  1480     # hooks activation control #################################################
  1480     # hooks activation control #################################################
  1481     # all hooks should be activated during normal execution
  1481     # all hooks should be activated during normal execution
  1482 
  1482 
  1483 
  1483 
  1484     @deprecated('[4.0] use a Connection object instead')
  1484     @deprecated('[3.19] use a Connection object instead')
  1485     def allow_all_hooks_but(self, *categories):
  1485     def allow_all_hooks_but(self, *categories):
  1486         return _session_hooks_control(self, HOOKS_ALLOW_ALL, *categories)
  1486         return _session_hooks_control(self, HOOKS_ALLOW_ALL, *categories)
  1487     @deprecated('[4.0] use a Connection object instead')
  1487     @deprecated('[3.19] use a Connection object instead')
  1488     def deny_all_hooks_but(self, *categories):
  1488     def deny_all_hooks_but(self, *categories):
  1489         return _session_hooks_control(self, HOOKS_DENY_ALL, *categories)
  1489         return _session_hooks_control(self, HOOKS_DENY_ALL, *categories)
  1490 
  1490 
  1491     hooks_mode = cnx_attr('hooks_mode')
  1491     hooks_mode = cnx_attr('hooks_mode')
  1492 
  1492 
  1497     is_hook_category_activated = cnx_meth('is_hook_category_activated')
  1497     is_hook_category_activated = cnx_meth('is_hook_category_activated')
  1498     is_hook_activated = cnx_meth('is_hook_activated')
  1498     is_hook_activated = cnx_meth('is_hook_activated')
  1499 
  1499 
  1500     # connection management ###################################################
  1500     # connection management ###################################################
  1501 
  1501 
  1502     @deprecated('[4.0] use a Connection object instead')
  1502     @deprecated('[3.19] use a Connection object instead')
  1503     def keep_cnxset_mode(self, mode):
  1503     def keep_cnxset_mode(self, mode):
  1504         """set `mode`, e.g. how the session will keep its connections set:
  1504         """set `mode`, e.g. how the session will keep its connections set:
  1505 
  1505 
  1506         * if mode == 'write', the connections set is freed after each read
  1506         * if mode == 'write', the connections set is freed after each read
  1507           query, but kept until the transaction's end (eg commit or rollback)
  1507           query, but kept until the transaction's end (eg commit or rollback)
  1522 
  1522 
  1523     mode = cnx_attr('mode', writable=True)
  1523     mode = cnx_attr('mode', writable=True)
  1524     commit_state = cnx_attr('commit_state', writable=True)
  1524     commit_state = cnx_attr('commit_state', writable=True)
  1525 
  1525 
  1526     @property
  1526     @property
  1527     @deprecated('[4.0] use a Connection object instead')
  1527     @deprecated('[3.19] use a Connection object instead')
  1528     def cnxset(self):
  1528     def cnxset(self):
  1529         """connections set, set according to transaction mode for each query"""
  1529         """connections set, set according to transaction mode for each query"""
  1530         if self._closed:
  1530         if self._closed:
  1531             self.free_cnxset(True)
  1531             self.free_cnxset(True)
  1532             raise SessionClosedError('try to access connections set on a closed session %s' % self.id)
  1532             raise SessionClosedError('try to access connections set on a closed session %s' % self.id)
  1578     call_service = cnx_meth('call_service')
  1578     call_service = cnx_meth('call_service')
  1579 
  1579 
  1580     # request interface #######################################################
  1580     # request interface #######################################################
  1581 
  1581 
  1582     @property
  1582     @property
  1583     @deprecated('[4.0] use a Connection object instead')
  1583     @deprecated('[3.19] use a Connection object instead')
  1584     def cursor(self):
  1584     def cursor(self):
  1585         """return a rql cursor"""
  1585         """return a rql cursor"""
  1586         return self
  1586         return self
  1587 
  1587 
  1588     set_entity_cache  = cnx_meth('set_entity_cache')
  1588     set_entity_cache  = cnx_meth('set_entity_cache')
  1593     source_defs = cnx_meth('source_defs')
  1593     source_defs = cnx_meth('source_defs')
  1594     describe = cnx_meth('describe')
  1594     describe = cnx_meth('describe')
  1595     source_from_eid = cnx_meth('source_from_eid')
  1595     source_from_eid = cnx_meth('source_from_eid')
  1596 
  1596 
  1597 
  1597 
  1598     @deprecated('[4.0] use a Connection object instead')
  1598     @deprecated('[3.19] use a Connection object instead')
  1599     def execute(self, *args, **kwargs):
  1599     def execute(self, *args, **kwargs):
  1600         """db-api like method directly linked to the querier execute method.
  1600         """db-api like method directly linked to the querier execute method.
  1601 
  1601 
  1602         See :meth:`cubicweb.dbapi.Cursor.execute` documentation.
  1602         See :meth:`cubicweb.dbapi.Cursor.execute` documentation.
  1603         """
  1603         """
  1622                 else:
  1622                 else:
  1623                     cnx.clear()
  1623                     cnx.clear()
  1624             else:
  1624             else:
  1625                 cnx.clear()
  1625                 cnx.clear()
  1626 
  1626 
  1627     @deprecated('[4.0] use a Connection object instead')
  1627     @deprecated('[3.19] use a Connection object instead')
  1628     def commit(self, free_cnxset=True, reset_pool=None):
  1628     def commit(self, free_cnxset=True, reset_pool=None):
  1629         """commit the current session's transaction"""
  1629         """commit the current session's transaction"""
  1630         cstate = self._cnx.commit_state
  1630         cstate = self._cnx.commit_state
  1631         if cstate == 'uncommitable':
  1631         if cstate == 'uncommitable':
  1632             raise QueryError('transaction must be rolled back')
  1632             raise QueryError('transaction must be rolled back')
  1633         try:
  1633         try:
  1634             return self._cnx.commit(free_cnxset, reset_pool)
  1634             return self._cnx.commit(free_cnxset, reset_pool)
  1635         finally:
  1635         finally:
  1636             self._clear_thread_data(free_cnxset)
  1636             self._clear_thread_data(free_cnxset)
  1637 
  1637 
  1638     @deprecated('[4.0] use a Connection object instead')
  1638     @deprecated('[3.19] use a Connection object instead')
  1639     def rollback(self, free_cnxset=True, **kwargs):
  1639     def rollback(self, free_cnxset=True, **kwargs):
  1640         """rollback the current session's transaction"""
  1640         """rollback the current session's transaction"""
  1641         try:
  1641         try:
  1642             return self._cnx.rollback(free_cnxset, **kwargs)
  1642             return self._cnx.rollback(free_cnxset, **kwargs)
  1643         finally:
  1643         finally:
  1698     rql_rewriter = cnx_attr('_rewriter')
  1698     rql_rewriter = cnx_attr('_rewriter')
  1699 
  1699 
  1700     # deprecated ###############################################################
  1700     # deprecated ###############################################################
  1701 
  1701 
  1702     @property
  1702     @property
  1703     @deprecated('[4.0] use a Connection object instead')
  1703     @deprecated('[3.19] use a Connection object instead')
  1704     def anonymous_session(self):
  1704     def anonymous_session(self):
  1705         # XXX for now, anonymous-user is a web side option.
  1705         # XXX for now, anonymous-user is a web side option.
  1706         # It will only be present inside all-in-one instance.
  1706         # It will only be present inside all-in-one instance.
  1707         # there is plan to move it down to global config.
  1707         # there is plan to move it down to global config.
  1708         return self.user.login == self.repo.config.get('anonymous-user')
  1708         return self.user.login == self.repo.config.get('anonymous-user')