equal
deleted
inserted
replaced
19 |
19 |
20 from __future__ import print_function |
20 from __future__ import print_function |
21 |
21 |
22 import functools |
22 import functools |
23 import sys |
23 import sys |
24 from time import time |
|
25 from uuid import uuid4 |
24 from uuid import uuid4 |
26 from warnings import warn |
25 from warnings import warn |
27 from contextlib import contextmanager |
26 from contextlib import contextmanager |
28 from logging import getLogger |
27 from logging import getLogger |
29 |
28 |
254 |
253 |
255 #: server.Repository object |
254 #: server.Repository object |
256 self.repo = session.repo |
255 self.repo = session.repo |
257 self.vreg = self.repo.vreg |
256 self.vreg = self.repo.vreg |
258 self._execute = self.repo.querier.execute |
257 self._execute = self.repo.querier.execute |
259 |
|
260 # other session utility |
|
261 self._session_timestamp = session._timestamp |
|
262 |
258 |
263 # internal (root) session |
259 # internal (root) session |
264 self.is_internal_session = isinstance(session.user, InternalManager) |
260 self.is_internal_session = isinstance(session.user, InternalManager) |
265 |
261 |
266 #: dict containing arbitrary data cleared at the end of the transaction |
262 #: dict containing arbitrary data cleared at the end of the transaction |
754 def execute(self, rql, kwargs=None, build_descr=True): |
750 def execute(self, rql, kwargs=None, build_descr=True): |
755 """db-api like method directly linked to the querier execute method. |
751 """db-api like method directly linked to the querier execute method. |
756 |
752 |
757 See :meth:`cubicweb.dbapi.Cursor.execute` documentation. |
753 See :meth:`cubicweb.dbapi.Cursor.execute` documentation. |
758 """ |
754 """ |
759 self._session_timestamp.touch() |
|
760 rset = self._execute(self, rql, kwargs, build_descr) |
755 rset = self._execute(self, rql, kwargs, build_descr) |
761 rset.req = self |
756 rset.req = self |
762 self._session_timestamp.touch() |
|
763 return rset |
757 return rset |
764 |
758 |
765 @_open_only |
759 @_open_only |
766 def rollback(self, free_cnxset=None, reset_pool=None): |
760 def rollback(self, free_cnxset=None, reset_pool=None): |
767 """rollback the current transaction""" |
761 """rollback the current transaction""" |
784 self.critical('rollback error', exc_info=sys.exc_info()) |
778 self.critical('rollback error', exc_info=sys.exc_info()) |
785 continue |
779 continue |
786 cnxset.rollback() |
780 cnxset.rollback() |
787 self.debug('rollback for transaction %s done', self.connectionid) |
781 self.debug('rollback for transaction %s done', self.connectionid) |
788 finally: |
782 finally: |
789 self._session_timestamp.touch() |
|
790 self.clear() |
783 self.clear() |
791 |
784 |
792 @_open_only |
785 @_open_only |
793 def commit(self, free_cnxset=None, reset_pool=None): |
786 def commit(self, free_cnxset=None, reset_pool=None): |
794 """commit the current session's transaction""" |
787 """commit the current session's transaction""" |
876 self.critical('error while postcommit', |
869 self.critical('error while postcommit', |
877 exc_info=sys.exc_info()) |
870 exc_info=sys.exc_info()) |
878 self.debug('postcommit transaction %s done', self.connectionid) |
871 self.debug('postcommit transaction %s done', self.connectionid) |
879 return self.transaction_uuid(set=False) |
872 return self.transaction_uuid(set=False) |
880 finally: |
873 finally: |
881 self._session_timestamp.touch() |
|
882 self.clear() |
874 self.clear() |
883 |
875 |
884 # resource accessors ###################################################### |
876 # resource accessors ###################################################### |
885 |
877 |
886 @_open_only |
878 @_open_only |
926 return setattr(session._cnx, attr_name, value) |
918 return setattr(session._cnx, attr_name, value) |
927 args['fset'] = write_attr |
919 args['fset'] = write_attr |
928 return property(**args) |
920 return property(**args) |
929 |
921 |
930 |
922 |
931 class Timestamp(object): |
|
932 |
|
933 def __init__(self): |
|
934 self.value = time() |
|
935 |
|
936 def touch(self): |
|
937 self.value = time() |
|
938 |
|
939 def __float__(self): |
|
940 return float(self.value) |
|
941 |
|
942 |
|
943 class Session(object): |
923 class Session(object): |
944 """Repository user session |
924 """Repository user session |
945 |
925 |
946 This ties all together: |
926 This ties all together: |
947 * session id, |
927 * session id, |
951 |
931 |
952 def __init__(self, user, repo, _id=None): |
932 def __init__(self, user, repo, _id=None): |
953 self.sessionid = _id or make_uid(unormalize(user.login)) |
933 self.sessionid = _id or make_uid(unormalize(user.login)) |
954 self.user = user # XXX repoapi: deprecated and store only a login. |
934 self.user = user # XXX repoapi: deprecated and store only a login. |
955 self.repo = repo |
935 self.repo = repo |
956 self._timestamp = Timestamp() |
|
957 self.data = {} |
936 self.data = {} |
958 |
937 |
959 def __unicode__(self): |
938 def __unicode__(self): |
960 return '<session %s (%s 0x%x)>' % ( |
939 return '<session %s (%s 0x%x)>' % ( |
961 unicode(self.user.login), self.sessionid, id(self)) |
940 unicode(self.user.login), self.sessionid, id(self)) |
962 |
|
963 @property |
|
964 def timestamp(self): |
|
965 return float(self._timestamp) |
|
966 |
941 |
967 @property |
942 @property |
968 @deprecated('[3.19] session.id is deprecated, use session.sessionid') |
943 @deprecated('[3.19] session.id is deprecated, use session.sessionid') |
969 def id(self): |
944 def id(self): |
970 return self.sessionid |
945 return self.sessionid |
984 def get_option_value(self, option, foreid=None): |
959 def get_option_value(self, option, foreid=None): |
985 if foreid is not None: |
960 if foreid is not None: |
986 warn('[3.19] foreid argument is deprecated', DeprecationWarning, |
961 warn('[3.19] foreid argument is deprecated', DeprecationWarning, |
987 stacklevel=2) |
962 stacklevel=2) |
988 return self.repo.get_option_value(option) |
963 return self.repo.get_option_value(option) |
989 |
|
990 def _touch(self): |
|
991 """update latest session usage timestamp and reset mode to read""" |
|
992 self._timestamp.touch() |
|
993 |
|
994 local_perm_cache = cnx_attr('local_perm_cache') |
|
995 |
|
996 @local_perm_cache.setter |
|
997 def local_perm_cache(self, value): |
|
998 # base class assign an empty dict:-( |
|
999 assert value == {} |
|
1000 pass |
|
1001 |
964 |
1002 # deprecated ############################################################### |
965 # deprecated ############################################################### |
1003 |
966 |
1004 @property |
967 @property |
1005 def anonymous_session(self): |
968 def anonymous_session(self): |