author | Florent Cayre <florent.cayre@logilab.fr> |
Sat, 13 Jul 2013 05:10:55 +0200 | |
branch | stable |
changeset 9155 | b4ccaf13081d |
parent 8983 | 2a244f90a1e3 |
child 8992 | 55a1567d92a0 |
permissions | -rw-r--r-- |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
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 | 18 |
"""DB-API 2.0 compliant module |
19 |
||
20 |
Take a look at http://www.python.org/peps/pep-0249.html |
|
21 |
||
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 | 24 |
|
25 |
__docformat__ = "restructuredtext en" |
|
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 | 28 |
from logging import getLogger |
0 | 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 |
7428
5338d895b891
[web session] fix session handling so we get a chance to have for instance the 'forgotpwd' feature working on a site where anonymous are not allowed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
33 |
from uuid import uuid4 |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
34 |
from urlparse import urlparse |
0 | 35 |
|
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
|
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
|
40 |
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
|
41 |
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
|
42 |
from cubicweb.req import RequestSessionBase |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
43 |
from cubicweb.utils import parse_repo_uri |
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
|
44 |
|
1524 | 45 |
|
0 | 46 |
_MARKER = object() |
47 |
||
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
48 |
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
|
49 |
try: |
4716
55b6a3262071
fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4705
diff
changeset
|
50 |
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
|
51 |
except KeyError: |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
52 |
return '' |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
53 |
|
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
54 |
def fake(*args, **kwargs): |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
55 |
return None |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
56 |
|
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
|
57 |
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
|
58 |
"""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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
""" |
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
63 |
defaultcls = cwvreg.CWRegistryStore.REGISTRY_FACTORY[None] |
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
|
64 |
|
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
65 |
etypescls = cwvreg.CWRegistryStore.REGISTRY_FACTORY['etypes'] |
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
|
66 |
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
|
67 |
@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
|
68 |
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
|
69 |
"""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
|
70 |
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
|
71 |
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
|
72 |
""" |
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 = 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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
|
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 |
def multiple_connections_unfix(): |
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
80 |
etypescls = cwvreg.CWRegistryStore.REGISTRY_FACTORY['etypes'] |
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
|
81 |
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
|
82 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
83 |
|
0 | 84 |
class ConnectionProperties(object): |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
85 |
def __init__(self, cnxtype=None, close=True, log=False): |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
86 |
if cnxtype is not None: |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
87 |
warn('[3.16] cnxtype argument is deprecated', DeprecationWarning, |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
88 |
stacklevel=2) |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
89 |
self.cnxtype = cnxtype |
0 | 90 |
self.log_queries = log |
91 |
self.close_on_del = close |
|
92 |
||
93 |
||
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
94 |
def _get_inmemory_repo(config, vreg=None): |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
95 |
from cubicweb.server.repository import Repository |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
96 |
from cubicweb.server.utils import TasksManager |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
97 |
return Repository(config, TasksManager(), vreg=vreg) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
98 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
99 |
def get_repository(uri=None, config=None, vreg=None): |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
100 |
"""get a repository for the given URI or config/vregistry (in case we're |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
101 |
loading the repository for a client, eg web server, configuration). |
1524 | 102 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
103 |
The returned repository may be an in-memory repository or a proxy object |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
104 |
using a specific RPC method, depending on the given URI (pyro or zmq). |
0 | 105 |
""" |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
106 |
if uri is None: |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
107 |
return _get_inmemory_repo(config, vreg) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
108 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
109 |
protocol, hostport, appid = parse_repo_uri(uri) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
110 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
111 |
if protocol == 'inmemory': |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
112 |
# me may have been called with a dummy 'inmemory://' uri ... |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
113 |
return _get_inmemory_repo(config, vreg) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
114 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
115 |
if protocol == 'pyroloc': # direct connection to the instance |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
116 |
from logilab.common.pyro_ext import get_proxy |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
117 |
uri = uri.replace('pyroloc', 'PYRO') |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
118 |
return get_proxy(uri) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
119 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
120 |
if protocol == 'pyro': # connection mediated through the pyro ns |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
121 |
from logilab.common.pyro_ext import ns_get_proxy |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
122 |
path = appid.strip('/') |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
123 |
if not path: |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
124 |
raise ConnectionError( |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
125 |
"can't find instance name in %s (expected to be the path component)" |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
126 |
% uri) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
127 |
if '.' in path: |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
128 |
nsgroup, nsid = path.rsplit('.', 1) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
129 |
else: |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
130 |
nsgroup = 'cubicweb' |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
131 |
nsid = path |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
132 |
return ns_get_proxy(nsid, defaultnsgroup=nsgroup, nshost=hostport) |
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
133 |
|
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
134 |
if protocol.startswith('zmqpickle-'): |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
135 |
from cubicweb.zmqclient import ZMQRepositoryClient |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
136 |
return ZMQRepositoryClient(uri) |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
137 |
else: |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
138 |
raise ConnectionError('unknown protocol: `%s`' % protocol) |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
139 |
|
1524 | 140 |
|
8673
8ea63a2cc2cc
[db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8670
diff
changeset
|
141 |
def _repo_connect(repo, login, **kwargs): |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
142 |
"""Constructor to create a new connection to the given CubicWeb repository. |
1524 | 143 |
|
0 | 144 |
Returns a Connection instance. |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
145 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
146 |
Raises AuthenticationError if authentication failed |
0 | 147 |
""" |
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
|
148 |
cnxid = repo.connect(unicode(login), **kwargs) |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
149 |
cnx = Connection(repo, cnxid, kwargs.get('cnxprops')) |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
150 |
if cnx.is_repo_in_memory: |
0 | 151 |
cnx.vreg = repo.vreg |
152 |
return cnx |
|
1524 | 153 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
154 |
def connect(database, login=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
|
155 |
cnxprops=None, setvreg=True, mulcnx=True, initlog=True, **kwargs): |
0 | 156 |
"""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
|
157 |
Returns a :class:`Connection` object. |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
158 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
159 |
Typical usage:: |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
160 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
161 |
cnx = connect('myinstance', login='me', password='toto') |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
162 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
163 |
`database` may be: |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
164 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
165 |
* a simple instance id for in-memory connection |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
166 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
167 |
* an uri like scheme://host:port/instanceid where scheme may be one of |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
168 |
'pyro', 'inmemory' or 'zmqpickle' |
5345
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
169 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
170 |
* if scheme is 'pyro', <host:port> determine the name server address. If |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
171 |
not specified (e.g. 'pyro:///instanceid'), it will be detected through a |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
172 |
broadcast query. The instance id is the name of the instance in the name |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
173 |
server and may be prefixed by a group (e.g. |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
174 |
'pyro:///:cubicweb.instanceid') |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
175 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
176 |
* if scheme is handled by ZMQ (eg 'tcp'), you should not specify an |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
177 |
instance id |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
178 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
179 |
Other arguments: |
5345
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
180 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
181 |
:login: |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
182 |
the user login to use to authenticate. |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
183 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
184 |
:cnxprops: |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
185 |
a :class:`ConnectionProperties` instance, allowing to specify |
5345
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
186 |
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
|
187 |
established if you don't specify that argument. |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
188 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
189 |
:setvreg: |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
193 |
:mulcnx: |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
198 |
:initlog: |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
199 |
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
|
200 |
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
|
201 |
where it's already initialized. |
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
202 |
|
dc9afabbb45b
[dbapi] document connect() function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5307
diff
changeset
|
203 |
: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
|
204 |
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
|
205 |
for the given user, using a named 'password' argument. |
0 | 206 |
""" |
8744
2091d275fe5c
dbapi: try to restore compatibility of the connect api with 3.15
Julien Cristau <julien.cristau@logilab.fr>
parents:
8702
diff
changeset
|
207 |
if not urlparse(database).scheme: |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
208 |
warn('[3.16] give an qualified URI as database instead of using ' |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
209 |
'host/cnxprops to specify the connection method', |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
210 |
DeprecationWarning, stacklevel=2) |
8744
2091d275fe5c
dbapi: try to restore compatibility of the connect api with 3.15
Julien Cristau <julien.cristau@logilab.fr>
parents:
8702
diff
changeset
|
211 |
if cnxprops and cnxprops.cnxtype == 'zmq': |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
212 |
database = kwargs.pop('host') |
8744
2091d275fe5c
dbapi: try to restore compatibility of the connect api with 3.15
Julien Cristau <julien.cristau@logilab.fr>
parents:
8702
diff
changeset
|
213 |
elif cnxprops and cnxprops.cnxtype == 'inmemory': |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
214 |
database = 'inmemory://' + database |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
215 |
else: |
8983
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
216 |
host = kwargs.pop('host', None) |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
217 |
if host is None: |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
218 |
host = '' |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
219 |
group = kwargs.pop('group', None) |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
220 |
if group is None: |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
221 |
group = 'cubicweb' |
2a244f90a1e3
[dbapi] fix connect backwards compat harder
Julien Cristau <julien.cristau@logilab.fr>
parents:
8744
diff
changeset
|
222 |
database = 'pyro://%s/%s.%s' % (host, group, database) |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
223 |
puri = urlparse(database) |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
224 |
method = puri.scheme.lower() |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
225 |
if method == 'inmemory': |
8682
20bd1cdf86ae
[cwctl] fix cubicweb-ctl shell command (closes #2583919)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8673
diff
changeset
|
226 |
config = cwconfig.instance_configuration(puri.path) |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
227 |
else: |
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
|
228 |
config = cwconfig.CubicWebNoAppConfiguration() |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
229 |
repo = get_repository(database, config=config) |
0 | 230 |
if method == 'inmemory': |
231 |
vreg = repo.vreg |
|
232 |
elif setvreg: |
|
233 |
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
|
234 |
multiple_connections_fix() |
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
235 |
vreg = cwvreg.CWRegistryStore(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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
vreg.set_schema(schema) |
0 | 242 |
else: |
243 |
vreg = None |
|
8673
8ea63a2cc2cc
[db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8670
diff
changeset
|
244 |
cnx = _repo_connect(repo, login, cnxprops=cnxprops, **kwargs) |
0 | 245 |
cnx.vreg = vreg |
246 |
return cnx |
|
247 |
||
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
248 |
def in_memory_repo(config): |
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
249 |
"""Return and in_memory Repository object from a config (or vreg)""" |
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
250 |
if isinstance(config, cwvreg.CWRegistryStore): |
0 | 251 |
vreg = config |
252 |
config = None |
|
253 |
else: |
|
254 |
vreg = None |
|
255 |
# get local access to the repository |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
256 |
return get_repository('inmemory://', config=config, vreg=vreg) |
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
257 |
|
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
258 |
def in_memory_repo_cnx(config, login, **kwargs): |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
259 |
"""useful method for testing and scripting to get a dbapi.Connection |
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
260 |
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
|
261 |
""" |
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
262 |
# connection to the CubicWeb repository |
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7004
diff
changeset
|
263 |
repo = in_memory_repo(config) |
8673
8ea63a2cc2cc
[db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8670
diff
changeset
|
264 |
return repo, _repo_connect(repo, login, **kwargs) |
0 | 265 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
266 |
# XXX web only method, move to webconfig? |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
267 |
def anonymous_session(vreg): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
268 |
"""return a new anonymous session |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
269 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
270 |
raises an AuthenticationError if anonymous usage is not allowed |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
271 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
272 |
anoninfo = vreg.config.anonymous_user() |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
273 |
if anoninfo is None: # no anonymous user |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
274 |
raise AuthenticationError('anonymous access is not authorized') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
275 |
anon_login, anon_password = anoninfo |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
276 |
# use vreg's repository cache |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
277 |
repo = vreg.config.repository(vreg) |
8673
8ea63a2cc2cc
[db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8670
diff
changeset
|
278 |
anon_cnx = _repo_connect(repo, anon_login, password=anon_password) |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
279 |
anon_cnx.vreg = vreg |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
280 |
return DBAPISession(anon_cnx, anon_login) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
281 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7815
diff
changeset
|
282 |
|
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
return False |
0 | 288 |
|
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
self.login = login |
7589
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
294 |
self.mtime = time() |
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
|
295 |
# 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
|
296 |
# 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
|
297 |
# 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
|
298 |
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
|
299 |
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
|
300 |
else: |
7428
5338d895b891
[web session] fix session handling so we get a chance to have for instance the 'forgotpwd' feature working on a site where anonymous are not allowed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7140
diff
changeset
|
301 |
self.sessionid = uuid4().hex |
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
|
302 |
|
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
|
303 |
@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
|
304 |
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
|
305 |
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
|
306 |
|
7909
71c70bf482cf
[web session] ProgrammingError may be raised, we should catch it (closes #1980076)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
307 |
def __repr__(self): |
71c70bf482cf
[web session] ProgrammingError may be raised, we should catch it (closes #1980076)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
308 |
return '<DBAPISession %r>' % self.sessionid |
0 | 309 |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
310 |
|
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
|
311 |
class DBAPIRequest(RequestSessionBase): |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
312 |
#: Request language identifier eg: 'en' |
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
313 |
lang = None |
1524 | 314 |
|
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
|
315 |
def __init__(self, vreg, session=None): |
0 | 316 |
super(DBAPIRequest, self).__init__(vreg) |
8274
aedb32d324ab
[dbapi] some documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8268
diff
changeset
|
317 |
#: 'language' => translation_function() mapping |
0 | 318 |
try: |
319 |
# no vreg or config which doesn't handle translations |
|
320 |
self.translations = vreg.config.translations |
|
321 |
except AttributeError: |
|
8404
3dcb117fb3b0
[db-api] fix typo leading to crash on client connection (config has no translations). Closes #2357044
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8367
diff
changeset
|
322 |
self.translations = {} |
8274
aedb32d324ab
[dbapi] some documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8268
diff
changeset
|
323 |
#: cache entities built during the request |
0 | 324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
# 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
|
329 |
# 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
|
330 |
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
|
331 |
self.cnx = self.user = _NeedAuthAccessMock() |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
332 |
self.set_default_language(vreg) |
0 | 333 |
|
334 |
def from_controller(self): |
|
335 |
return 'view' |
|
1524 | 336 |
|
8585
3f60f416dddb
[dbapi] provide get_option_value over DBAPIRequest (closes #2515522)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
337 |
def get_option_value(self, option, foreid=None): |
3f60f416dddb
[dbapi] provide get_option_value over DBAPIRequest (closes #2515522)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
338 |
return self.cnx.get_option_value(option, foreid) |
3f60f416dddb
[dbapi] provide get_option_value over DBAPIRequest (closes #2515522)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
339 |
|
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
|
340 |
def set_session(self, session, user=None): |
0 | 341 |
"""method called by the session handler when the user is authenticated |
342 |
or an anonymous connection is open |
|
343 |
""" |
|
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
|
344 |
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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
if user is None: |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
349 |
user = self.cnx.user(self) |
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
|
350 |
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
|
351 |
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
|
352 |
self.set_entity_cache(user) |
1524 | 353 |
|
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
354 |
def execute(self, *args, **kwargs): # pylint: disable=E0202 |
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
|
355 |
"""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
|
356 |
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
|
357 |
""" |
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
|
358 |
raise AuthenticationError() |
1524 | 359 |
|
0 | 360 |
def set_default_language(self, vreg): |
361 |
try: |
|
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
362 |
lang = vreg.property_value('ui.language') |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7665
diff
changeset
|
363 |
except Exception: # property may not be registered |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
364 |
lang = 'en' |
0 | 365 |
try: |
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
366 |
self.set_language(lang) |
0 | 367 |
except KeyError: |
368 |
# this occurs usually during test execution |
|
369 |
self._ = self.__ = unicode |
|
7960
981d2a6d95be
[test] map pgettext to unicode as _
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7909
diff
changeset
|
370 |
self.pgettext = lambda x, y: unicode(y) |
0 | 371 |
|
8367
fc59d2380c48
[service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8352
diff
changeset
|
372 |
# server-side service call ################################################# |
fc59d2380c48
[service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8352
diff
changeset
|
373 |
|
fc59d2380c48
[service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8352
diff
changeset
|
374 |
def call_service(self, regid, async=False, **kwargs): |
fc59d2380c48
[service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8352
diff
changeset
|
375 |
return self.cnx.call_service(regid, async, **kwargs) |
fc59d2380c48
[service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8352
diff
changeset
|
376 |
|
0 | 377 |
# entities cache management ############################################### |
1524 | 378 |
|
0 | 379 |
def entity_cache(self, eid): |
380 |
return self._eid_cache[eid] |
|
1524 | 381 |
|
0 | 382 |
def set_entity_cache(self, entity): |
383 |
self._eid_cache[entity.eid] = entity |
|
384 |
||
385 |
def cached_entities(self): |
|
386 |
return self._eid_cache.values() |
|
1524 | 387 |
|
0 | 388 |
def drop_entity_cache(self, eid=None): |
389 |
if eid is None: |
|
390 |
self._eid_cache = {} |
|
391 |
else: |
|
392 |
del self._eid_cache[eid] |
|
393 |
||
394 |
# low level session data management ####################################### |
|
395 |
||
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
|
396 |
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
|
397 |
"""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
|
398 |
return self.cnx.get_shared_data(key, default, pop, txdata) |
0 | 399 |
|
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
|
400 |
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
|
401 |
"""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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
return self.cnx.set_shared_data(key, value, txdata) |
0 | 407 |
|
408 |
# server session compat layer ############################################# |
|
409 |
||
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
410 |
def describe(self, eid, asdict=False): |
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
|
411 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
412 |
return self.cnx.describe(eid, asdict) |
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
|
413 |
|
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 |
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
|
415 |
"""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
|
416 |
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
|
417 |
|
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
|
418 |
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
|
419 |
"""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
|
420 |
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
|
421 |
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
|
422 |
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
|
423 |
|
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
|
424 |
@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
|
425 |
def session_data(self): |
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
426 |
"""return a dictionary containing session data""" |
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
|
427 |
return self.session.data |
0 | 428 |
|
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
|
429 |
@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
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
return self.session.data.get(key, default) |
1524 | 434 |
|
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
|
435 |
@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
|
436 |
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
|
437 |
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
|
438 |
|
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 |
@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
|
440 |
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
|
441 |
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
|
442 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7056
diff
changeset
|
443 |
# 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
|
444 |
# 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
|
445 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 446 |
|
447 |
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi')) |
|
1524 | 448 |
|
449 |
||
0 | 450 |
# exceptions ################################################################## |
451 |
||
452 |
class ProgrammingError(Exception): #DatabaseError): |
|
453 |
"""Exception raised for errors that are related to the database's operation |
|
454 |
and not necessarily under the control of the programmer, e.g. an unexpected |
|
455 |
disconnect occurs, the data source name is not found, a transaction could |
|
456 |
not be processed, a memory allocation error occurred during processing, |
|
457 |
etc. |
|
458 |
""" |
|
459 |
||
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 |
|
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 |
# 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
|
462 |
|
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
|
463 |
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
|
464 |
"""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
|
465 |
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
|
466 |
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
|
467 |
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
|
468 |
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
|
469 |
""" |
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 |
|
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 |
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
|
472 |
"""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
|
473 |
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
|
474 |
""" |
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 |
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
|
476 |
"""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
|
477 |
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
|
478 |
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
|
479 |
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
|
480 |
|
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
|
481 |
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
|
482 |
"""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
|
483 |
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
|
484 |
|
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
|
485 |
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
|
486 |
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
|
487 |
|
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
|
488 |
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
|
489 |
"""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
|
490 |
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
|
491 |
|
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 |
* `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
|
493 |
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
|
494 |
|
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
|
495 |
* `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
|
496 |
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
|
497 |
|
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
|
498 |
* `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
|
499 |
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
|
500 |
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
|
501 |
|
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
|
502 |
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
|
503 |
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
|
504 |
|
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
|
505 |
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
|
506 |
|
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
|
507 |
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
|
508 |
|
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
|
509 |
.. 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
|
510 |
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
|
511 |
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
|
512 |
|
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
|
513 |
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
|
514 |
|
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
|
515 |
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
|
516 |
|
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
|
517 |
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
|
518 |
""" |
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
|
519 |
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
|
520 |
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
|
521 |
DeprecationWarning, stacklevel=2) |
5364
e0feefb87de5
[dbapi] some compat with distant < 3.8 repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5359
diff
changeset
|
522 |
# 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
|
523 |
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
|
524 |
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
|
525 |
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
|
526 |
return rset |
0 | 527 |
|
528 |
||
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
|
529 |
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
|
530 |
"""override the standard cursor to log executed queries""" |
0 | 531 |
|
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
|
532 |
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
|
533 |
"""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
|
534 |
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
|
535 |
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
|
536 |
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
|
537 |
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
|
538 |
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
|
539 |
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
|
540 |
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
|
541 |
return rset |
0 | 542 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
543 |
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
|
544 |
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
|
545 |
if self._closed is not None: |
7665
0cd299c16f12
[session] add session id to closed connection errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7650
diff
changeset
|
546 |
raise ProgrammingError('Closed connection %s' % 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
|
547 |
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
|
548 |
return decorator |
0 | 549 |
|
550 |
class Connection(object): |
|
3258
6536ee4f37f7
update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3110
diff
changeset
|
551 |
"""DB-API 2.0 compatible Connection object for CubicWeb |
0 | 552 |
""" |
553 |
# make exceptions available through the connection object |
|
554 |
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
|
555 |
# 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
|
556 |
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
|
557 |
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
|
558 |
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
|
559 |
_closed = None |
0 | 560 |
|
561 |
def __init__(self, repo, cnxid, cnxprops=None): |
|
562 |
self._repo = repo |
|
563 |
self.sessionid = cnxid |
|
564 |
self._close_on_del = getattr(cnxprops, 'close_on_del', True) |
|
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
565 |
self._web_request = False |
0 | 566 |
if cnxprops and cnxprops.log_queries: |
567 |
self.executed_queries = [] |
|
568 |
self.cursor_class = LogCursor |
|
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
569 |
|
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
570 |
@property |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
571 |
def is_repo_in_memory(self): |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
572 |
"""return True if this is a local, aka in-memory, connection to the |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
573 |
repository |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
574 |
""" |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
575 |
try: |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
576 |
from cubicweb.server.repository import Repository |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
577 |
except ImportError: |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
578 |
# code not available, no way |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
579 |
return False |
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
580 |
return isinstance(self._repo, Repository) |
0 | 581 |
|
582 |
def __repr__(self): |
|
583 |
if self.anonymous_connection: |
|
584 |
return '<Connection %s (anonymous)>' % self.sessionid |
|
585 |
return '<Connection %s>' % self.sessionid |
|
586 |
||
5100
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
587 |
def __enter__(self): |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
588 |
return self.cursor() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
589 |
|
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
590 |
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
|
591 |
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
|
592 |
self.commit() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
593 |
else: |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
594 |
self.rollback() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
595 |
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
|
596 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
597 |
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
|
598 |
"""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
|
599 |
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
|
600 |
try: |
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
601 |
self.close() |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7665
diff
changeset
|
602 |
except Exception: |
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
603 |
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
|
604 |
|
8268
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
605 |
# server-side service call ################################################# |
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
606 |
|
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
607 |
@check_not_closed |
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
608 |
def call_service(self, regid, async=False, **kwargs): |
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
609 |
return self._repo.call_service(self.sessionid, regid, async, **kwargs) |
c9babe49c1c1
[repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents:
8239
diff
changeset
|
610 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
611 |
# connection initialization methods ######################################## |
0 | 612 |
|
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
|
613 |
def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True): |
0 | 614 |
config = self.vreg.config |
615 |
if cubes is _MARKER: |
|
616 |
cubes = self._repo.get_cubes() |
|
617 |
elif cubes is None: |
|
618 |
cubes = () |
|
619 |
else: |
|
620 |
if not isinstance(cubes, (list, tuple)): |
|
621 |
cubes = (cubes,) |
|
622 |
if expand: |
|
623 |
cubes = config.expand_cubes(cubes) |
|
624 |
if subpath is None: |
|
625 |
subpath = esubpath = ('entities', 'views') |
|
626 |
else: |
|
627 |
esubpath = subpath |
|
628 |
if 'views' in subpath: |
|
629 |
esubpath = list(subpath) |
|
630 |
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
|
631 |
esubpath.append(join('web', 'views')) |
8571
7e5246889148
[dbapi] load_appobjects must attempt to load available cubicweb configurations to avoid error when some object use a persistent propery (CWProperty) defined there. Closes #2497697
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
632 |
# first load available configs, necessary for proper persistent |
7e5246889148
[dbapi] load_appobjects must attempt to load available cubicweb configurations to avoid error when some object use a persistent propery (CWProperty) defined there. Closes #2497697
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
633 |
# properties initialization |
7e5246889148
[dbapi] load_appobjects must attempt to load available cubicweb configurations to avoid error when some object use a persistent propery (CWProperty) defined there. Closes #2497697
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
634 |
config.load_available_configs() |
7e5246889148
[dbapi] load_appobjects must attempt to load available cubicweb configurations to avoid error when some object use a persistent propery (CWProperty) defined there. Closes #2497697
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
635 |
# then init cubes |
6494
70c87c717e4a
[config] make config.cubes() available on NoAppCubicWebConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6333
diff
changeset
|
636 |
config.init_cubes(cubes) |
8571
7e5246889148
[dbapi] load_appobjects must attempt to load available cubicweb configurations to avoid error when some object use a persistent propery (CWProperty) defined there. Closes #2497697
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
637 |
# then load appobjects into the registry |
8537
e30d0a7f0087
[config] turn internal configuration methods building appobjects search path into normal method rather than class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
638 |
vpath = config.build_appobjects_path(reversed(config.cubes_path()), |
e30d0a7f0087
[config] turn internal configuration methods building appobjects search path into normal method rather than class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
639 |
evobjpath=esubpath, |
e30d0a7f0087
[config] turn internal configuration methods building appobjects search path into normal method rather than class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8404
diff
changeset
|
640 |
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
|
641 |
self.vreg.register_objects(vpath) |
1524 | 642 |
|
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
643 |
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
|
644 |
"""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
|
645 |
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
|
646 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
647 |
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
|
648 |
""" |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
649 |
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
|
650 |
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
|
651 |
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
|
652 |
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
|
653 |
DBAPIRequest.get_page_data = fake |
3833
5507aa75f601
add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3832
diff
changeset
|
654 |
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
|
655 |
# 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
|
656 |
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
|
657 |
self.vreg.config.uiprops = {} |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
658 |
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
|
659 |
# 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
|
660 |
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
|
661 |
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
|
662 |
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
|
663 |
|
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
664 |
def request(self): |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
665 |
if self._web_request: |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
666 |
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
|
667 |
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
|
668 |
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
|
669 |
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
|
670 |
req, session, user) |
6613
e7ff604491b2
[dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6581
diff
changeset
|
671 |
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
|
672 |
else: |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
673 |
req = DBAPIRequest(self.vreg) |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
674 |
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
|
675 |
return req |
0 | 676 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
677 |
@check_not_closed |
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
678 |
def user(self, req=None, props=None): |
0 | 679 |
"""return the User object associated to this connection""" |
680 |
# 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
|
681 |
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
|
682 |
props) |
0 | 683 |
if req is None: |
684 |
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
|
685 |
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
|
686 |
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
|
687 |
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
|
688 |
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
|
689 |
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
|
690 |
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
|
691 |
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
|
692 |
user.cw_attr_cache['login'] = login # cache login |
0 | 693 |
return user |
694 |
||
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
695 |
@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
|
696 |
def check(self): |
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
697 |
"""raise `BadConnectionId` if the connection is no more valid, else |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
698 |
return its latest activity timestamp. |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
699 |
""" |
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
|
700 |
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
|
701 |
|
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
702 |
def _txid(self, cursor=None): # pylint: disable=E0202 |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
703 |
# XXX could now handle various isolation level! |
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
704 |
# 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
|
705 |
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
|
706 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
707 |
# 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
|
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 |
@check_not_closed |
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
710 |
def get_shared_data(self, key, default=None, pop=False, txdata=False): |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
711 |
"""return value associated to key in the session's data dictionary or |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
712 |
session's transaction's data if `txdata` is true. |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
713 |
|
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
714 |
If pop is True, value will be removed from the dictionary. |
1524 | 715 |
|
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7962
diff
changeset
|
716 |
If key isn't defined in the dictionary, value specified by the |
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
717 |
`default` argument will be returned. |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
718 |
""" |
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
719 |
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
|
720 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
721 |
@check_not_closed |
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
722 |
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
|
723 |
"""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
|
724 |
|
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
725 |
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
|
726 |
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>
diff
changeset
|
727 |
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
|
728 |
""" |
6279
42079f752a9c
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
729 |
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
|
730 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
731 |
# 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
|
732 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
733 |
@check_not_closed |
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
734 |
def source_defs(self): |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
735 |
"""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
|
736 |
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
|
737 |
|
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6494
diff
changeset
|
738 |
@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
|
739 |
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
|
740 |
"""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
|
741 |
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
|
742 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
743 |
@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
|
744 |
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
|
745 |
"""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
|
746 |
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
|
747 |
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
|
748 |
""" |
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
|
749 |
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
|
750 |
|
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
|
751 |
@check_not_closed |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
752 |
def describe(self, eid, asdict=False): |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
753 |
metas = self._repo.describe(self.sessionid, eid, **self._txid()) |
7650
278fe9c1f3ad
[repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
754 |
if len(metas) == 3: # backward compat |
278fe9c1f3ad
[repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
755 |
metas = list(metas) |
278fe9c1f3ad
[repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
756 |
metas.append(metas[1]) |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
757 |
if asdict: |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
758 |
return dict(zip(('type', 'source', 'extid', 'asource'), metas)) |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
759 |
# XXX :-1 for cw compat, use asdict=True for full information |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
760 |
return metas[:-1] |
1524 | 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 |
# 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
|
763 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
764 |
@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
|
765 |
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
|
766 |
"""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
|
767 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
768 |
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
|
769 |
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
|
770 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
771 |
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
|
772 |
""" |
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
773 |
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
|
774 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
775 |
@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
|
776 |
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
|
777 |
"""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
|
778 |
support. |
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
779 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
780 |
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
|
781 |
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
|
782 |
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
|
783 |
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
|
784 |
""" |
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
785 |
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
|
786 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
787 |
@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
|
788 |
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
|
789 |
"""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
|
790 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
791 |
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
|
792 |
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
|
793 |
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
|
794 |
""" |
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
795 |
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
|
796 |
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
|
797 |
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
|
798 |
|
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
799 |
@check_not_closed |
0 | 800 |
def close(self): |
801 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 802 |
|
0 | 803 |
The connection will be unusable from this point forward; an Error (or |
804 |
subclass) exception will be raised if any operation is attempted with |
|
805 |
the connection. The same applies to all cursor objects trying to use the |
|
806 |
connection. Note that closing a connection without committing the |
|
807 |
changes first will cause an implicit rollback to be performed. |
|
808 |
""" |
|
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
|
809 |
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
|
810 |
del self._repo # necessary for proper garbage collection |
0 | 811 |
self._closed = 1 |
812 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
813 |
# undo support ############################################################ |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
814 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
815 |
@check_not_closed |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
816 |
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
|
817 |
"""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
|
818 |
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
|
819 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
820 |
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
|
821 |
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
|
822 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
823 |
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
|
824 |
named arguments: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
825 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
826 |
* `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
|
827 |
of the given type |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
828 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
829 |
* `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
|
830 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
831 |
* `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
|
832 |
'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
|
833 |
'D'. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
834 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
835 |
* `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
|
836 |
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
|
837 |
and set to false. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
838 |
""" |
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
|
839 |
actionfilters.update(self._txid()) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
840 |
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
|
841 |
**actionfilters) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
842 |
if req is None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
843 |
req = self.request() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
844 |
for txinfo in txinfos: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
845 |
txinfo.req = req |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
846 |
return txinfos |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
847 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
848 |
@check_not_closed |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
849 |
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
|
850 |
"""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
|
851 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
852 |
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
|
853 |
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
|
854 |
him). |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
855 |
""" |
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
|
856 |
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
|
857 |
**self._txid()) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
858 |
if req is None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
859 |
req = self.request() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
860 |
txinfo.req = req |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
861 |
return txinfo |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
862 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
863 |
@check_not_closed |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
864 |
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
|
865 |
"""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
|
866 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
867 |
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
|
868 |
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
|
869 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
870 |
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
|
871 |
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
|
872 |
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
|
873 |
""" |
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
|
874 |
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
|
875 |
**self._txid()) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
876 |
|
6252
d33509b79efe
[dbapi] reorganize Connection methods and use a @check_not_closed decorator
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
877 |
@check_not_closed |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
878 |
def undo_transaction(self, txuuid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
879 |
"""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
|
880 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
881 |
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
|
882 |
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
|
883 |
him). |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
884 |
""" |
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
|
885 |
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
|
886 |
**self._txid()) |
8669
62213a34726e
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8596
diff
changeset
|
887 |
|
8673
8ea63a2cc2cc
[db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8670
diff
changeset
|
888 |
in_memory_cnx = deprecated('[3.16] use _repo_connect instead)')(_repo_connect) |