dbapi.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Mon, 21 Mar 2011 17:47:42 +0100
branchstable
changeset 7098 78bfe257cb16
parent 7085 36e91d19188b
child 7134 01544b6d98fa
permissions -rw-r--r--
backported tag to stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5345
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
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: 5345
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""DB-API 2.0 compliant module
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
Take a look at http://www.python.org/peps/pep-0249.html
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
(most parts of this document are reported here in docstrings)
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: 5812
diff changeset
    23
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
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: 5812
diff changeset
    27
from threading import currentThread
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 322
diff changeset
    28
from logging import getLogger
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from time import time, clock
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    30
from itertools import count
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5100
diff changeset
    31
from warnings import warn
5507
3604c1c78295 [dbapi] missing import
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5500
diff changeset
    32
from os.path import join
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
1923
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
    34
from logilab.common.logging_ext import set_log_methods
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    35
from logilab.common.decorators import monkeypatch
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
    36
from logilab.common.deprecation import deprecated
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    37
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    38
from cubicweb import ETYPE_NAME_MAP, ConnectionError, AuthenticationError,\
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
    39
     cwvreg, cwconfig
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: 2770
diff changeset
    40
from cubicweb.req import RequestSessionBase
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: 2770
diff changeset
    41
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    42
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
_MARKER = object()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    45
def _fake_property_value(self, name):
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    46
    try:
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4705
diff changeset
    47
        return super(DBAPIRequest, self).property_value(name)
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    48
    except KeyError:
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    49
        return ''
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    50
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
    51
def fake(*args, **kwargs):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
    52
    return None
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
    53
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    54
def multiple_connections_fix():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    55
    """some monkey patching necessary when an application has to deal with
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    56
    several connections to different repositories. It tries to hide buggy class
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    57
    attributes since classes are not designed to be shared among multiple
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    58
    registries.
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    59
    """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    60
    defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None]
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    61
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    62
    etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes']
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    63
    orig_etype_class = etypescls.orig_etype_class = etypescls.etype_class
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    64
    @monkeypatch(defaultcls)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    65
    def etype_class(self, etype):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    66
        """return an entity class for the given entity type.
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    67
        Try to find out a specific class for this kind of entity or
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    68
        default to a dump of the class registered for 'Any'
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    69
        """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    70
        usercls = orig_etype_class(self, etype)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    71
        if etype == 'Any':
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    72
            return usercls
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    73
        usercls.e_schema = self.schema.eschema(etype)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    74
        return usercls
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    75
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    76
def multiple_connections_unfix():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    77
    etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes']
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    78
    etypescls.etype_class = etypescls.orig_etype_class
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
    79
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
    80
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
class ConnectionProperties(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    def __init__(self, cnxtype=None, lang=None, close=True, log=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        self.cnxtype = cnxtype or 'pyro'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        self.lang = lang
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        self.log_queries = log
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        self.close_on_del = close
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
def get_repository(method, database=None, config=None, vreg=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    """get a proxy object to the CubicWeb repository, using a specific RPC method.
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    91
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    Only 'in-memory' and 'pyro' are supported for now. Either vreg or config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    argument should be given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    assert method in ('pyro', 'inmemory')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    assert vreg or config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
    if vreg and not config:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        config = vreg.config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    if method == 'inmemory':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        # get local access to the repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        from cubicweb.server.repository import Repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        return Repository(config, vreg=vreg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    else: # method == 'pyro'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        # resolve the Pyro object
2665
0c6281487f90 [pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   105
        from logilab.common.pyro_ext import ns_get_proxy
6061
7cb29eab1c9d [pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5863
diff changeset
   106
        pyroid = database or config['pyro-instance-id'] or config.appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        try:
6061
7cb29eab1c9d [pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5863
diff changeset
   108
            return ns_get_proxy(pyroid, defaultnsgroup=config['pyro-ns-group'],
2665
0c6281487f90 [pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   109
                                nshost=config['pyro-ns-host'])
0c6281487f90 [pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   110
        except Exception, ex:
0c6281487f90 [pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   111
            raise ConnectionError(str(ex))
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   112
3647
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   113
def repo_connect(repo, login, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    """Constructor to create a new connection to the CubicWeb repository.
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   115
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    Returns a Connection instance.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    """
3647
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   118
    if not 'cnxprops' in kwargs:
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   119
        kwargs['cnxprops'] = ConnectionProperties('inmemory')
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   120
    cnxid = repo.connect(unicode(login), **kwargs)
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   121
    cnx = Connection(repo, cnxid, kwargs['cnxprops'])
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   122
    if kwargs['cnxprops'].cnxtype == 'inmemory':
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        cnx.vreg = repo.vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    return cnx
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   125
3647
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   126
def connect(database=None, login=None, host=None, group=None,
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   127
            cnxprops=None, setvreg=True, mulcnx=True, initlog=True, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
    """Constructor for creating a connection to the CubicWeb repository.
5345
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   129
    Returns a :class:`Connection` object.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   130
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   131
    Typical usage::
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   132
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   133
      cnx = connect('myinstance', login='me', password='toto')
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   134
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   135
    Arguments:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   136
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   137
    :database:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   138
      the instance's pyro identifier.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   139
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   140
    :login:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   141
      the user login to use to authenticate.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   142
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   143
    :host:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   144
      the pyro nameserver host. Will be detected using broadcast query if
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   145
      unspecified.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   146
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   147
    :group:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   148
      the instance's pyro nameserver group. You don't have to specify it unless
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   149
      tweaked in instance's configuration.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
5345
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   151
    :cnxprops:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   152
      an optional :class:`ConnectionProperties` instance, allowing to specify
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   153
      the connection method (eg in memory or pyro). A Pyro connection will be
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   154
      established if you don't specify that argument.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   155
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   156
    :setvreg:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   157
      flag telling if a registry should be initialized for the connection.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   158
      Don't change this unless you know what you're doing.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   159
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   160
    :mulcnx:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   161
      Will disappear at some point. Try to deal with connections to differents
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   162
      instances in the same process unless specified otherwise by setting this
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   163
      flag to False. Don't change this unless you know what you're doing.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   164
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   165
    :initlog:
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   166
      flag telling if logging should be initialized. You usually don't want
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   167
      logging initialization when establishing the connection from a process
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   168
      where it's already initialized.
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   169
dc9afabbb45b [dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5307
diff changeset
   170
    :kwargs:
7004
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   171
      there goes authentication tokens. You usually have to specify a password
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   172
      for the given user, using a named 'password' argument.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
    cnxprops = cnxprops or ConnectionProperties()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    method = cnxprops.cnxtype
7004
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   176
    if method == 'pyro':
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   177
        config = cwconfig.CubicWebNoAppConfiguration()
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   178
        if host:
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   179
            config.global_set_option('pyro-ns-host', host)
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   180
        if group:
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   181
            config.global_set_option('pyro-ns-group', group)
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   182
    else:
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   183
        assert database
9e142116d82f [dbapi] fix for cases where one want to use 'connect' for an in-memory connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   184
        config = cwconfig.instance_configuration(database)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
    repo = get_repository(method, database, config=config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
    if method == 'inmemory':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        vreg = repo.vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
    elif setvreg:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
        if mulcnx:
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
   190
            multiple_connections_fix()
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
   191
        vreg = cwvreg.CubicWebVRegistry(config, initlog=initlog)
1923
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   192
        schema = repo.get_schema()
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   193
        for oldetype, newetype in ETYPE_NAME_MAP.items():
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   194
            if oldetype in schema:
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   195
                print 'aliasing', newetype, 'to', oldetype
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   196
                schema._entities[newetype] = schema._entities[oldetype]
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   197
        vreg.set_schema(schema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        vreg = None
3647
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   200
    cnx = repo_connect(repo, login, cnxprops=cnxprops, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    cnx.vreg = vreg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    return cnx
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
7056
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   204
def in_memory_repo(config):
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   205
    """Return and in_memory Repository object from a config (or vreg)"""
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2496
diff changeset
   206
    if isinstance(config, cwvreg.CubicWebVRegistry):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        vreg = config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
        config = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        vreg = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    # get local access to the repository
7056
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   212
    return get_repository('inmemory', config=config, vreg=vreg)
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   213
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   214
def in_memory_cnx(repo, login, **kwargs):
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   215
    """Establish a In memory connection to a <repo> for the user with <login>
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   216
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   217
    additionel credential might be required"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
    cnxprops = ConnectionProperties('inmemory')
7056
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   219
    return repo_connect(repo, login, cnxprops=cnxprops, **kwargs)
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   220
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   221
def in_memory_repo_cnx(config, login, **kwargs):
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   222
    """usefull method for testing and scripting to get a dbapi.Connection
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   223
    object connected to an in-memory repository instance
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   224
    """
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   225
    # connection to the CubicWeb repository
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   226
    repo = in_memory_repo(config)
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7004
diff changeset
   227
    return repo, in_memory_cnx(repo, login, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   229
class _NeedAuthAccessMock(object):
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   230
    def __getattribute__(self, attr):
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   231
        raise AuthenticationError()
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   232
    def __nonzero__(self):
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   233
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   235
class DBAPISession(object):
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   236
    def __init__(self, cnx, login=None):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   237
        self.cnx = cnx
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   238
        self.data = {}
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   239
        self.login = login
5251
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   240
        # dbapi session identifier is the same as the first connection
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   241
        # identifier, but may later differ in case of auto-reconnection as done
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   242
        # by the web authentication manager (in cw.web.views.authentication)
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   243
        if cnx is not None:
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   244
            self.sessionid = cnx.sessionid
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   245
        else:
b675edd05c19 [web session] fix web session id bug on automatic reconnection. The web session id should keep the first connection id, then differ of the repo connection id once some reconnection has been done (since the session cookie isn't updated in such cases). Also, use a lock to avoid potential race condition on reconnection.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5244
diff changeset
   246
            self.sessionid = None
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   247
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   248
    @property
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   249
    def anonymous_session(self):
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   250
        return not self.cnx or self.cnx.anonymous_connection
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   251
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
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: 2770
diff changeset
   253
class DBAPIRequest(RequestSessionBase):
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   254
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   255
    def __init__(self, vreg, session=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
        super(DBAPIRequest, self).__init__(vreg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
            # no vreg or config which doesn't handle translations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
            self.translations = vreg.config.translations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
        except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
            self.translations = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
        self.set_default_language(vreg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        # cache entities built during the request
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
        self._eid_cache = {}
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   265
        if session is not None:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   266
            self.set_session(session)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   267
        else:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   268
            # these args are initialized after a connection is
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   269
            # established
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   270
            self.session = None
5271
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   271
            self.cnx = self.user = _NeedAuthAccessMock()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
    def from_controller(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
        return 'view'
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   275
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   276
    def set_session(self, session, user=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
        """method called by the session handler when the user is authenticated
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
        or an anonymous connection is open
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
        """
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   280
        self.session = session
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   281
        if session.cnx:
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   282
            self.cnx = session.cnx
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   283
            self.execute = session.cnx.cursor(self).execute
5271
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   284
            if user is None:
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   285
                user = self.cnx.user(self, {'lang': self.lang})
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   286
        if user is not None:
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   287
            self.user = user
94e16453a640 [dbapi] fix user handling on dbapi request. Avoid getting None as _user and remove the need for a property.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5251
diff changeset
   288
            self.set_entity_cache(user)
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   289
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   290
    def execute(self, *args, **kwargs):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   291
        """overriden when session is set. By default raise authentication error
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   292
        so authentication is requested.
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   293
        """
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   294
        raise AuthenticationError()
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   295
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
    def set_default_language(self, vreg):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
            self.lang = vreg.property_value('ui.language')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
        except: # property may not be registered
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
            self.lang = 'en'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
        # use req.__ to translate a message without registering it to the catalog
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
        try:
3275
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3258
diff changeset
   303
            gettext, pgettext = self.translations[self.lang]
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3258
diff changeset
   304
            self._ = self.__ = gettext
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3258
diff changeset
   305
            self.pgettext = pgettext
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
            # this occurs usually during test execution
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
            self._ = self.__ = unicode
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4716
diff changeset
   309
            self.pgettext = lambda x, y: y
2111
5e142c7a4531 different message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   310
        self.debug('request default language: %s', self.lang)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
    # entities cache management ###############################################
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   313
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
    def entity_cache(self, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
        return self._eid_cache[eid]
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   316
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
    def set_entity_cache(self, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
        self._eid_cache[entity.eid] = entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
    def cached_entities(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
        return self._eid_cache.values()
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   322
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    def drop_entity_cache(self, eid=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
        if eid is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
            self._eid_cache = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
            del self._eid_cache[eid]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
    # low level session data management #######################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
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: 6012
diff changeset
   331
    def get_shared_data(self, key, default=None, pop=False, txdata=False):
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: 6012
diff changeset
   332
        """see :meth:`Connection.get_shared_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: 6012
diff changeset
   333
        return self.cnx.get_shared_data(key, default, pop, txdata)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
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: 6012
diff changeset
   335
    def set_shared_data(self, key, value, txdata=False, querydata=None):
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: 6012
diff changeset
   336
        """see :meth:`Connection.set_shared_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: 6012
diff changeset
   337
        if querydata is not None:
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: 6012
diff changeset
   338
            txdata = querydata
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: 6012
diff changeset
   339
            warn('[3.10] querydata argument has been renamed to txdata',
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: 6012
diff changeset
   340
                 DeprecationWarning, stacklevel=2)
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: 6012
diff changeset
   341
        return self.cnx.set_shared_data(key, value, txdata)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
    # server session compat layer #############################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   345
    def describe(self, eid):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   346
        """return a tuple (type, sourceuri, extid) for the entity with id <eid>"""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   347
        return self.cnx.describe(eid)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   348
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   349
    def source_defs(self):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   350
        """return the definition of sources used by the repository."""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   351
        return self.cnx.source_defs()
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   352
3110
757d36162235 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: 2665
diff changeset
   353
    def hijack_user(self, user):
757d36162235 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: 2665
diff changeset
   354
        """return a fake request/session using specified user"""
757d36162235 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: 2665
diff changeset
   355
        req = DBAPIRequest(self.vreg)
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   356
        req.set_session(self.session, user)
3110
757d36162235 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: 2665
diff changeset
   357
        return req
757d36162235 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: 2665
diff changeset
   358
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   359
    @deprecated('[3.8] use direct access to req.session.data dictionary')
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   360
    def session_data(self):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   361
        """return a dictionnary containing session data"""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   362
        return self.session.data
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   364
    @deprecated('[3.8] use direct access to req.session.data dictionary')
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   365
    def get_session_data(self, key, default=None, pop=False):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   366
        if pop:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   367
            return self.session.data.pop(key, default)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   368
        return self.session.data.get(key, default)
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   369
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   370
    @deprecated('[3.8] use direct access to req.session.data dictionary')
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   371
    def set_session_data(self, key, value):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   372
        self.session.data[key] = value
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   373
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   374
    @deprecated('[3.8] use direct access to req.session.data dictionary')
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   375
    def del_session_data(self, key):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   376
        self.session.data.pop(key, None)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   377
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7056
diff changeset
   378
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7056
diff changeset
   379
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7056
diff changeset
   380
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi'))
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   383
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   384
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
# exceptions ##################################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
class ProgrammingError(Exception): #DatabaseError):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
    """Exception raised for errors that are related to the database's operation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
    and not necessarily under the control of the programmer, e.g. an unexpected
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
    disconnect occurs, the data source name is not found, a transaction could
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
    not be processed, a memory allocation error occurred during processing,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
    etc.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   395
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   396
# cursor / connection objects ##################################################
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   397
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   398
class Cursor(object):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   399
    """These objects represent a database cursor, which is used to manage the
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   400
    context of a fetch operation. Cursors created from the same connection are
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   401
    not isolated, i.e., any changes done to the database by a cursor are
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   402
    immediately visible by the other cursors. Cursors created from different
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   403
    connections are isolated.
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   404
    """
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   405
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   406
    def __init__(self, connection, repo, req=None):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   407
        """This read-only attribute return a reference to the Connection
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   408
        object on which the cursor was created.
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   409
        """
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   410
        self.connection = connection
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   411
        """optionnal issuing request instance"""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   412
        self.req = req
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   413
        self._repo = repo
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   414
        self._sessid = connection.sessionid
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   415
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   416
    def close(self):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   417
        """no effect"""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   418
        pass
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   419
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: 5812
diff changeset
   420
    def _txid(self):
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: 5812
diff changeset
   421
        return self.connection._txid(self)
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: 5812
diff changeset
   422
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   423
    def execute(self, rql, args=None, eid_key=None, build_descr=True):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   424
        """execute a rql query, return resulting rows and their description in
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   425
        a :class:`~cubicweb.rset.ResultSet` object
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   426
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   427
        * `rql` should be an Unicode string or a plain ASCII string, containing
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   428
          the rql query
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   429
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   430
        * `args` the optional args dictionary associated to the query, with key
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   431
          matching named substitution in `rql`
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   432
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   433
        * `build_descr` is a boolean flag indicating if the description should
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   434
          be built on select queries (if false, the description will be en empty
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   435
          list)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   436
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   437
        on INSERT queries, there will be one row for each inserted entity,
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   438
        containing its eid
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   439
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   440
        on SET queries, XXX describe
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   441
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   442
        DELETE queries returns no result.
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   443
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   444
        .. Note::
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   445
          to maximize the rql parsing/analyzing cache performance, you should
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   446
          always use substitute arguments in queries, i.e. avoid query such as::
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   447
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   448
            execute('Any X WHERE X eid 123')
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   449
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   450
          use::
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   451
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   452
            execute('Any X WHERE X eid %(x)s', {'x': 123})
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   453
        """
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   454
        if eid_key is not None:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   455
            warn('[3.8] eid_key is deprecated, you can safely remove this argument',
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   456
                 DeprecationWarning, stacklevel=2)
5364
e0feefb87de5 [dbapi] some compat with distant < 3.8 repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5359
diff changeset
   457
        # XXX use named argument for build_descr in case repo is < 3.8
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: 5812
diff changeset
   458
        rset = self._repo.execute(self._sessid, rql, args,
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   459
                                  build_descr=build_descr, **self._txid())
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   460
        rset.req = self.req
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   461
        return rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   464
class LogCursor(Cursor):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   465
    """override the standard cursor to log executed queries"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   467
    def execute(self, operation, parameters=None, eid_key=None, build_descr=True):
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   468
        """override the standard cursor to log executed queries"""
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   469
        if eid_key is not None:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   470
            warn('[3.8] eid_key is deprecated, you can safely remove this argument',
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   471
                 DeprecationWarning, stacklevel=2)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   472
        tstart, cstart = time(), clock()
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   473
        rset = Cursor.execute(self, operation, parameters, build_descr=build_descr)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   474
        self.connection.executed_queries.append((operation, parameters,
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   475
                                                 time() - tstart, clock() - cstart))
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   476
        return rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   478
def check_not_closed(func):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   479
    def decorator(self, *args, **kwargs):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   480
        if self._closed is not None:
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   481
            raise ProgrammingError('Closed connection')
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   482
        return func(self, *args, **kwargs)
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   483
    return decorator
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
class Connection(object):
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3110
diff changeset
   486
    """DB-API 2.0 compatible Connection object for CubicWeb
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
    # make exceptions available through the connection object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
    ProgrammingError = ProgrammingError
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   490
    # attributes that may be overriden per connection instance
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   491
    anonymous_connection = False
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   492
    cursor_class = Cursor
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   493
    vreg = None
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   494
    _closed = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
    def __init__(self, repo, cnxid, cnxprops=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
        self._repo = repo
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
        self.sessionid = cnxid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
        self._close_on_del = getattr(cnxprops, 'close_on_del', True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
        self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro')
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   501
        self._web_request = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
        if cnxprops and cnxprops.log_queries:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
            self.executed_queries = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
            self.cursor_class = LogCursor
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   505
        if self._cnxtype == 'pyro':
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   506
            # check client/server compat
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   507
            if self._repo.get_versions()['cubicweb'] < (3, 8, 6):
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   508
                self._txid = lambda cursor=None: {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
    def __repr__(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
        if self.anonymous_connection:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
            return '<Connection %s (anonymous)>' % self.sessionid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
        return '<Connection %s>' % self.sessionid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
5100
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   515
    def __enter__(self):
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   516
        return self.cursor()
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   517
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   518
    def __exit__(self, exc_type, exc_val, exc_tb):
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   519
        if exc_type is None:
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   520
            self.commit()
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   521
        else:
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   522
            self.rollback()
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   523
            return False #propagate the exception
04c71ebf38a5 Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5032
diff changeset
   524
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   525
    def __del__(self):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   526
        """close the remote connection if necessary"""
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   527
        if self._closed is None and self._close_on_del:
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   528
            try:
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   529
                self.close()
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   530
            except:
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   531
                pass
2245
7463e1a748dd new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2111
diff changeset
   532
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   533
    # connection initialization methods ########################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   534
5763
1bdddb7460a9 [vreg] register_objects lost its force_reload argument, fix dbapi.Connection.load_appobjects consequently
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5507
diff changeset
   535
    def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   536
        config = self.vreg.config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   537
        if cubes is _MARKER:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   538
            cubes = self._repo.get_cubes()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
        elif cubes is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
            cubes = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   541
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   542
            if not isinstance(cubes, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   543
                cubes = (cubes,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   544
            if expand:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
                cubes = config.expand_cubes(cubes)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
        if subpath is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
            subpath = esubpath = ('entities', 'views')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   549
            esubpath = subpath
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   550
        if 'views' in subpath:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
            esubpath = list(subpath)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
            esubpath.remove('views')
5500
55a40cc0ab9a replaced hardcoded 'web/view' by os.path.join('web', 'view') so views path is also valid under windows
egazoni
parents: 5429
diff changeset
   553
            esubpath.append(join('web', 'views'))
6494
70c87c717e4a [config] make config.cubes() available on NoAppCubicWebConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
   554
        config.init_cubes(cubes)
70c87c717e4a [config] make config.cubes() available on NoAppCubicWebConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
   555
        vpath = config.build_vregistry_path(reversed(config.cubes_path()),
5812
d970049d7cfd [dbapi] Connection.load_appobjects should load site_cubicweb files as well, they may contains necessary RQL extension function definitions. Also kill some broken deprecated code in this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5763
diff changeset
   556
                                            evobjpath=esubpath,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
                                            tvobjpath=subpath)
5763
1bdddb7460a9 [vreg] register_objects lost its force_reload argument, fix dbapi.Connection.load_appobjects consequently
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5507
diff changeset
   558
        self.vreg.register_objects(vpath)
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   559
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   560
    def use_web_compatible_requests(self, baseurl, sitetitle=None):
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   561
        """monkey patch DBAPIRequest to fake a cw.web.request, so you should
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   562
        able to call html views using rset from a simple dbapi connection.
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   563
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   564
        You should call `load_appobjects` at some point to register those views.
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   565
        """
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   566
        DBAPIRequest.property_value = _fake_property_value
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   567
        DBAPIRequest.next_tabindex = count().next
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   568
        DBAPIRequest.relative_path = fake
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   569
        DBAPIRequest.url = fake
3832
ce8dd861f442 #473188: missing get_page_data when patching bare db-api request for web compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3707
diff changeset
   570
        DBAPIRequest.get_page_data = fake
3833
5507aa75f601 add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3832
diff changeset
   571
        DBAPIRequest.set_page_data = fake
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   572
        # XXX could ask the repo for it's base-url configuration
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   573
        self.vreg.config.set_option('base-url', baseurl)
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   574
        self.vreg.config.uiprops = {}
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   575
        self.vreg.config.datadir_url = baseurl + '/data'
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   576
        # XXX why is this needed? if really needed, could be fetched by a query
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   577
        if sitetitle is not None:
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   578
            self.vreg['propertydefs']['ui.site-title'] = {'default': sitetitle}
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   579
        self._web_request = True
2496
fbd1fd2ca312 #343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   580
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   581
    def request(self):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   582
        if self._web_request:
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   583
            from cubicweb.web.request import CubicWebRequestBase
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   584
            req = CubicWebRequestBase(self.vreg, False)
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   585
            req.get_header = lambda x, default=None: default
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   586
            req.set_session = lambda session, user=None: DBAPIRequest.set_session(
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   587
                req, session, user)
6613
e7ff604491b2 [dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6581
diff changeset
   588
            req.relative_path = lambda includeparams=True: ''
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   589
        else:
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   590
            req = DBAPIRequest(self.vreg)
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   591
        req.set_session(DBAPISession(self))
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   592
        return req
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   593
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   594
    @check_not_closed
322
0d9aca19b3d0 make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 169
diff changeset
   595
    def user(self, req=None, props=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
        """return the User object associated to this connection"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
        # cnx validity is checked by the call to .user_info
2245
7463e1a748dd new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2111
diff changeset
   598
        eid, login, groups, properties = self._repo.user_info(self.sessionid,
7463e1a748dd new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2111
diff changeset
   599
                                                              props)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
        if req is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
            req = self.request()
1923
3802c2e37e72 handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1882
diff changeset
   602
        rset = req.eid_rset(eid, 'CWUser')
5359
d83a227df75c [dbapi] take care vreg may not be set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5347
diff changeset
   603
        if self.vreg is not None and 'etypes' in self.vreg:
6848
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   604
            user = self.vreg['etypes'].etype_class('CWUser')(
f87cd875c6db [web session] cleanup session/authentication api: we don't have anymore to store authentication information on web session since the auto-reconnection feature has been dropped (eg in 3.10)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
   605
                req, rset, row=0, groups=groups, properties=properties)
5321
6bdca255985a [dbapi] fallback to Entity when etypes registry isn't initialized (weird we haven't catchi this pb before)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5271
diff changeset
   606
        else:
6bdca255985a [dbapi] fallback to Entity when etypes registry isn't initialized (weird we haven't catchi this pb before)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5271
diff changeset
   607
            from cubicweb.entity import Entity
6bdca255985a [dbapi] fallback to Entity when etypes registry isn't initialized (weird we haven't catchi this pb before)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5271
diff changeset
   608
            user = Entity(req, rset, row=0)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6066
diff changeset
   609
        user.cw_attr_cache['login'] = login # cache login
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   610
        return user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   612
    @check_not_closed
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   613
    def check(self):
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   614
        """raise `BadConnectionId` if the connection is no more valid, else
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   615
        return its latest activity timestamp.
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   616
        """
6293
df44d7163582 [dbapi] Connection.check() must return the latest activity timestamp (dropped accidentally during latest merge)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6279
diff changeset
   617
        return self._repo.check_session(self.sessionid)
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   618
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   619
    def _txid(self, cursor=None): # XXX could now handle various isolation level!
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   620
        # return a dict as bw compat trick
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   621
        return {'txid': currentThread().getName()}
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   622
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   623
    # session data methods #####################################################
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   624
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   625
    @check_not_closed
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   626
    def set_session_props(self, **props):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   627
        """raise `BadConnectionId` if the connection is no more valid"""
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   628
        self._repo.set_session_props(self.sessionid, props)
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   629
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   630
    @check_not_closed
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   631
    def get_shared_data(self, key, default=None, pop=False, txdata=False):
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   632
        """return value associated to key in the session's data dictionary or
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   633
        session's transaction's data if `txdata` is true.
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   634
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   635
        If pop is True, value will be removed from the dictionnary.
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   636
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   637
        If key isn't defined in the dictionnary, value specified by the
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   638
        `default` argument will be returned.
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   639
        """
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   640
        return self._repo.get_shared_data(self.sessionid, key, default, pop, txdata)
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   641
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   642
    @check_not_closed
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   643
    def set_shared_data(self, key, value, txdata=False):
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   644
        """set value associated to `key` in shared data
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   645
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   646
        if `txdata` is true, the value will be added to the repository
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   647
        session's query data which are cleared on commit/rollback of the current
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   648
        transaction.
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   649
        """
6279
42079f752a9c backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6257
diff changeset
   650
        return self._repo.set_shared_data(self.sessionid, key, value, txdata)
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   651
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   652
    # meta-data accessors ######################################################
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   653
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   654
    @check_not_closed
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   655
    def source_defs(self):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   656
        """Return the definition of sources used by the repository."""
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   657
        return self._repo.source_defs()
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   658
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6494
diff changeset
   659
    @check_not_closed
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   660
    def get_schema(self):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   661
        """Return the schema currently used by the repository."""
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   662
        return self._repo.get_schema()
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   663
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   664
    @check_not_closed
6308
c151c730a1ac [dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6257
diff changeset
   665
    def get_option_value(self, option, foreid=None):
c151c730a1ac [dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6257
diff changeset
   666
        """Return the value for `option` in the configuration. If `foreid` is
c151c730a1ac [dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6257
diff changeset
   667
        specified, the actual repository to which this entity belongs is
6312
b6c2b81e49ec [dbapi] fix typo
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6308
diff changeset
   668
        dereferenced and the option value retrieved from it.
6308
c151c730a1ac [dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6257
diff changeset
   669
        """
c151c730a1ac [dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6257
diff changeset
   670
        return self._repo.get_option_value(option, foreid)
6257
7eb5f1aed728 [repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6252
diff changeset
   671
7eb5f1aed728 [repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6252
diff changeset
   672
    @check_not_closed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   673
    def describe(self, eid):
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   674
        return self._repo.describe(self.sessionid, eid, **self._txid())
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   675
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   676
    # db-api like interface ####################################################
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   677
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   678
    @check_not_closed
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   679
    def commit(self):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   680
        """Commit pending transaction for this connection to the repository.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   681
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   682
        may raises `Unauthorized` or `ValidationError` if we attempted to do
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   683
        something we're not allowed to for security or integrity reason.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   684
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   685
        If the transaction is undoable, a transaction id will be returned.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   686
        """
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   687
        return self._repo.commit(self.sessionid, **self._txid())
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   688
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   689
    @check_not_closed
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   690
    def rollback(self):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   691
        """This method is optional since not all databases provide transaction
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   692
        support.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   693
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   694
        In case a database does provide transactions this method causes the the
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   695
        database to roll back to the start of any pending transaction.  Closing
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   696
        a connection without committing the changes first will cause an implicit
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   697
        rollback to be performed.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   698
        """
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   699
        self._repo.rollback(self.sessionid, **self._txid())
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   700
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   701
    @check_not_closed
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   702
    def cursor(self, req=None):
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   703
        """Return a new Cursor Object using the connection.
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   704
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   705
        On pyro connection, you should get cursor after calling if
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   706
        load_appobjects method if desired (which you should call if you intend
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   707
        to use ORM abilities).
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   708
        """
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   709
        if req is None:
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   710
            req = self.request()
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   711
        return self.cursor_class(self, self._repo, req=req)
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   712
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   713
    @check_not_closed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
    def close(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
        """Close the connection now (rather than whenever __del__ is called).
1524
1d7575f5deaf delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   716
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
        The connection will be unusable from this point forward; an Error (or
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   718
        subclass) exception will be raised if any operation is attempted with
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
        the connection. The same applies to all cursor objects trying to use the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
        connection.  Note that closing a connection without committing the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
        changes first will cause an implicit rollback to be performed.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   722
        """
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   723
        self._repo.close(self.sessionid, **self._txid())
4768
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   724
        del self._repo # necessary for proper garbage collection
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   725
        self._closed = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   726
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   727
    # undo support ############################################################
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   728
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   729
    @check_not_closed
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   730
    def undoable_transactions(self, ueid=None, req=None, **actionfilters):
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   731
        """Return a list of undoable transaction objects by the connection's
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   732
        user, ordered by descendant transaction time.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   733
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   734
        Managers may filter according to user (eid) who has done the transaction
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   735
        using the `ueid` argument. Others will only see their own transactions.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   736
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   737
        Additional filtering capabilities is provided by using the following
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   738
        named arguments:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   739
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   740
        * `etype` to get only transactions creating/updating/deleting entities
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   741
          of the given type
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   742
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   743
        * `eid` to get only transactions applied to entity of the given eid
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   744
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   745
        * `action` to get only transactions doing the given action (action in
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   746
          'C', 'U', 'D', 'A', 'R'). If `etype`, action can only be 'C', 'U' or
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   747
          'D'.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   748
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   749
        * `public`: when additional filtering is provided, their are by default
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   750
          only searched in 'public' actions, unless a `public` argument is given
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   751
          and set to false.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   752
        """
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   753
        actionfilters.update(self._txid())
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   754
        txinfos = self._repo.undoable_transactions(self.sessionid, ueid,
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   755
                                                   **actionfilters)
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   756
        if req is None:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   757
            req = self.request()
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   758
        for txinfo in txinfos:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   759
            txinfo.req = req
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   760
        return txinfos
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   761
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   762
    @check_not_closed
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   763
    def transaction_info(self, txuuid, req=None):
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   764
        """Return transaction object for the given uid.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   765
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   766
        raise `NoSuchTransaction` if not found or if session's user is not
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   767
        allowed (eg not in managers group and the transaction doesn't belong to
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   768
        him).
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   769
        """
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: 5812
diff changeset
   770
        txinfo = self._repo.transaction_info(self.sessionid, txuuid,
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   771
                                             **self._txid())
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   772
        if req is None:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   773
            req = self.request()
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   774
        txinfo.req = req
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   775
        return txinfo
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   776
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   777
    @check_not_closed
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   778
    def transaction_actions(self, txuuid, public=True):
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   779
        """Return an ordered list of action effectued during that transaction.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   780
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   781
        If public is true, return only 'public' actions, eg not ones triggered
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   782
        under the cover by hooks, else return all actions.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   783
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   784
        raise `NoSuchTransaction` if the transaction is not found or if
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   785
        session's user is not allowed (eg not in managers group and the
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   786
        transaction doesn't belong to him).
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   787
        """
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: 5812
diff changeset
   788
        return self._repo.transaction_actions(self.sessionid, txuuid, public,
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   789
                                              **self._txid())
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   790
6252
d33509b79efe [dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6061
diff changeset
   791
    @check_not_closed
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   792
    def undo_transaction(self, txuuid):
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   793
        """Undo the given transaction. Return potential restoration errors.
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   794
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   795
        raise `NoSuchTransaction` if not found or if session's user is not
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   796
        allowed (eg not in managers group and the transaction doesn't belong to
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   797
        him).
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4895
diff changeset
   798
        """
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: 5812
diff changeset
   799
        return self._repo.undo_transaction(self.sessionid, txuuid,
5859
3da3574fe397 [dbapi] refactor so that cw 3.8.6 can still speak with a 3.8.5 server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5813
diff changeset
   800
                                           **self._txid())