server/session.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Wed, 15 May 2013 16:09:31 +0200
changeset 9076 7743e7b29635
parent 9075 8d36838ccb3e
child 9077 55a88fbfd39c
permissions -rw-r--r--
[connection] give access to session execute from connection This allows multiple function that both requires an execute method and are required by the execute method.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8096
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5226
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5701
41119f034735 [querier] fix rset description bug with some union queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""Repository users' and internal' sessions."""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
import threading
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from time import time
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
    24
from uuid import uuid4
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
    25
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2466
diff changeset
    27
from logilab.common.deprecation import deprecated
7769
8af09eeee130 [session] take care of non-ascii characters in login and session id (closes: #1910849)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7757
diff changeset
    28
from logilab.common.textutils import unormalize
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8096
diff changeset
    29
from logilab.common.registry import objectify_predicate
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
    31
from cubicweb import UnknownEid, QueryError, schema, server
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
    32
from cubicweb.req import RequestSessionBase
8542
7e264ce34cd4 [session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8538
diff changeset
    33
from cubicweb.utils import make_uid
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3198
diff changeset
    34
from cubicweb.rqlrewrite import RQLRewriter
7573
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7526
diff changeset
    35
from cubicweb.server import ShuttingDown
7119
8b29c4c2ffc6 [repository] fix crash in optimized [add|remove]_relation w/ inlined relation (broken by cw 3.10 refactoring)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7118
diff changeset
    36
from cubicweb.server.edition import EditedEntity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
7329
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    38
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
    39
NO_UNDO_TYPES = schema.SCHEMA_TYPES.copy()
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
    40
NO_UNDO_TYPES.add('CWCache')
5074
bc481dab93d4 [undo] consistent is/is_instance_of processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
    41
# is / is_instance_of are usually added by sql hooks except when using
bc481dab93d4 [undo] consistent is/is_instance_of processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
    42
# dataimport.NoHookRQLObjectStore, and we don't want to record them
bc481dab93d4 [undo] consistent is/is_instance_of processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
    43
# anyway in the later case
bc481dab93d4 [undo] consistent is/is_instance_of processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
    44
NO_UNDO_TYPES.add('is')
bc481dab93d4 [undo] consistent is/is_instance_of processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5069
diff changeset
    45
NO_UNDO_TYPES.add('is_instance_of')
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    46
NO_UNDO_TYPES.add('cw_source')
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
    47
# XXX rememberme,forgotpwd,apycot,vcsfile
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
    48
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8096
diff changeset
    49
@objectify_predicate
7329
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    50
def is_user_session(cls, req, **kwargs):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    51
    """return 1 when session is not internal.
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    52
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    53
    This predicate can only be used repository side only. """
7330
584907154ce3 [session selector] missing not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7329
diff changeset
    54
    return not req.is_internal_session
7329
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    55
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8096
diff changeset
    56
@objectify_predicate
7329
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    57
def is_internal_session(cls, req, **kwargs):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    58
    """return 1 when session is not internal.
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    59
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    60
    This predicate can only be used repository side only. """
7329
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    61
    return req.is_internal_session
f2d52aa8bcdb [session] new selectors according to session type (eg user or internal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7237
diff changeset
    62
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8096
diff changeset
    63
@objectify_predicate
7500
cb0f4da64e86 [repository] new hook selector according to configuration's repairing flag
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7451
diff changeset
    64
def repairing(cls, req, **kwargs):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    65
    """return 1 when repository is running in repair mode"""
7500
cb0f4da64e86 [repository] new hook selector according to configuration's repairing flag
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7451
diff changeset
    66
    return req.vreg.config.repairing
cb0f4da64e86 [repository] new hook selector according to configuration's repairing flag
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7451
diff changeset
    67
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
7451
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    69
class transaction(object):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    70
    """Ensure that the transaction is either commited or rollbacked at exit
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    71
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    72
    Context manager to enter a transaction for a session: when exiting the
7451
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    73
    `with` block on exception, call `session.rollback()`, else call
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    74
    `session.commit()` on normal exit
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    75
    """
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    76
    def __init__(self, session, free_cnxset=True):
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    77
        self.session = session
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    78
        self.free_cnxset = free_cnxset
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    79
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    80
    def __enter__(self):
8529
1daea1f433c9 [datafeed] make cnxset handling of datafeed source more robust
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8433
diff changeset
    81
        # ensure session has a cnxset
1daea1f433c9 [datafeed] make cnxset handling of datafeed source more robust
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8433
diff changeset
    82
        self.session.set_cnxset()
7451
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    83
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    84
    def __exit__(self, exctype, exc, traceback):
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    85
        if exctype:
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    86
            self.session.rollback(free_cnxset=self.free_cnxset)
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    87
        else:
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    88
            self.session.commit(free_cnxset=self.free_cnxset)
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    89
8840
bd5b5759c9b3 [session] make hook_control API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8817
diff changeset
    90
@deprecated('[3.17] use <object>.allow/deny_all_hooks_but instead')
8915
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    91
def hooks_control(obj, mode, *categories):
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    92
    assert mode in  (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    93
    if mode == HOOKS_ALLOW_ALL:
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    94
        return obj.allow_all_hooks_but(*categories)
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    95
    elif mode == HOOKS_DENY_ALL:
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    96
        return obj.deny_all_hooks_but(*categories)
c5c6da8e42db [session] fix hooks_control backward compat
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8848
diff changeset
    97
7451
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
    98
8840
bd5b5759c9b3 [session] make hook_control API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8817
diff changeset
    99
class _hooks_control(object):
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   100
    """context manager to control activated hooks categories.
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   101
9025
768eb9a6a2db [session] update _hook_control docstring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9024
diff changeset
   102
    If mode is`HOOKS_DENY_ALL`, given hooks categories will
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   103
    be enabled.
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   104
9025
768eb9a6a2db [session] update _hook_control docstring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9024
diff changeset
   105
    If mode is `HOOKS_ALLOW_ALL`, given hooks categories will
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   106
    be disabled.
6147
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   107
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   108
    .. sourcecode:: python
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   109
9025
768eb9a6a2db [session] update _hook_control docstring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9024
diff changeset
   110
       with _hooks_control(cnx, HOOKS_ALLOW_ALL, 'integrity'):
6147
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   111
           # ... do stuff with all but 'integrity' hooks activated
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   112
9025
768eb9a6a2db [session] update _hook_control docstring
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9024
diff changeset
   113
       with _hooks_control(cnx, HOOKS_DENY_ALL, 'integrity'):
6147
95c604ec89bf update documentation to follow 6142:8bc6eac1fac1 changes. Try to make it better and move most doc with code on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142
diff changeset
   114
           # ... do stuff with none but 'integrity' hooks activated
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
   115
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
   116
    This is an internal api, you should rather use
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   117
    :meth:`~cubicweb.server.session.Connection.deny_all_hooks_but` or
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   118
    :meth:`~cubicweb.server.session.Connection.allow_all_hooks_but`
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   119
    Transaction methods.
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   120
    """
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   121
    def __init__(self, cnx, mode, *categories):
8814
37fd037c0526 [session] move context manager logic in the hook controls context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8813
diff changeset
   122
        assert mode in (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   123
        self.cnx = cnx
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   124
        self.mode = mode
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   125
        self.categories = categories
8814
37fd037c0526 [session] move context manager logic in the hook controls context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8813
diff changeset
   126
        self.oldmode = None
37fd037c0526 [session] move context manager logic in the hook controls context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8813
diff changeset
   127
        self.changes = ()
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   128
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   129
    def __enter__(self):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   130
        self.oldmode = self.cnx.hooks_mode
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   131
        self.cnx.hooks_mode = self.mode
8814
37fd037c0526 [session] move context manager logic in the hook controls context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8813
diff changeset
   132
        if self.mode is HOOKS_DENY_ALL:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   133
            self.changes = self.cnx.enable_hook_categories(*self.categories)
8814
37fd037c0526 [session] move context manager logic in the hook controls context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8813
diff changeset
   134
        else:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   135
            self.changes = self.cnx.disable_hook_categories(*self.categories)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   136
        self.cnx.ctx_count += 1
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   137
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   138
    def __exit__(self, exctype, exc, traceback):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   139
        self.cnx.ctx_count -= 1
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   140
        try:
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   141
            if self.categories:
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   142
                if self.mode is HOOKS_DENY_ALL:
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   143
                    self.cnx.disable_hook_categories(*self.categories)
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   144
                else:
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   145
                    self.cnx.enable_hook_categories(*self.categories)
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   146
        finally:
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   147
            self.cnx.hooks_mode = self.oldmode
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   148
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   149
class _session_hooks_control(_hooks_control):
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   150
    """hook control context manager for session
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   151
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   152
    Necessary to handle some unholy transaction scope logic."""
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   153
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   154
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   155
    def __init__(self, session, mode, *categories):
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   156
        self.session = session
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   157
        super_init = super(_session_hooks_control, self).__init__
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   158
        return super_init(session._cnx, mode, *categories)
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   159
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   160
    def __exit__(self, exctype, exc, traceback):
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   161
        super_exit = super(_session_hooks_control, self).__exit__
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   162
        ret = super_exit(exctype, exc, traceback)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   163
        if self.cnx.ctx_count == 0:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   164
            self.session._clear_thread_storage(self.cnx)
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   165
        return ret
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
   166
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
   167
@deprecated('[3.17] use <object>.security_enabled instead')
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
   168
def security_enabled(obj, *args, **kwargs):
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
   169
    return obj.security_enabled(*args, **kwargs)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
   170
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
   171
class _security_enabled(object):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   172
    """context manager to control security w/ session.execute,
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   173
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   174
    By default security is disabled on queries executed on the repository
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   175
    side.
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   176
    """
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   177
    def __init__(self, cnx, read=None, write=None):
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   178
        self.cnx = cnx
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   179
        self.read = read
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   180
        self.write = write
8816
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   181
        self.oldread = None
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   182
        self.oldwrite = None
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   183
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   184
    def __enter__(self):
8816
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   185
        if self.read is None:
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   186
            self.oldread = None
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   187
        else:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   188
            self.oldread = self.cnx.read_security
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   189
            self.cnx.read_security = self.read
8816
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   190
        if self.write is None:
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   191
            self.oldwrite = None
fe321c0f6d13 [session] move context manager logic in the security context manager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8815
diff changeset
   192
        else:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   193
            self.oldwrite = self.cnx.write_security
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   194
            self.cnx.write_security = self.write
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   195
        self.cnx.ctx_count += 1
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   196
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   197
    def __exit__(self, exctype, exc, traceback):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   198
        self.cnx.ctx_count -= 1
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   199
        if self.oldread is not None:
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   200
            self.cnx.read_security = self.oldread
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   201
        if self.oldwrite is not None:
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   202
            self.cnx.write_security = self.oldwrite
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   203
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   204
class _session_security_enabled(_security_enabled):
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   205
    """hook security context manager for session
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   206
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   207
    Necessary To handle some unholy transaction scope logic."""
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   208
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   209
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   210
    def __init__(self, session, read=None, write=None):
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   211
        self.session = session
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   212
        super_init = super(_session_security_enabled, self).__init__
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   213
        return super_init(session._cnx, read=read, write=write)
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   214
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   215
    def __exit__(self, exctype, exc, traceback):
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   216
        super_exit = super(_session_security_enabled, self).__exit__
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   217
        ret = super_exit(exctype, exc, traceback)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   218
        if self.cnx.ctx_count == 0:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   219
            self.session._clear_thread_storage(self.cnx)
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   220
        return ret
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   221
8765
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
   222
HOOKS_ALLOW_ALL = object()
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
   223
HOOKS_DENY_ALL = object()
8767
a75670ef2d87 [session] move security constant out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8766
diff changeset
   224
DEFAULT_SECURITY = object() # evaluated to true by design
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   225
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   226
class SessionClosedError(RuntimeError):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   227
    pass
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   228
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   229
class CnxSetTracker(object):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   230
    """Keep track of which connection use which cnxset.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   231
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   232
    There should be one of this object per session plus one another for
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   233
    internal session.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   234
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   235
    Session object are responsible of creating their CnxSetTracker object.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   236
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   237
    Connection should use the :meth:`record` and :meth:`forget` to inform the
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   238
    tracker of cnxset they have acquired.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   239
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   240
    .. automethod:: cubicweb.server.session.CnxSetTracker.record
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   241
    .. automethod:: cubicweb.server.session.CnxSetTracker.forget
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   242
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   243
    Session use the :meth:`close` and :meth:`wait` method when closing.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   244
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   245
    .. automethod:: cubicweb.server.session.CnxSetTracker.close
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   246
    .. automethod:: cubicweb.server.session.CnxSetTracker.wait
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   247
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   248
    This object itself is threadsafe. It also requires caller to acquired its
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   249
    lock in some situation.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   250
    """
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   251
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   252
    def __init__(self):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   253
        self._active = True
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   254
        self._condition = threading.Condition()
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   255
        self._record = {}
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   256
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   257
    def __enter__(self):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   258
        self._condition.__enter__()
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   259
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   260
    def __exit__(self, *args):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   261
        self._condition.__exit__(*args)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   262
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   263
    def record(self, cnxid, cnxset):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   264
        """Inform the tracker that a cnxid have acquired a cnxset
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   265
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   266
        This methode is to be used by Connection object.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   267
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   268
        This method fails when:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   269
        - The cnxid already have a recorded cnxset.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   270
        - The tracker is not active anymore.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   271
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   272
        Notes about the caller:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   273
        (1) It is responsible for retrieving a cnxset.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   274
        (2) It must be prepared to release the cnxset if the
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   275
            `cnxsettracker.forget` call fails.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   276
        (3) It should acquire the tracker lock until the very end of the operation.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   277
        (4) However It take care to lock the CnxSetTracker object after having
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   278
            retrieved the cnxset to prevent deadlock.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   279
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   280
        A typical usage look like::
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   281
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   282
        cnxset = repo._get_cnxset() # (1)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   283
        try:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   284
            with cnxset_tracker: # (3) and (4)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   285
                cnxset_tracker.record(caller.id, cnxset)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   286
                # (3') operation ends when caller is in expected state only
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   287
                caller.cnxset = cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   288
        except Exception:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   289
            repo._free_cnxset(cnxset) # (2)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   290
            raise
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   291
        """
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   292
        # dubious since the caller is suppose to have acquired it anyway.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   293
        with self._condition:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   294
            if not self._active:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   295
                raise SessionClosedError('Closed')
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   296
            old = self._record.get(cnxid)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   297
            if old is not None:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   298
                raise ValueError('"%s" already have a cnx_set (%r)'
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   299
                                 % (cnxid, old))
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   300
            self._record[cnxid] = cnxset
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   301
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   302
    def forget(self, cnxid, cnxset):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   303
        """Inform the tracker that a cnxid have release a cnxset
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   304
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   305
        This methode is to be used by Connection object.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   306
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   307
        This method fails when:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   308
        - The cnxset for the cnxid does not match the recorded one.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   309
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   310
        Notes about the caller:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   311
        (1) It is responsible for releasing the cnxset.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   312
        (2) It should acquire the tracker lock during the operation to ensure
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   313
            the internal tracker state is always accurate regarding its own state.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   314
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   315
        A typical usage look like::
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   316
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   317
        cnxset = caller.cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   318
        try:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   319
            with cnxset_tracker:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   320
                # (2) you can not have caller.cnxset out of sync with
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   321
                #     cnxset_tracker state while unlocked
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   322
                caller.cnxset = None
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   323
                cnxset_tracker.forget(caller.id, cnxset)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   324
        finally:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   325
            cnxset = repo._free_cnxset(cnxset) # (1)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   326
        """
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   327
        with self._condition:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   328
            old = self._record.get(cnxid, None)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   329
            if old is not cnxset:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   330
                raise ValueError('recorded cnxset for "%s" mismatch: %r != %r'
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   331
                                 % (cnxid, old, cnxset))
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   332
            self._record.pop(cnxid)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   333
            self._condition.notify_all()
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   334
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   335
    def close(self):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   336
        """Marks the tracker as inactive.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   337
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   338
        This methode is to be used by Session object.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   339
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   340
        Inactive tracker does not accept new record anymore.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   341
        """
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   342
        with self._condition:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   343
            self._active = False
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   344
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   345
    def wait(self, timeout=10):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   346
        """Wait for all recorded cnxset to be released
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   347
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   348
        This methode is to be used by Session object.
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   349
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   350
        returns a tuple of connection id that remains open.
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   351
        """
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   352
        with self._condition:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   353
            if  self._active:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   354
                raise RuntimeError('Cannot wait on active tracker.'
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   355
                                   ' Call tracker.close() first')
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   356
            while self._record and timeout > 0:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   357
                start = time()
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   358
                self._condition.wait(timeout)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   359
                timeout -= time() - start
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   360
            return tuple(self._record)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   361
9073
9574df1cd054 [Connection] inherit from RequestSessionBase
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   362
class Connection(RequestSessionBase):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   363
    """Repository Connection
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   364
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   365
    Holds all connection related data
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   366
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   367
    Database connections resource:
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   368
8805
d91285fe7242 [transaction] initialize dbapi_request in __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8790
diff changeset
   369
      :attr:`running_dbapi_query`, boolean flag telling if the executing query
d91285fe7242 [transaction] initialize dbapi_request in __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8790
diff changeset
   370
      is coming from a dbapi connection or is a query from within the repository
d91285fe7242 [transaction] initialize dbapi_request in __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8790
diff changeset
   371
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   372
      :attr:`cnxset`, the connections set to use to execute queries on sources.
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   373
      If the transaction is read only, the connection set may be freed between
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   374
      actual query. This allows multiple connection with a reasonable low
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   375
      connection set pool size. control mechanism is detailed below
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   376
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   377
    .. automethod:: cubicweb.server.session.Connection.set_cnxset
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   378
    .. automethod:: cubicweb.server.session.Connection.free_cnxset
8843
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   379
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   380
      :attr:`mode`, string telling the connections set handling mode, may be one
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   381
      of 'read' (connections set may be freed), 'write' (some write was done in
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   382
      the connections set, it can't be freed before end of the transaction),
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   383
      'transaction' (we want to keep the connections set during all the
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   384
      transaction, with or without writing)
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   385
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   386
    Internal transaction data:
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   387
8779
9b2f68916474 [transaction] rename transaction_data to data
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8778
diff changeset
   388
      :attr:`data`,is a dictionary containing some shared data
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   389
      cleared at the end of the transaction. Hooks and operations may put
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   390
      arbitrary data in there, and this may also be used as a communication
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   391
      channel between the client and the repository.
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   392
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   393
      :attr:`pending_operations`, ordered list of operations to be processed on
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   394
      commit/rollback
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   395
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   396
      :attr:`commit_state`, describing the transaction commit state, may be one
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   397
      of None (not yet committing), 'precommit' (calling precommit event on
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   398
      operations), 'postcommit' (calling postcommit event on operations),
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   399
      'uncommitable' (some :exc:`ValidationError` or :exc:`Unauthorized` error
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   400
      has been raised during the transaction and so it must be rollbacked).
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   401
8766
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   402
    Hooks controls:
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   403
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   404
      :attr:`hooks_mode`, may be either `HOOKS_ALLOW_ALL` or `HOOKS_DENY_ALL`.
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   405
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   406
      :attr:`enabled_hook_cats`, when :attr:`hooks_mode` is
8766
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   407
      `HOOKS_DENY_ALL`, this set contains hooks categories that are enabled.
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   408
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   409
      :attr:`disabled_hook_cats`, when :attr:`hooks_mode` is
8766
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   410
      `HOOKS_ALLOW_ALL`, this set contains hooks categories that are disabled.
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   411
8768
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   412
    Security level Management:
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   413
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   414
      :attr:`read_security` and :attr:`write_security`, boolean flags telling if
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   415
      read/write security is currently activated.
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   416
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   417
    """
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   418
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   419
    def __init__(self, cnxid, session, rewriter):
9073
9574df1cd054 [Connection] inherit from RequestSessionBase
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   420
        # using super(Connection, self) confuse some test hack
9574df1cd054 [Connection] inherit from RequestSessionBase
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   421
        RequestSessionBase.__init__(self, session.vreg)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   422
        #: connection unique id
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   423
        self.connectionid = cnxid
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   424
        #: reentrance handling
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
   425
        self.ctx_count = 0
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
   426
8842
63fa6b02b241 [transaction] keep a reference to the repo object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8841
diff changeset
   427
        #: server.Repository object
63fa6b02b241 [transaction] keep a reference to the repo object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8841
diff changeset
   428
        self.repo = session.repo
8845
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   429
        self.vreg = self.repo.vreg
8842
63fa6b02b241 [transaction] keep a reference to the repo object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8841
diff changeset
   430
9074
0616f7a713c6 [connection] initialize connection.user and connection.lang
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9073
diff changeset
   431
        # other session utility
0616f7a713c6 [connection] initialize connection.user and connection.lang
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9073
diff changeset
   432
        self.user = session.user # XXX migrate to self._set_user when
0616f7a713c6 [connection] initialize connection.user and connection.lang
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9073
diff changeset
   433
        self.lang = session.lang # Connection gain execute
9076
7743e7b29635 [connection] give access to session execute from connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9075
diff changeset
   434
        self.execute = session.execute # temporary hack until all necessary
7743e7b29635 [connection] give access to session execute from connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9075
diff changeset
   435
                                       # function have been migrated. Most of
7743e7b29635 [connection] give access to session execute from connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9075
diff changeset
   436
                                       # those function requires an execute
7743e7b29635 [connection] give access to session execute from connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9075
diff changeset
   437
                                       # method and execute requires them too.
9074
0616f7a713c6 [connection] initialize connection.user and connection.lang
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9073
diff changeset
   438
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   439
        #: connection handling mode
8810
1a25bdd49f9a [session/transaction] pass the session object to the Transaction __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8809
diff changeset
   440
        self.mode = session.default_mode
8762
6b397a0ba1d6 [transaction] explicit Transaction cnxset attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8761
diff changeset
   441
        #: connection set used to execute queries on sources
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   442
        self._cnxset = None
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   443
        #: CnxSetTracker used to report cnxset usage
8810
1a25bdd49f9a [session/transaction] pass the session object to the Transaction __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8809
diff changeset
   444
        self._cnxset_tracker = session._cnxset_tracker
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   445
        #: is this connection from a client or internal to the repo
8805
d91285fe7242 [transaction] initialize dbapi_request in __init__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8790
diff changeset
   446
        self.running_dbapi_query = True
9023
67f242261dd3 [connection] give access to is_internal_session boolean
pierre-yves
parents: 9022
diff changeset
   447
        # internal (root) session
67f242261dd3 [connection] give access to is_internal_session boolean
pierre-yves
parents: 9022
diff changeset
   448
        self.is_internal_session = session.is_internal_session
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   449
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   450
        #: dict containing arbitrary data cleared at the end of the transaction
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   451
        self.transaction_data = {}
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   452
        #: ordered list of operations to be processed on commit/rollback
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   453
        self.pending_operations = []
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   454
        #: (None, 'precommit', 'postcommit', 'uncommitable')
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   455
        self.commit_state = None
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   456
8766
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   457
        ### hook control attribute
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   458
        self.hooks_mode = HOOKS_ALLOW_ALL
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   459
        self.disabled_hook_cats = set()
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   460
        self.enabled_hook_cats = set()
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   461
        self.pruned_hooks_cache = {}
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   462
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   463
8768
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   464
        ### security control attributes
8806
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   465
        self._read_security = DEFAULT_SECURITY # handled by a property
8768
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   466
        self.write_security = DEFAULT_SECURITY
3d105e270abc [transaction] initialize security control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8767
diff changeset
   467
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   468
        # undo control
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   469
        config = session.repo.config
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   470
        if config.creating or config.repairing or session.is_internal_session:
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   471
            self.undo_actions = False
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   472
        else:
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   473
            self.undo_actions = config['undo-enabled']
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   474
8769
1672502ac204 [transaction] move RQLRewriter in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8768
diff changeset
   475
        # RQLRewriter are not thread safe
1672502ac204 [transaction] move RQLRewriter in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8768
diff changeset
   476
        self._rewriter = rewriter
1672502ac204 [transaction] move RQLRewriter in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8768
diff changeset
   477
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   478
    def clear(self):
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   479
        """reset internal data"""
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   480
        self.transaction_data = {}
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   481
        #: ordered list of operations to be processed on commit/rollback
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   482
        self.pending_operations = []
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   483
        #: (None, 'precommit', 'postcommit', 'uncommitable')
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   484
        self.commit_state = None
8766
db80ffb2f71c [transaction] initialize hook control attribute in Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8765
diff changeset
   485
        self.pruned_hooks_cache = {}
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   486
    # Connection Set Management ###############################################
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   487
    @property
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   488
    def cnxset(self):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   489
        return self._cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   490
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   491
    @cnxset.setter
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   492
    def cnxset(self, new_cnxset):
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   493
        with self._cnxset_tracker:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   494
            old_cnxset = self._cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   495
            if new_cnxset is old_cnxset:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   496
                return #nothing to do
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   497
            if old_cnxset is not None:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   498
                self._cnxset = None
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   499
                self.ctx_count -= 1
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   500
                self._cnxset_tracker.forget(self.connectionid, old_cnxset)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   501
            if new_cnxset is not None:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   502
                self._cnxset_tracker.record(self.connectionid, new_cnxset)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   503
                self._cnxset = new_cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   504
                self.ctx_count += 1
8764
c4f022a6c7dd [transaction] initialize transaction data and state related attribute.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8763
diff changeset
   505
8843
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   506
    def set_cnxset(self):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   507
        """the connection need a connections set to execute some queries"""
8843
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   508
        if self.cnxset is None:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   509
            cnxset = self.repo._get_cnxset()
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   510
            try:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   511
                self.cnxset = cnxset
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   512
                try:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   513
                    cnxset.cnxset_set()
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   514
                except:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   515
                    self.cnxset = None
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   516
                    raise
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   517
            except:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   518
                self.repo._free_cnxset(cnxset)
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   519
                raise
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   520
        return self.cnxset
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   521
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   522
    def free_cnxset(self, ignoremode=False):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   523
        """the connection is no longer using its connections set, at least for some time"""
8843
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   524
        # cnxset may be none if no operation has been done since last commit
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   525
        # or rollback
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   526
        cnxset = self.cnxset
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   527
        if cnxset is not None and (ignoremode or self.mode == 'read'):
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   528
            try:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   529
                self.cnxset = None
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   530
            finally:
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   531
                cnxset.cnxset_freed()
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   532
                self.repo._free_cnxset(cnxset)
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   533
ea05b8545dd8 [session/transaction] handle cnxset repository logic in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8842
diff changeset
   534
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   535
    # Entity cache management #################################################
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   536
    #
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   537
    # The connection entity cache as held in cnx.transaction_data it is removed at end the
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   538
    # end of the connection (commit and rollback)
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   539
    #
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   540
    # XXX connection level caching may be a pb with multiple repository
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   541
    # instances, but 1. this is probably not the only one :$ and 2. it may be
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   542
    # an acceptable risk. Anyway we could activate it or not according to a
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   543
    # configuration option
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   544
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   545
    def set_entity_cache(self, entity):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   546
        """Add `entity` to the connection entity cache"""
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   547
        ecache = self.transaction_data.setdefault('ecache', {})
8784
07f453bf72e8 [transaction] small simplification in ecache code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8783
diff changeset
   548
        ecache.setdefault(entity.eid, entity)
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   549
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   550
    def entity_cache(self, eid):
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   551
        """get cache entity for `eid`"""
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   552
        return self.transaction_data['ecache'][eid]
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   553
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   554
    def cached_entities(self):
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   555
        """return the whole entity cache"""
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   556
        return self.transaction_data.get('ecache', {}).values()
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   557
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   558
    def drop_entity_cache(self, eid=None):
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   559
        """drop entity from the cache
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   560
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   561
        If eid is None, the whole cache is dropped"""
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   562
        if eid is None:
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   563
            self.transaction_data.pop('ecache', None)
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   564
        else:
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   565
            del self.transaction_data['ecache'][eid]
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   566
8785
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   567
    # Tracking of entity added of removed in the transaction ##################
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   568
    #
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   569
    # Those are function to  allows cheap call from client in other process.
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   570
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   571
    def deleted_in_transaction(self, eid):
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   572
        """return True if the entity of the given eid is being deleted in the
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   573
        current transaction
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   574
        """
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   575
        return eid in self.transaction_data.get('pendingeids', ())
8785
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   576
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   577
    def added_in_transaction(self, eid):
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   578
        """return True if the entity of the given eid is being created in the
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   579
        current transaction
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   580
        """
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   581
        return eid in self.transaction_data.get('neweids', ())
8785
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   582
8786
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   583
    # Operation management ####################################################
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   584
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   585
    def add_operation(self, operation, index=None):
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   586
        """add an operation to be executed at the end of the transaction"""
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   587
        if index is None:
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   588
            self.pending_operations.append(operation)
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   589
        else:
c2bc0b804982 [session/transaction] move add operation in session code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8785
diff changeset
   590
            self.pending_operations.insert(index, operation)
8785
8f2786492369 [session/transaction] move entity life utility on transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8784
diff changeset
   591
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   592
    # Hooks control ###########################################################
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   593
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   594
    def allow_all_hooks_but(self, *categories):
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   595
        return _hooks_control(self, HOOKS_ALLOW_ALL, *categories)
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   596
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   597
    def deny_all_hooks_but(self, *categories):
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   598
        return _hooks_control(self, HOOKS_DENY_ALL, *categories)
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
   599
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   600
    def disable_hook_categories(self, *categories):
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   601
        """disable the given hook categories:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   602
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   603
        - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   604
        - on HOOKS_ALLOW_ALL mode, ensure those categories are disabled
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   605
        """
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   606
        changes = set()
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   607
        self.pruned_hooks_cache.clear()
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   608
        categories = set(categories)
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   609
        if self.hooks_mode is HOOKS_DENY_ALL:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   610
            enabledcats = self.enabled_hook_cats
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   611
            changes = enabledcats & categories
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   612
            enabledcats -= changes # changes is small hence faster
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   613
        else:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   614
            disabledcats = self.disabled_hook_cats
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   615
            changes = categories - disabledcats
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   616
            disabledcats |= changes # changes is small hence faster
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   617
        return tuple(changes)
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   618
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   619
    def enable_hook_categories(self, *categories):
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   620
        """enable the given hook categories:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   621
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   622
        - on HOOKS_DENY_ALL mode, ensure those categories are enabled
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   623
        - on HOOKS_ALLOW_ALL mode, ensure those categories are not disabled
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   624
        """
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   625
        changes = set()
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   626
        self.pruned_hooks_cache.clear()
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   627
        categories = set(categories)
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   628
        if self.hooks_mode is HOOKS_DENY_ALL:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   629
            enabledcats = self.enabled_hook_cats
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   630
            changes = categories - enabledcats
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   631
            enabledcats |= changes # changes is small hence faster
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   632
        else:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   633
            disabledcats = self.disabled_hook_cats
8789
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   634
            changes = disabledcats & categories
465a87e6a89a [transaction] use set operation in the hook control code
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8788
diff changeset
   635
            disabledcats -= changes # changes is small hence faster
8788
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   636
        return tuple(changes)
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   637
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   638
    def is_hook_category_activated(self, category):
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   639
        """return a boolean telling if the given category is currently activated
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   640
        or not
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   641
        """
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   642
        if self.hooks_mode is HOOKS_DENY_ALL:
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   643
            return category in self.enabled_hook_cats
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   644
        return category not in self.disabled_hook_cats
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   645
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   646
    def is_hook_activated(self, hook):
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   647
        """return a boolean telling if the given hook class is currently
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   648
        activated or not
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   649
        """
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   650
        return self.is_hook_category_activated(hook.category)
194f1418bc7e [session/transaction] move hook management in transaction itself
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8787
diff changeset
   651
8806
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   652
    # Security management #####################################################
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   653
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   654
    def security_enabled(self, read=None, write=None):
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   655
        return _security_enabled(self, read=read, write=write)
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
   656
8806
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   657
    @property
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   658
    def read_security(self):
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   659
        return self._read_security
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   660
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   661
    @read_security.setter
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   662
    def read_security(self, activated):
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   663
        oldmode = self._read_security
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   664
        self._read_security = activated
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   665
        # running_dbapi_query used to detect hooks triggered by a 'dbapi' query
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   666
        # (eg not issued on the session). This is tricky since we the execution
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   667
        # model of a (write) user query is:
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   668
        #
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   669
        # repository.execute (security enabled)
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   670
        #  \-> querier.execute
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   671
        #       \-> repo.glob_xxx (add/update/delete entity/relation)
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   672
        #            \-> deactivate security before calling hooks
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   673
        #                 \-> WE WANT TO CHECK QUERY NATURE HERE
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   674
        #                      \-> potentially, other calls to querier.execute
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   675
        #
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   676
        # so we can't rely on simply checking session.read_security, but
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   677
        # recalling the first transition from DEFAULT_SECURITY to something
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   678
        # else (False actually) is not perfect but should be enough
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   679
        #
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   680
        # also reset running_dbapi_query to true when we go back to
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   681
        # DEFAULT_SECURITY
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   682
        self.running_dbapi_query = (oldmode is DEFAULT_SECURITY
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   683
                                    or activated is DEFAULT_SECURITY)
8ca4b323223c [session/transaction] move read_security subtlety in transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8805
diff changeset
   684
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   685
    # undo support ############################################################
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   686
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   687
    def ertype_supports_undo(self, ertype):
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   688
        return self.undo_actions and ertype not in NO_UNDO_TYPES
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   689
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   690
    def transaction_uuid(self, set=True):
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   691
        uuid = self.transaction_data.get('tx_uuid')
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   692
        if set and uuid is None:
9075
8d36838ccb3e [connection] move last part of undo logic in connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9074
diff changeset
   693
            self.transaction_data['tx_uuid'] = uuid = uuid4().hex
8d36838ccb3e [connection] move last part of undo logic in connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9074
diff changeset
   694
            self.repo.system_source.start_undoable_transaction(self, uuid)
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   695
        return uuid
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   696
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   697
    def transaction_inc_action_counter(self):
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   698
        num = self.transaction_data.setdefault('tx_action_count', 0) + 1
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
   699
        self.transaction_data['tx_action_count'] = num
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   700
        return num
8844
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   701
    # db-api like interface ###################################################
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   702
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   703
    def source_defs(self):
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   704
        return self.repo.source_defs()
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   705
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   706
    def describe(self, eid, asdict=False):
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   707
        """return a tuple (type, sourceuri, extid) for the entity with id <eid>"""
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   708
        metas = self.repo.type_and_source_from_eid(eid, self)
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   709
        if asdict:
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   710
            return dict(zip(('type', 'source', 'extid', 'asource'), metas))
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   711
       # XXX :-1 for cw compat, use asdict=True for full information
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   712
        return metas[:-1]
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   713
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   714
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   715
    def source_from_eid(self, eid):
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   716
        """return the source where the entity with id <eid> is located"""
168cf5ef21f8 [session/transaction] move multiple utility on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8843
diff changeset
   717
        return self.repo.source_from_eid(eid, self)
8812
52af67a2f0a5 [session/transaction] move most undo support into transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8811
diff changeset
   718
8845
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   719
    # resource accessors ######################################################
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   720
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   721
    def system_sql(self, sql, args=None, rollback_on_failure=True):
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   722
        """return a sql cursor on the system database"""
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   723
        if sql.split(None, 1)[0].upper() != 'SELECT':
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   724
            self.mode = 'write'
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   725
        source = self.cnxset.source('system')
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   726
        try:
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   727
            return source.doexec(self, sql, args, rollback=rollback_on_failure)
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   728
        except (source.OperationalError, source.InterfaceError):
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   729
            if not rollback_on_failure:
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   730
                raise
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   731
            source.warning("trying to reconnect")
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   732
            self.cnxset.reconnect(source)
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   733
            return source.doexec(self, sql, args, rollback=rollback_on_failure)
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   734
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   735
    def rtype_eids_rdef(self, rtype, eidfrom, eidto):
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   736
        # use type_and_source_from_eid instead of type_from_eid for optimization
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   737
        # (avoid two extra methods call)
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   738
        subjtype = self.repo.type_and_source_from_eid(eidfrom, self)[0]
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   739
        objtype = self.repo.type_and_source_from_eid(eidto, self)[0]
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   740
        return self.vreg.schema.rschema(rtype).rdefs[(subjtype, objtype)]
667cc27e9d12 [session/transaction] move resource accessors on Transaction
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8844
diff changeset
   741
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   742
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   743
def cnx_attr(attr_name, writable=False):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   744
    """return a property to forward attribute access to connection.
8782
ee675f0a9612 [session] have a nice helper function to forward access to session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8781
diff changeset
   745
ee675f0a9612 [session] have a nice helper function to forward access to session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8781
diff changeset
   746
    This is to be used by session"""
8787
1b3b7284377f [session] allow writable tx_attr and use it for commit_state
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8786
diff changeset
   747
    args = {}
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   748
    def attr_from_cnx(session):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   749
        return getattr(session._cnx, attr_name)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   750
    args['fget'] = attr_from_cnx
8787
1b3b7284377f [session] allow writable tx_attr and use it for commit_state
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8786
diff changeset
   751
    if writable:
1b3b7284377f [session] allow writable tx_attr and use it for commit_state
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8786
diff changeset
   752
        def write_attr(session, value):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   753
            return setattr(session._cnx, attr_name, value)
8787
1b3b7284377f [session] allow writable tx_attr and use it for commit_state
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8786
diff changeset
   754
        args['fset'] = write_attr
1b3b7284377f [session] allow writable tx_attr and use it for commit_state
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8786
diff changeset
   755
    return property(**args)
8782
ee675f0a9612 [session] have a nice helper function to forward access to session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8781
diff changeset
   756
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   757
def cnx_meth(meth_name):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   758
    """return a function forwarding calls to connection.
8783
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   759
c024365ac8ac [session/transaction] move entity cache management on session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8782
diff changeset
   760
    This is to be used by session"""
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   761
    def meth_from_cnx(session, *args, **kwargs):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   762
        return getattr(session._cnx, meth_name)(*args, **kwargs)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   763
    return meth_from_cnx
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
   764
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
   765
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   766
class Session(RequestSessionBase):
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   767
    """Repository user session
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   768
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   769
    This tie all together:
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   770
     * session id,
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   771
     * user,
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   772
     * connections set,
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   773
     * other session data.
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   774
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   775
    About session storage / transactions
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   776
    ------------------------------------
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   777
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   778
    Here is a description of internal session attributes. Besides :attr:`data`
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   779
    and :attr:`transaction_data`, you should not have to use attributes
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   780
    described here but higher level APIs.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   781
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   782
      :attr:`data` is a dictionary containing shared data, used to communicate
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   783
      extra information between the client and the repository
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   784
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   785
      :attr:`_cnxs` is a dictionary of :class:`Connection` instance, one
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   786
      for each running connection. The key is the connection id. By default
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   787
      the connection id is the thread name but it can be otherwise (per dbapi
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   788
      cursor for instance, or per thread name *from another process*).
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   789
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   790
      :attr:`__threaddata` is a thread local storage whose `cnx` attribute
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   791
      refers to the proper instance of :class:`Connection` according to the
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   792
      connection.
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   793
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   794
    You should not have to use neither :attr:`_cnx` nor :attr:`__threaddata`,
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   795
    simply access connection data transparently through the :attr:`_cnx`
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   796
    property. Also, you usually don't have to access it directly since current
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   797
    connection's data may be accessed/modified through properties / methods:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   798
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   799
      :attr:`connection_data`, similarly to :attr:`data`, is a dictionary
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   800
      containing some shared data that should be cleared at the end of the
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   801
      connection. Hooks and operations may put arbitrary data in there, and
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   802
      this may also be used as a communication channel between the client and
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   803
      the repository.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   804
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   805
    .. automethod:: cubicweb.server.session.Session.get_shared_data
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   806
    .. automethod:: cubicweb.server.session.Session.set_shared_data
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   807
    .. automethod:: cubicweb.server.session.Session.added_in_transaction
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   808
    .. automethod:: cubicweb.server.session.Session.deleted_in_transaction
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   809
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   810
    Connection state information:
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   811
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   812
      :attr:`running_dbapi_query`, boolean flag telling if the executing query
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   813
      is coming from a dbapi connection or is a query from within the repository
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   814
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   815
      :attr:`cnxset`, the connections set to use to execute queries on sources.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   816
      During a transaction, the connection set may be freed so that is may be
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   817
      used by another session as long as no writing is done. This means we can
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   818
      have multiple sessions with a reasonably low connections set pool size.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   819
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   820
    .. automethod:: cubicweb.server.session.set_cnxset
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   821
    .. automethod:: cubicweb.server.session.free_cnxset
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   822
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   823
      :attr:`mode`, string telling the connections set handling mode, may be one
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   824
      of 'read' (connections set may be freed), 'write' (some write was done in
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   825
      the connections set, it can't be freed before end of the transaction),
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   826
      'transaction' (we want to keep the connections set during all the
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   827
      transaction, with or without writing)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   828
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   829
      :attr:`pending_operations`, ordered list of operations to be processed on
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   830
      commit/rollback
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   831
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   832
      :attr:`commit_state`, describing the transaction commit state, may be one
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   833
      of None (not yet committing), 'precommit' (calling precommit event on
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   834
      operations), 'postcommit' (calling postcommit event on operations),
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   835
      'uncommitable' (some :exc:`ValidationError` or :exc:`Unauthorized` error
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   836
      has been raised during the transaction and so it must be rollbacked).
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   837
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   838
    .. automethod:: cubicweb.server.session.Session.commit
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   839
    .. automethod:: cubicweb.server.session.Session.rollback
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   840
    .. automethod:: cubicweb.server.session.Session.close
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   841
    .. automethod:: cubicweb.server.session.Session.closed
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   842
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   843
    Security level Management:
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   844
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   845
      :attr:`read_security` and :attr:`write_security`, boolean flags telling if
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   846
      read/write security is currently activated.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   847
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   848
    .. automethod:: cubicweb.server.session.Session.security_enabled
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   849
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   850
    Hooks Management:
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   851
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   852
      :attr:`hooks_mode`, may be either `HOOKS_ALLOW_ALL` or `HOOKS_DENY_ALL`.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   853
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   854
      :attr:`enabled_hook_categories`, when :attr:`hooks_mode` is
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   855
      `HOOKS_DENY_ALL`, this set contains hooks categories that are enabled.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   856
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   857
      :attr:`disabled_hook_categories`, when :attr:`hooks_mode` is
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   858
      `HOOKS_ALLOW_ALL`, this set contains hooks categories that are disabled.
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   859
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   860
    .. automethod:: cubicweb.server.session.Session.deny_all_hooks_but
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   861
    .. automethod:: cubicweb.server.session.Session.allow_all_hooks_but
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   862
    .. automethod:: cubicweb.server.session.Session.is_hook_category_activated
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   863
    .. automethod:: cubicweb.server.session.Session.is_hook_activated
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
   864
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   865
    Data manipulation:
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   866
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   867
    .. automethod:: cubicweb.server.session.Session.add_relation
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   868
    .. automethod:: cubicweb.server.session.Session.add_relations
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   869
    .. automethod:: cubicweb.server.session.Session.delete_relation
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
   870
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   871
    Other:
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   872
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   873
    .. automethod:: cubicweb.server.session.Session.call_service
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   874
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   875
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   876
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   877
    """
8525
c09feae04094 [entity edition] don't remove values from attribute cache on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8433
diff changeset
   878
    is_request = False
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
   879
    is_internal_session = False
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   880
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   881
    def __init__(self, user, repo, cnxprops=None, _id=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   882
        super(Session, self).__init__(repo.vreg)
7769
8af09eeee130 [session] take care of non-ascii characters in login and session id (closes: #1910849)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7757
diff changeset
   883
        self.id = _id or make_uid(unormalize(user.login).encode('UTF8'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   884
        self.user = user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   885
        self.repo = repo
5792
e13aa4786a72 [session] update session's timestamp in session.execute, so long running transactions are not erroneously closed by the repository
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5791
diff changeset
   886
        self.timestamp = time()
2570
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
   887
        self.default_mode = 'read'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   888
        # short cut to querier .execute method
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   889
        self._execute = repo.querier.execute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   890
        # shared data, used to communicate extra information between the client
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   891
        # and the rql server
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   892
        self.data = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   893
        # i18n initialization
8538
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8535
diff changeset
   894
        self.set_language(user.prefered_language())
8771
519629422391 [session] rename _tx_data into _txs
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8770
diff changeset
   895
        ### internals
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   896
        # Connection of this section
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   897
        self._cnxs = {}
8760
17994bf95d6a [doc] update Session documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
   898
        # Data local to the thread
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   899
        self.__threaddata = threading.local()
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
   900
        self._cnxset_tracker = CnxSetTracker()
1880
293fe4b49e28 two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1660
diff changeset
   901
        self._closed = False
8776
cdb261bd36ac [session] make session lock reentrant
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8775
diff changeset
   902
        self._lock = threading.RLock()
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   903
4703
4e803c30b7db [session] user.login is usually an unicode string, so implements __unicode__ instead of __str__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4533
diff changeset
   904
    def __unicode__(self):
8669
62213a34726e [db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8626
diff changeset
   905
        return '<session %s (%s 0x%x)>' % (
62213a34726e [db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8626
diff changeset
   906
            unicode(self.user.login), self.id, id(self))
2604
6b55a2a81fd8 [R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
   907
9049
9d62d53b49df [server/session] allow access to session id using sessionid
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9048
diff changeset
   908
    @property
9d62d53b49df [server/session] allow access to session id using sessionid
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9048
diff changeset
   909
    def sessionid(self):
9d62d53b49df [server/session] allow access to session id using sessionid
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9048
diff changeset
   910
        return self.id
9d62d53b49df [server/session] allow access to session id using sessionid
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9048
diff changeset
   911
9050
635862b48485 [server/session] add a login property
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9049
diff changeset
   912
    @property
635862b48485 [server/session] add a login property
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9049
diff changeset
   913
    def login(self):
635862b48485 [server/session] add a login property
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9049
diff changeset
   914
        # XXX backward compat with dbapi. deprecate me ASAP.
635862b48485 [server/session] add a login property
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9049
diff changeset
   915
        return self.user.login
635862b48485 [server/session] add a login property
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9049
diff changeset
   916
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   917
    def get_cnx(self, cnxid):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   918
        """return the <cnxid> connection attached to this session
8777
4e72b78ea5aa [session] split session creation from default session assignation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8776
diff changeset
   919
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   920
        Connection is created if necessary"""
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   921
        with self._lock: # no connection exist with the same id
8777
4e72b78ea5aa [session] split session creation from default session assignation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8776
diff changeset
   922
            try:
8996
dcd8aa47adce [session] raise proper exception with get_tx is called on a closed session
pierre-yves
parents: 8934
diff changeset
   923
                if self.closed:
dcd8aa47adce [session] raise proper exception with get_tx is called on a closed session
pierre-yves
parents: 8934
diff changeset
   924
                    raise SessionClosedError('try to access connections set on a closed session %s' % self.id)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   925
                cnx = self._cnxs[cnxid]
8777
4e72b78ea5aa [session] split session creation from default session assignation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8776
diff changeset
   926
            except KeyError:
4e72b78ea5aa [session] split session creation from default session assignation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8776
diff changeset
   927
                rewriter = RQLRewriter(self)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   928
                cnx = Connection(cnxid, self, rewriter)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   929
                self._cnxs[cnxid] = cnx
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   930
        return cnx
8777
4e72b78ea5aa [session] split session creation from default session assignation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8776
diff changeset
   931
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   932
    def set_cnx(self, cnxid=None):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   933
        """set the default connection of the current thread to <cnxid>
8775
3d932eec0bda [session] document set_tx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8774
diff changeset
   934
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   935
        Connection is created if necessary"""
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   936
        if cnxid is None:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   937
            cnxid = threading.currentThread().getName()
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   938
        self.__threaddata.cnx = self.get_cnx(cnxid)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   939
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   940
    @property
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   941
    def _cnx(self):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   942
        """default connection for current session in current thread"""
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   943
        try:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   944
            return self.__threaddata.cnx
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   945
        except AttributeError:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   946
            self.set_cnx()
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
   947
            return self.__threaddata.cnx
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   948
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   949
    @property
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   950
    def _current_cnx_id(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   951
        """TRANSITIONAL PURPOSE"""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   952
        try:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   953
            return self.__threaddata.cnx.transactionid
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   954
        except AttributeError:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   955
            return None
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
   956
8585
3f60f416dddb [dbapi] provide get_option_value over DBAPIRequest (closes #2515522)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8529
diff changeset
   957
    def get_option_value(self, option, foreid=None):
3f60f416dddb [dbapi] provide get_option_value over DBAPIRequest (closes #2515522)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8529
diff changeset
   958
        return self.repo.get_option_value(option, foreid)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
   959
7451
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   960
    def transaction(self, free_cnxset=True):
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   961
        """return context manager to enter a transaction for the session: when
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   962
        exiting the `with` block on exception, call `session.rollback()`, else
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   963
        call `session.commit()` on normal exit.
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   964
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   965
        The `free_cnxset` will be given to rollback/commit methods to indicate
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   966
        wether the connections set should be freed or not.
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   967
        """
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   968
        return transaction(self, free_cnxset)
48ba5f0c11de [session] provide a simple transaction context manager for session. Closes #1725640
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7405
diff changeset
   969
3112
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   970
    def add_relation(self, fromeid, rtype, toeid):
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   971
        """provide direct access to the repository method to add a relation.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   972
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   973
        This is equivalent to the following rql query:
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   974
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   975
          SET X rtype Y WHERE X eid  fromeid, T eid toeid
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   976
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   977
        without read security check but also all the burden of rql execution.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   978
        You may use this in hooks when you know both eids of the relation you
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   979
        want to add.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
   980
        """
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   981
        self.add_relations([(rtype, [(fromeid,  toeid)])])
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   982
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   983
    def add_relations(self, relations):
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   984
        '''set many relation using a shortcut similar to the one in add_relation
7526
ae31063f3274 cleanup and assert we don't import unexpected stuff
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7502
diff changeset
   985
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   986
        relations is a list of 2-uples, the first element of each
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   987
        2-uple is the rtype, and the second is a list of (fromeid,
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   988
        toeid) tuples
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   989
        '''
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   990
        edited_entities = {}
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   991
        relations_dict = {}
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
   992
        with self.security_enabled(False, False):
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   993
            for rtype, eids in relations:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   994
                if self.vreg.schema[rtype].inlined:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   995
                    for fromeid, toeid in eids:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   996
                        if fromeid not in edited_entities:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   997
                            entity = self.entity_from_eid(fromeid)
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   998
                            edited = EditedEntity(entity)
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
   999
                            edited_entities[fromeid] = edited
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1000
                        else:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1001
                            edited = edited_entities[fromeid]
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1002
                        edited.edited_attribute(rtype, toeid)
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1003
                else:
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1004
                    relations_dict[rtype] = eids
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1005
            self.repo.glob_add_relations(self, relations_dict)
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1006
            for edited in edited_entities.itervalues():
7119
8b29c4c2ffc6 [repository] fix crash in optimized [add|remove]_relation w/ inlined relation (broken by cw 3.10 refactoring)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7118
diff changeset
  1007
                self.repo.glob_update_entity(self, edited)
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7119
diff changeset
  1008
3197
b27d19c0db1c [repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3196
diff changeset
  1009
3112
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1010
    def delete_relation(self, fromeid, rtype, toeid):
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1011
        """provide direct access to the repository method to delete a relation.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1012
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1013
        This is equivalent to the following rql query:
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1014
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1015
          DELETE X rtype Y WHERE X eid  fromeid, T eid toeid
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1016
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1017
        without read security check but also all the burden of rql execution.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1018
        You may use this in hooks when you know both eids of the relation you
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1019
        want to delete.
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1020
        """
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
  1021
        with self.security_enabled(False, False):
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1022
            if self.vreg.schema[rtype].inlined:
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1023
                entity = self.entity_from_eid(fromeid)
7119
8b29c4c2ffc6 [repository] fix crash in optimized [add|remove]_relation w/ inlined relation (broken by cw 3.10 refactoring)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7118
diff changeset
  1024
                entity.cw_attr_cache[rtype] = None
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1025
                self.repo.glob_update_entity(self, entity, set((rtype,)))
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1026
            else:
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1027
                self.repo.glob_delete_relation(self, fromeid, rtype, toeid)
3112
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1028
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1029
    # relations cache handling #################################################
873202e181bb enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3074
diff changeset
  1030
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
  1031
    def update_rel_cache_add(self, subject, rtype, object, symmetric=False):
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1032
        self._update_entity_rel_cache_add(subject, rtype, 'subject', object)
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
  1033
        if symmetric:
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1034
            self._update_entity_rel_cache_add(object, rtype, 'subject', subject)
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1035
        else:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1036
            self._update_entity_rel_cache_add(object, rtype, 'object', subject)
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1037
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
  1038
    def update_rel_cache_del(self, subject, rtype, object, symmetric=False):
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1039
        self._update_entity_rel_cache_del(subject, rtype, 'subject', object)
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
  1040
        if symmetric:
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1041
            self._update_entity_rel_cache_del(object, rtype, 'object', object)
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1042
        else:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1043
            self._update_entity_rel_cache_del(object, rtype, 'object', subject)
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1044
3553
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1045
    def _update_entity_rel_cache_add(self, eid, rtype, role, targeteid):
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1046
        try:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1047
            entity = self.entity_cache(eid)
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1048
        except KeyError:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1049
            return
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1050
        rcache = entity.cw_relation_cached(rtype, role)
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1051
        if rcache is not None:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1052
            rset, entities = rcache
3553
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1053
            rset = rset.copy()
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1054
            entities = list(entities)
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1055
            rset.rows.append([targeteid])
2781
4e1ad9d6a3e7 [session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2764
diff changeset
  1056
            if not isinstance(rset.description, list): # else description not set
4e1ad9d6a3e7 [session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2764
diff changeset
  1057
                rset.description = list(rset.description)
4e1ad9d6a3e7 [session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2764
diff changeset
  1058
            rset.description.append([self.describe(targeteid)[0]])
3196
77936fa67ae6 [repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3161
diff changeset
  1059
            targetentity = self.entity_from_eid(targeteid)
3379
9192ba07890d use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3373
diff changeset
  1060
            if targetentity.cw_rset is None:
3373
3cd644bfab12 use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
  1061
                targetentity.cw_rset = rset
3cd644bfab12 use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
  1062
                targetentity.cw_row = rset.rowcount
3cd644bfab12 use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
  1063
                targetentity.cw_col = 0
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1064
            rset.rowcount += 1
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1065
            entities.append(targetentity)
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1066
            entity._cw_related_cache['%s_%s' % (rtype, role)] = (
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1067
                rset, tuple(entities))
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1068
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1069
    def _update_entity_rel_cache_del(self, eid, rtype, role, targeteid):
3553
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1070
        try:
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1071
            entity = self.entity_cache(eid)
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1072
        except KeyError:
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1073
            return
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1074
        rcache = entity.cw_relation_cached(rtype, role)
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1075
        if rcache is not None:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1076
            rset, entities = rcache
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1077
            for idx, row in enumerate(rset.rows):
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1078
                if row[0] == targeteid:
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1079
                    break
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1080
            else:
3074
141cfaac6b97 don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2874
diff changeset
  1081
                # this may occurs if the cache has been filed by a hook
141cfaac6b97 don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2874
diff changeset
  1082
                # after the database update
141cfaac6b97 don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2874
diff changeset
  1083
                self.debug('cache inconsistency for %s %s %s %s', eid, rtype,
141cfaac6b97 don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2874
diff changeset
  1084
                           role, targeteid)
141cfaac6b97 don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2874
diff changeset
  1085
                return
3553
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1086
            rset = rset.copy()
76b4a177a018 [session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
  1087
            entities = list(entities)
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1088
            del rset.rows[idx]
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1089
            if isinstance(rset.description, list): # else description not set
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1090
                del rset.description[idx]
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1091
            del entities[idx]
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1092
            rset.rowcount -= 1
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1093
            entity._cw_related_cache['%s_%s' % (rtype, role)] = (
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
  1094
                rset, tuple(entities))
2647
b0a2e779845c enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2630
diff changeset
  1095
2100
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1096
    # resource accessors ######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1097
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1098
    system_sql = cnx_meth('system_sql')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1099
    deleted_in_transaction = cnx_meth('deleted_in_transaction')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1100
    added_in_transaction = cnx_meth('added_in_transaction')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1101
    rtype_eids_rdef = cnx_meth('rtype_eids_rdef')
2840
06daf13195d4 [hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
  1102
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1103
    # security control #########################################################
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1104
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1105
8562
0d2fb4604265 [session] fix arguments default value and promote usage of security_enabled as session method. Closes #2481820
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
  1106
    def security_enabled(self, read=None, write=None):
9027
b6b96d61e055 [connection] move security control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9026
diff changeset
  1107
        return _session_security_enabled(self, read=read, write=write)
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1108
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1109
    read_security = cnx_attr('read_security', writable=True)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1110
    write_security = cnx_attr('write_security', writable=True)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1111
    running_dbapi_query = cnx_attr('running_dbapi_query')
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1112
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
  1113
    # hooks activation control #################################################
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
  1114
    # all hooks should be activated during normal execution
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
  1115
7405
8c752d113ebb [session] new methods on session to ease access to security/hooks control context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7340
diff changeset
  1116
    def allow_all_hooks_but(self, *categories):
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
  1117
        return _session_hooks_control(self, HOOKS_ALLOW_ALL, *categories)
7405
8c752d113ebb [session] new methods on session to ease access to security/hooks control context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7340
diff changeset
  1118
    def deny_all_hooks_but(self, *categories):
9026
84c5b01ebb3e [connection] move hook control logic on Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9025
diff changeset
  1119
        return _session_hooks_control(self, HOOKS_DENY_ALL, *categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1120
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1121
    hooks_mode = cnx_attr('hooks_mode')
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
  1122
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1123
    disabled_hook_categories = cnx_attr('disabled_hook_cats')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1124
    enabled_hook_categories = cnx_attr('enabled_hook_cats')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1125
    disable_hook_categories = cnx_meth('disable_hook_categories')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1126
    enable_hook_categories = cnx_meth('enable_hook_categories')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1127
    is_hook_category_activated = cnx_meth('is_hook_category_activated')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1128
    is_hook_activated = cnx_meth('is_hook_activated')
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4767
diff changeset
  1129
2100
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1130
    # connection management ###################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1131
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1132
    def keep_cnxset_mode(self, mode):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1133
        """set `mode`, e.g. how the session will keep its connections set:
2570
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1134
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1135
        * if mode == 'write', the connections set is freed after each ready
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1136
          query, but kept until the transaction's end (eg commit or rollback)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1137
          when a write query is detected (eg INSERT/SET/DELETE queries)
2570
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1138
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1139
        * if mode == 'transaction', the connections set is only freed after the
2570
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1140
          transaction's end
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1141
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1142
        notice that a repository has a limited set of connections sets, and a
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1143
        session has to wait for a free connections set to run any rql query
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1144
        (unless it already has one set).
2570
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1145
        """
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1146
        assert mode in ('transaction', 'write')
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1147
        if mode == 'transaction':
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1148
            self.default_mode = 'transaction'
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1149
        else: # mode == 'write'
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1150
            self.default_mode = 'read'
80a996bb536d [repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2319
diff changeset
  1151
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1152
    mode = cnx_attr('mode', writable=True)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1153
    commit_state = cnx_attr('commit_state', writable=True)
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1154
2100
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1155
    @property
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1156
    def cnxset(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1157
        """connections set, set according to transaction mode for each query"""
5826
462435bf5457 [session] refactor session handling so that when calling session.close(), pool of long running transaction of the same session is properly freed
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5813
diff changeset
  1158
        if self._closed:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1159
            self.free_cnxset(True)
8999
81a5d57d9230 [session] upgrade session closed error from Exception to SessionClosedError
pierre-yves
parents: 8996
diff changeset
  1160
            raise SessionClosedError('try to access connections set on a closed session %s' % self.id)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1161
        return self._cnx.cnxset
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1162
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1163
    def set_cnxset(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1164
        """the session need a connections set to execute some queries"""
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1165
        with self._lock: # can probably be removed
7340
9303fd71c2ee [session] lock self._closed and session.close to avoid race conditions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7330
diff changeset
  1166
            if self._closed:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1167
                self.free_cnxset(True)
8999
81a5d57d9230 [session] upgrade session closed error from Exception to SessionClosedError
pierre-yves
parents: 8996
diff changeset
  1168
                raise SessionClosedError('try to set connections set on a closed session %s' % self.id)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1169
            return self._cnx.set_cnxset()
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1170
    free_cnxset = cnx_meth('free_cnxset')
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1171
2100
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1172
    def _touch(self):
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1173
        """update latest session usage timestamp and reset mode to read"""
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1174
        self.timestamp = time()
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1175
        self.local_perm_cache.clear() # XXX simply move in cnx.data, no?
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1176
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1177
    # shared data handling ###################################################
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1178
6013
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1179
    def get_shared_data(self, key, default=None, pop=False, txdata=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1180
        """return value associated to `key` in session data"""
6013
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1181
        if txdata:
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
  1182
            data = self._cnx.transaction_data
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1183
        else:
6013
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1184
            data = self.data
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1185
        if pop:
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1186
            return data.pop(key, default)
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1187
        else:
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1188
            return data.get(key, default)
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1189
6013
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1190
    def set_shared_data(self, key, value, txdata=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1191
        """set value associated to `key` in session data"""
6013
8ca424bc393b [dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
  1192
        if txdata:
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
  1193
            self._cnx.transaction_data[key] = value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1194
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1195
            self.data[key] = value
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1196
8367
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8306
diff changeset
  1197
    # server-side service call #################################################
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8306
diff changeset
  1198
9033
614bf73cc126 [service] drop the asynchronous execution possibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9027
diff changeset
  1199
    def call_service(self, regid, **kwargs):
614bf73cc126 [service] drop the asynchronous execution possibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9027
diff changeset
  1200
        return self.repo._call_service_with_session(self, regid,
8839
49eb84b80b96 [service] split session retrieval and service execution
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
  1201
                                                    **kwargs)
8367
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8306
diff changeset
  1202
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8306
diff changeset
  1203
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1204
    # request interface #######################################################
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1205
2855
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1206
    @property
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1207
    def cursor(self):
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1208
        """return a rql cursor"""
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1209
        return self
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1210
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1211
    set_entity_cache  = cnx_meth('set_entity_cache')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1212
    entity_cache      = cnx_meth('entity_cache')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1213
    cache_entities    = cnx_meth('cached_entities')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1214
    drop_entity_cache = cnx_meth('drop_entity_cache')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1215
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1216
    source_defs = cnx_meth('source_defs')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1217
    describe = cnx_meth('describe')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1218
    source_from_eid = cnx_meth('source_from_eid')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1219
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1220
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1221
    def execute(self, rql, kwargs=None, eid_key=None, build_descr=True):
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1222
        """db-api like method directly linked to the querier execute method.
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1223
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1224
        See :meth:`cubicweb.dbapi.Cursor.execute` documentation.
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1225
        """
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1226
        if eid_key is not None:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1227
            warn('[3.8] eid_key is deprecated, you can safely remove this argument',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1228
                 DeprecationWarning, stacklevel=2)
5792
e13aa4786a72 [session] update session's timestamp in session.execute, so long running transactions are not erroneously closed by the repository
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5791
diff changeset
  1229
        self.timestamp = time() # update timestamp
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1230
        rset = self._execute(self, rql, kwargs, build_descr)
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4845
diff changeset
  1231
        rset.req = self
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1232
        return rset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1233
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1234
    def close_cnx(self, cnxid):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1235
        cnx = self._cnxs.get(cnxid, None)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1236
        if cnx is not None:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1237
            cnx.free_cnxset(ignoremode=True)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1238
            self._clear_thread_storage(cnx)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1239
            self._clear_cnx_storage(cnx)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1240
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9050
diff changeset
  1241
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1242
    def _clear_thread_data(self, free_cnxset=True):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1243
        """remove everything from the thread local storage, except connections set
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1244
        which is explicitly removed by free_cnxset, and mode which is set anyway
4704
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1245
        by _touch
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1246
        """
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1247
        try:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1248
            cnx = self.__threaddata.cnx
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1249
        except AttributeError:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1250
            pass
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1251
        else:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1252
            if free_cnxset:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1253
                self.free_cnxset()
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1254
                if cnx.ctx_count == 0:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1255
                    self._clear_thread_storage(cnx)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1256
                else:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1257
                    self._clear_cnx_storage(cnx)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1258
            else:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1259
                self._clear_cnx_storage(cnx)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1260
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1261
    def _clear_thread_storage(self, cnx):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1262
        self._cnxs.pop(cnx.connectionid, None)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1263
        try:
9047
c873445fe198 [server/session] do not clear session.cnx if unrelated to cnx actually cleared
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9033
diff changeset
  1264
            if self.__threaddata.cnx is cnx:
c873445fe198 [server/session] do not clear session.cnx if unrelated to cnx actually cleared
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9033
diff changeset
  1265
                del self.__threaddata.cnx
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1266
        except AttributeError:
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7349
diff changeset
  1267
            pass
4704
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1268
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1269
    def _clear_cnx_storage(self, cnx):
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1270
        cnx.clear()
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1271
        cnx._rewriter = RQLRewriter(self)
4704
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1272
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1273
    def commit(self, free_cnxset=True, reset_pool=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1274
        """commit the current session's transaction"""
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1275
        if reset_pool is not None:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1276
            warn('[3.13] use free_cnxset argument instead for reset_pool',
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1277
                 DeprecationWarning, stacklevel=2)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1278
            free_cnxset = reset_pool
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1279
        if self.cnxset is None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1280
            assert not self.pending_operations
4704
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1281
            self._clear_thread_data()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1282
            self._touch()
2200
25bb65dc4559 test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2190
diff changeset
  1283
            self.debug('commit session %s done (no db activity)', self.id)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1284
            return
6385
9f91d09ee5fa [repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6071
diff changeset
  1285
        cstate = self.commit_state
9f91d09ee5fa [repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6071
diff changeset
  1286
        if cstate == 'uncommitable':
9f91d09ee5fa [repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6071
diff changeset
  1287
            raise QueryError('transaction must be rollbacked')
9f91d09ee5fa [repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6071
diff changeset
  1288
        if cstate is not None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1289
            return
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1290
        # on rollback, an operation should have the following state
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1291
        # information:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1292
        # - processed by the precommit/commit event or not
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1293
        # - if processed, is it the failed operation
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1294
        debug = server.DEBUG & server.DBG_OPS
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1295
        try:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1296
            # by default, operations are executed with security turned off
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
  1297
            with self.security_enabled(False, False):
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1298
                processed = []
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1299
                self.commit_state = 'precommit'
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1300
                if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1301
                    print self.commit_state, '*' * 20
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1302
                try:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1303
                    while self.pending_operations:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1304
                        operation = self.pending_operations.pop(0)
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1305
                        operation.processed = 'precommit'
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1306
                        processed.append(operation)
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1307
                        if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1308
                            print operation
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1309
                        operation.handle_event('precommit_event')
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1310
                    self.pending_operations[:] = processed
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1311
                    self.debug('precommit session %s done', self.id)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1312
                except BaseException:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1313
                    # if error on [pre]commit:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1314
                    #
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1315
                    # * set .failed = True on the operation causing the failure
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1316
                    # * call revert<event>_event on processed operations
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1317
                    # * call rollback_event on *all* operations
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1318
                    #
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1319
                    # that seems more natural than not calling rollback_event
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1320
                    # for processed operations, and allow generic rollback
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1321
                    # instead of having to implements rollback, revertprecommit
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1322
                    # and revertcommit, that will be enough in mont case.
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1323
                    operation.failed = True
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1324
                    if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1325
                        print self.commit_state, '*' * 20
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1326
                    for operation in reversed(processed):
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1327
                        if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1328
                            print operation
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1329
                        try:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1330
                            operation.handle_event('revertprecommit_event')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1331
                        except BaseException:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1332
                            self.critical('error while reverting precommit',
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1333
                                          exc_info=True)
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1334
                    # XXX use slice notation since self.pending_operations is a
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1335
                    # read-only property.
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1336
                    self.pending_operations[:] = processed + self.pending_operations
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1337
                    self.rollback(free_cnxset)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1338
                    raise
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1339
                self.cnxset.commit()
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1340
                self.commit_state = 'postcommit'
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1341
                if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1342
                    print self.commit_state, '*' * 20
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1343
                while self.pending_operations:
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1344
                    operation = self.pending_operations.pop(0)
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1345
                    if debug:
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8596
diff changeset
  1346
                        print operation
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1347
                    operation.processed = 'postcommit'
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1348
                    try:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1349
                        operation.handle_event('postcommit_event')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1350
                    except BaseException:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1351
                        self.critical('error while postcommit',
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1352
                                      exc_info=sys.exc_info())
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6077
diff changeset
  1353
                self.debug('postcommit session %s done', self.id)
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
  1354
                return self.transaction_uuid(set=False)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1355
        finally:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1356
            self._touch()
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1357
            if free_cnxset:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1358
                self.free_cnxset(ignoremode=True)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1359
            self._clear_thread_data(free_cnxset)
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1360
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1361
    def rollback(self, free_cnxset=True, reset_pool=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1362
        """rollback the current session's transaction"""
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1363
        if reset_pool is not None:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1364
            warn('[3.13] use free_cnxset argument instead for reset_pool',
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1365
                 DeprecationWarning, stacklevel=2)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1366
            free_cnxset = reset_pool
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1367
        # don't use self.cnxset, rollback may be called with _closed == True
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1368
        cnxset = self._cnx.cnxset
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1369
        if cnxset is None:
4704
a1ac5a453146 [session] fix memory leak: local thread data living in a thread that never finishes (eg, the main thread) may not be properly freed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4703
diff changeset
  1370
            self._clear_thread_data()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1371
            self._touch()
2200
25bb65dc4559 test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2190
diff changeset
  1372
            self.debug('rollback session %s done (no db activity)', self.id)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1373
            return
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1374
        try:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1375
            # by default, operations are executed with security turned off
8841
f62fb831cfe6 [session] make security_enabled API private
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8840
diff changeset
  1376
            with self.security_enabled(False, False):
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1377
                while self.pending_operations:
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1378
                    try:
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1379
                        operation = self.pending_operations.pop(0)
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1380
                        operation.handle_event('rollback_event')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1381
                    except BaseException:
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1382
                        self.critical('rollback error', exc_info=sys.exc_info())
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1383
                        continue
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1384
                cnxset.rollback()
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1385
                self.debug('rollback for session %s done', self.id)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1386
        finally:
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1387
            self._touch()
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1388
            if free_cnxset:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1389
                self.free_cnxset(ignoremode=True)
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1390
            self._clear_thread_data(free_cnxset)
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1391
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1392
    def close(self):
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1393
        # do not close connections set on session close, since they are shared now
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1394
        tracker = self._cnxset_tracker
8776
cdb261bd36ac [session] make session lock reentrant
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8775
diff changeset
  1395
        with self._lock:
7340
9303fd71c2ee [session] lock self._closed and session.close to avoid race conditions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7330
diff changeset
  1396
            self._closed = True
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1397
        tracker.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1398
        self.rollback()
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1399
        self.info('waiting for open connection of session: %s', self)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1400
        timeout = 10
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1401
        pendings = tracker.wait(timeout)
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1402
        if pendings:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1403
            self.error('%i connection still alive after 10 seconds, will close '
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1404
                       'session anyway', len(pendings))
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1405
            for cnxid in pendings:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1406
                cnx = self._cnxs.get(cnxid)
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1407
                if cnx is not None:
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1408
                    # drop cnx.cnxset
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1409
                    with tracker:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1410
                        try:
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1411
                            cnxset = cnx.cnxset
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1412
                            if cnxset is None:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1413
                                continue
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1414
                            cnx.cnxset = None
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1415
                        except RuntimeError:
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1416
                            msg = 'issue while force free of cnxset in %s'
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1417
                            self.error(msg, cnx)
8809
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1418
                    # cnxset.reconnect() do an hard reset of the cnxset
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1419
                    # it force it to be freed
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1420
                    cnxset.reconnect()
9ee4d0c65ad2 [session] use a dedicated class to track cnxset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8808
diff changeset
  1421
                    self.repo._free_cnxset(cnxset)
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5802
diff changeset
  1422
        del self.__threaddata
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1423
        del self._cnxs
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1424
7054
c8f12ab250b7 Add a "closed" property on session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
  1425
    @property
c8f12ab250b7 Add a "closed" property on session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
  1426
    def closed(self):
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1427
        return not hasattr(self, '_cnxs')
7054
c8f12ab250b7 Add a "closed" property on session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
  1428
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1429
    # transaction data/operations management ##################################
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1430
9024
701b19785f1d [connection] reinstall cnx.data as cnx.transaction_data
pierre-yves
parents: 9023
diff changeset
  1431
    transaction_data = cnx_attr('transaction_data')
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1432
    pending_operations = cnx_attr('pending_operations')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1433
    pruned_hooks_cache = cnx_attr('pruned_hooks_cache')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1434
    add_operation      = cnx_meth('add_operation')
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1435
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
  1436
    # undo support ############################################################
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
  1437
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1438
    ertype_supports_undo = cnx_meth('ertype_supports_undo')
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1439
    transaction_inc_action_counter = cnx_meth('transaction_inc_action_counter')
9075
8d36838ccb3e [connection] move last part of undo logic in connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9074
diff changeset
  1440
    transaction_uuid = cnx_meth('transaction_uuid')
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
  1441
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1442
    # querier helpers #########################################################
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1443
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1444
    rql_rewriter = cnx_attr('_rewriter')
2100
89b825cdec74 simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2063
diff changeset
  1445
2855
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1446
    # deprecated ###############################################################
1d9be3dffa94 [repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2840
diff changeset
  1447
9048
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1448
    @property
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1449
    def anonymous_session(self):
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1450
        # XXX for now, anonymous-user is a web side option.
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1451
        # It will only be present inside all-in-one instance.
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1452
        # there is plan to move it down to global config.
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1453
        return self.user.login == self.repo.config.get('anonymous-user')
8481fc6aa6dd [server/session] Implement anonymous_session
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9047
diff changeset
  1454
7502
e7190f7e850e [session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7500
diff changeset
  1455
    @deprecated('[3.13] use getattr(session.rtype_eids_rdef(rtype, eidfrom, eidto), prop)')
e7190f7e850e [session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7500
diff changeset
  1456
    def schema_rproperty(self, rtype, eidfrom, eidto, rprop):
e7190f7e850e [session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7500
diff changeset
  1457
        return getattr(self.rtype_eids_rdef(rtype, eidfrom, eidto), rprop)
e7190f7e850e [session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7500
diff changeset
  1458
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1459
    @property
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1460
    @deprecated("[3.13] use .cnxset attribute instead of .pool")
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1461
    def pool(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1462
        return self.cnxset
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1463
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1464
    @deprecated("[3.13] use .set_cnxset() method instead of .set_pool()")
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1465
    def set_pool(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1466
        return self.set_cnxset()
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1467
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1468
    @deprecated("[3.13] use .free_cnxset() method instead of .reset_pool()")
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1469
    def reset_pool(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1470
        return self.free_cnxset()
7502
e7190f7e850e [session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7500
diff changeset
  1471
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7054
diff changeset
  1472
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7054
diff changeset
  1473
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7054
diff changeset
  1474
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7054
diff changeset
  1475
8765
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
  1476
Session.HOOKS_ALLOW_ALL = HOOKS_ALLOW_ALL
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
  1477
Session.HOOKS_DENY_ALL = HOOKS_DENY_ALL
8767
a75670ef2d87 [session] move security constant out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8766
diff changeset
  1478
Session.DEFAULT_SECURITY = DEFAULT_SECURITY
8765
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
  1479
9e9029ba2d4e [session] Move hook control constants out of the class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8764
diff changeset
  1480
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1481
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1482
class InternalSession(Session):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1483
    """special session created internaly by the repository"""
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4899
diff changeset
  1484
    is_internal_session = True
5193
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5130
diff changeset
  1485
    running_dbapi_query = False
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1486
7706
359bc86d2827 [session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7665
diff changeset
  1487
    def __init__(self, repo, cnxprops=None, safe=False):
2891
60afb9705035 [session] temporary fix, need .req on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2880
diff changeset
  1488
        super(InternalSession, self).__init__(InternalManager(), repo, cnxprops,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1489
                                              _id='internal')
6071
c7a9e25153c2 [session] should be _cw, not req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6028
diff changeset
  1490
        self.user._cw = self # XXX remove when "vreg = user._cw.vreg" hack in entity.py is gone
7706
359bc86d2827 [session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7665
diff changeset
  1491
        if not safe:
359bc86d2827 [session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7665
diff changeset
  1492
            self.disable_hook_categories('integrity')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1493
8433
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1494
    def __enter__(self):
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1495
        return self
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1496
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1497
    def __exit__(self, exctype, excvalue, tb):
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1498
        self.close()
ff9d6d269877 [server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8367
diff changeset
  1499
5906
d40ced753291 [repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5826
diff changeset
  1500
    @property
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1501
    def cnxset(self):
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1502
        """connections set, set according to transaction mode for each query"""
5906
d40ced753291 [repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5826
diff changeset
  1503
        if self.repo.shutting_down:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7387
diff changeset
  1504
            self.free_cnxset(True)
7573
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7526
diff changeset
  1505
            raise ShuttingDown('repository is shutting down')
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9016
diff changeset
  1506
        return self._cnx.cnxset
5906
d40ced753291 [repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5826
diff changeset
  1507
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1508
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1509
class InternalManager(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1510
    """a manager user with all access rights used internally for task such as
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1511
    bootstrapping the repository or creating regular users according to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1512
    repository content
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1513
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1514
    def __init__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1515
        self.eid = -1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1516
        self.login = u'__internal_manager__'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1517
        self.properties = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1518
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1519
    def matching_groups(self, groups):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1520
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1521
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1522
    def is_in_group(self, group):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1523
        return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1524
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1525
    def owns(self, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1526
        return True
1660
d1030dd9730b delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
  1527
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1528
    def property_value(self, key):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1529
        if key == 'ui.language':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1530
            return 'en'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1531
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1532
8563
d28ccecb7bf5 mock ``CWUser.prefered_language()`` on InternalManager objects.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8562
diff changeset
  1533
    def prefered_language(self, language=None):
d28ccecb7bf5 mock ``CWUser.prefered_language()`` on InternalManager objects.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8562
diff changeset
  1534
        # mock CWUser.prefered_language, mainly for testing purpose
d28ccecb7bf5 mock ``CWUser.prefered_language()`` on InternalManager objects.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8562
diff changeset
  1535
        return self.property_value('ui.language')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1536
8579
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1537
    # CWUser compat for notification ###########################################
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1538
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1539
    def name(self):
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1540
        return 'cubicweb'
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1541
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1542
    class _IEmailable:
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1543
        @staticmethod
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1544
        def get_email():
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1545
            return ''
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1546
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1547
    def cw_adapt_to(self, iface):
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1548
        if iface == 'IEmailable':
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1549
            return self._IEmailable
c4673bc11053 [req / session] drop is_internal_session (buggy) compat on base request by implementing necessary methods on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8563
diff changeset
  1550
        return None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1551
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1552
from logging import getLogger
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1553
from cubicweb import set_log_methods
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1554
set_log_methods(Session, getLogger('cubicweb.session'))
9021
d8806996ac01 [connection] add logging method on connection
pierre-yves
parents: 9020
diff changeset
  1555
set_log_methods(Connection, getLogger('cubicweb.session'))