author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 30 Mar 2010 11:15:57 +0200 | |
branch | stable |
changeset 5073 | a9697325cffa |
parent 5072 | 072ae171aeb0 |
child 5079 | e646047f80cb |
permissions | -rw-r--r-- |
0 | 1 |
"""Defines the central class for the CubicWeb RQL server: the repository. |
2 |
||
3 |
The repository is an abstraction allowing execution of rql queries against |
|
4 |
data sources. Most of the work is actually done in helper classes. The |
|
5 |
repository mainly: |
|
6 |
||
7 |
* brings these classes all together to provide a single access |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
8 |
point to a cubicweb instance. |
0 | 9 |
* handles session management |
10 |
* provides method for pyro registration, to call if pyro is enabled |
|
11 |
||
12 |
||
13 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3757
diff
changeset
|
14 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 15 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
16 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 17 |
""" |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
18 |
from __future__ import with_statement |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
19 |
|
0 | 20 |
__docformat__ = "restructuredtext en" |
21 |
||
22 |
import sys |
|
23 |
import Queue |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
24 |
from os.path import join |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
636
diff
changeset
|
25 |
from datetime import datetime |
0 | 26 |
from time import time, localtime, strftime |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
27 |
#from pickle import dumps |
0 | 28 |
|
29 |
from logilab.common.decorators import cached |
|
3757 | 30 |
from logilab.common.compat import any |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
31 |
from logilab.common import flatten |
0 | 32 |
|
33 |
from yams import BadSchemaDefinition |
|
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
34 |
from yams.schema import role_name |
0 | 35 |
from rql import RQLSyntaxError |
36 |
||
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
37 |
from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, |
2709
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
38 |
UnknownEid, AuthenticationError, ExecutionError, |
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
39 |
ETypeNotSupportedBySources, MultiSourcesError, |
0 | 40 |
BadConnectionId, Unauthorized, ValidationError, |
2709
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
41 |
typed_eid) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
42 |
from cubicweb import cwvreg, schema, server |
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
43 |
from cubicweb.server import utils, hook, pool, querier, sources |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
44 |
from cubicweb.server.session import Session, InternalSession, security_enabled |
0 | 45 |
|
46 |
||
47 |
def del_existing_rel_if_needed(session, eidfrom, rtype, eidto): |
|
48 |
"""delete existing relation when adding a new one if card is 1 or ? |
|
49 |
||
50 |
have to be done once the new relation has been inserted to avoid having |
|
51 |
an entity without a relation for some time |
|
52 |
||
53 |
this kind of behaviour has to be done in the repository so we don't have |
|
54 |
hooks order hazardness |
|
55 |
""" |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
56 |
# skip that for internal session or if integrity explicitly disabled |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
57 |
# |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
58 |
# XXX we should imo rely on the orm to first fetch existing entity if any |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
59 |
# then delete it. |
4756
a2c73b272c9b
do not check for existant entity when relation of single card is added but integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
60 |
if session.is_internal_session \ |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
61 |
or not session.is_hook_category_activated('integrity'): |
4687
082e66184f71
[integrity] skip auto-delete for internal sessions (but *not* for super session, see 4642:921737d2e3a8)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4686
diff
changeset
|
62 |
return |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
63 |
card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality') |
0 | 64 |
# one may be tented to check for neweids but this may cause more than one |
65 |
# relation even with '1?' cardinality if thoses relations are added in the |
|
66 |
# same transaction where the entity is being created. This never occurs from |
|
67 |
# the web interface but may occurs during test or dbapi connection (though |
|
68 |
# not expected for this). So: don't do it, we pretend to ensure repository |
|
69 |
# consistency. |
|
4673
6f8b925a29f4
auto-delete relation with single cardinality using execute, not unsafe_execute, so permissions are correctly checked
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4643
diff
changeset
|
70 |
# |
4686
c55606a5c4b0
[integrity] temporary fix auto-delete relation (see 4673:6f8b925a29f4 and 4642:921737d2e3a8): we want no read perms, though we want to check delete perms. Should be properly fixed in 3.7 once we've proper security control and [unsafe_]execute behaviour on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4673
diff
changeset
|
71 |
# XXX we don't want read permissions to be applied but we want delete |
c55606a5c4b0
[integrity] temporary fix auto-delete relation (see 4673:6f8b925a29f4 and 4642:921737d2e3a8): we want no read perms, though we want to check delete perms. Should be properly fixed in 3.7 once we've proper security control and [unsafe_]execute behaviour on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4673
diff
changeset
|
72 |
# permission to be checked |
c55606a5c4b0
[integrity] temporary fix auto-delete relation (see 4673:6f8b925a29f4 and 4642:921737d2e3a8): we want no read perms, though we want to check delete perms. Should be properly fixed in 3.7 once we've proper security control and [unsafe_]execute behaviour on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4673
diff
changeset
|
73 |
rschema = session.repo.schema.rschema(rtype) |
0 | 74 |
if card[0] in '1?': |
4643
921737d2e3a8
fix optimisation with super session that may lead to integrity loss
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
75 |
if not rschema.inlined: # inlined relations will be implicitly deleted |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
76 |
with security_enabled(session, read=False): |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
77 |
session.execute('DELETE X %s Y WHERE X eid %%(x)s, ' |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
78 |
'NOT Y eid %%(y)s' % rtype, |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
79 |
{'x': eidfrom, 'y': eidto}, 'x') |
0 | 80 |
if card[1] in '1?': |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
81 |
with security_enabled(session, read=False): |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
82 |
session.execute('DELETE X %sY WHERE Y eid %%(y)s, ' |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
83 |
'NOT X eid %%(x)s' % rtype, |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
84 |
{'x': eidfrom, 'y': eidto}, 'y') |
1482 | 85 |
|
3694 | 86 |
|
0 | 87 |
class Repository(object): |
88 |
"""a repository provides access to a set of persistent storages for |
|
89 |
entities and relations |
|
90 |
||
91 |
XXX protect pyro access |
|
92 |
""" |
|
1482 | 93 |
|
0 | 94 |
def __init__(self, config, vreg=None, debug=False): |
95 |
self.config = config |
|
96 |
if vreg is None: |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
97 |
vreg = cwvreg.CubicWebVRegistry(config, debug) |
0 | 98 |
self.vreg = vreg |
99 |
self.pyro_registered = False |
|
100 |
self.info('starting repository from %s', self.config.apphome) |
|
101 |
# dictionary of opened sessions |
|
102 |
self._sessions = {} |
|
103 |
# list of functions to be called at regular interval |
|
104 |
self._looping_tasks = [] |
|
105 |
# list of running threads |
|
106 |
self._running_threads = [] |
|
107 |
# initial schema, should be build or replaced latter |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
108 |
self.schema = schema.CubicWebSchema(config.appid) |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
109 |
self.vreg.schema = self.schema # until actual schema is loaded... |
0 | 110 |
# querier helper, need to be created after sources initialization |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
111 |
self.querier = querier.QuerierHelper(self, self.schema) |
0 | 112 |
# sources |
113 |
self.sources = [] |
|
114 |
self.sources_by_uri = {} |
|
115 |
# FIXME: store additional sources info in the system database ? |
|
116 |
# FIXME: sources should be ordered (add_entity priority) |
|
117 |
for uri, source_config in config.sources().items(): |
|
118 |
if uri == 'admin': |
|
119 |
# not an actual source |
|
1482 | 120 |
continue |
0 | 121 |
source = self.get_source(uri, source_config) |
122 |
self.sources_by_uri[uri] = source |
|
123 |
self.sources.append(source) |
|
124 |
self.system_source = self.sources_by_uri['system'] |
|
125 |
# ensure system source is the first one |
|
126 |
self.sources.remove(self.system_source) |
|
127 |
self.sources.insert(0, self.system_source) |
|
128 |
# cache eid -> type / source |
|
129 |
self._type_source_cache = {} |
|
130 |
# cache (extid, source uri) -> eid |
|
131 |
self._extid_cache = {} |
|
132 |
# open some connections pools |
|
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
133 |
if config.open_connections_pools: |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
134 |
self.open_connections_pools() |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
135 |
|
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
136 |
def _bootstrap_hook_registry(self): |
3424
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
137 |
"""called during bootstrap since we need the metadata hooks""" |
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
138 |
hooksdirectory = join(CW_SOFTWARE_ROOT, 'hooks') |
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
139 |
self.vreg.init_registration([hooksdirectory]) |
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
140 |
self.vreg.load_file(join(hooksdirectory, 'metadata.py'), |
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
141 |
'cubicweb.hooks.metadata') |
60c9722a590b
[tests] force hook registry instanciation for test configs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3409
diff
changeset
|
142 |
|
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
143 |
def open_connections_pools(self): |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
144 |
config = self.config |
0 | 145 |
self._available_pools = Queue.Queue() |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
146 |
self._available_pools.put_nowait(pool.ConnectionsPool(self.sources)) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
147 |
if config.quick_start: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
148 |
# quick start, usually only to get a minimal repository to get cubes |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
149 |
# information (eg dump/restore/ |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
150 |
config._cubes = () |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
151 |
self.set_schema(config.load_schema(), resetvreg=False) |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
152 |
config['connections-pool-size'] = 1 |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
153 |
config._cubes = None |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
154 |
elif config.creating: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
155 |
# repository creation |
0 | 156 |
config.bootstrap_cubes() |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
157 |
self.set_schema(config.load_schema(), resetvreg=False) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
158 |
# need to load the Any and CWUser entity types |
0 | 159 |
etdirectory = join(CW_SOFTWARE_ROOT, 'entities') |
1317 | 160 |
self.vreg.init_registration([etdirectory]) |
3409
c684606a95e0
[bootstrap] metadata hooks need to be registered at bootstrap time for db creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3381
diff
changeset
|
161 |
for modname in ('__init__', 'authobjs', 'wfobjs'): |
c684606a95e0
[bootstrap] metadata hooks need to be registered at bootstrap time for db creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3381
diff
changeset
|
162 |
self.vreg.load_file(join(etdirectory, '%s.py' % modname), |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
163 |
'cubicweb.entities.%s' % modname) |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
164 |
self._bootstrap_hook_registry() |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
165 |
elif config.read_instance_schema: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
166 |
# normal start: load the instance schema from the database |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
167 |
self.fill_schema() |
0 | 168 |
else: |
169 |
# test start: use the file system schema (quicker) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
170 |
self.warning("set fs instance'schema") |
0 | 171 |
config.bootstrap_cubes() |
2961 | 172 |
self.set_schema(config.load_schema()) |
0 | 173 |
if not config.creating: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
174 |
if 'CWProperty' in self.schema: |
0 | 175 |
self.vreg.init_properties(self.properties()) |
176 |
# call source's init method to complete their initialisation if |
|
177 |
# needed (for instance looking for persistent configuration using an |
|
178 |
# internal session, which is not possible until pools have been |
|
179 |
# initialized) |
|
180 |
for source in self.sources: |
|
181 |
source.init() |
|
182 |
else: |
|
183 |
# call init_creating so for instance native source can configurate |
|
184 |
# tsearch according to postgres version |
|
185 |
for source in self.sources: |
|
186 |
source.init_creating() |
|
187 |
# close initialization pool and reopen fresh ones for proper |
|
188 |
# initialization now that we know cubes |
|
1482 | 189 |
self._get_pool().close(True) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
190 |
# list of available pools (we can't iterated on Queue instance) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
191 |
self.pools = [] |
0 | 192 |
for i in xrange(config['connections-pool-size']): |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
193 |
self.pools.append(pool.ConnectionsPool(self.sources)) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
194 |
self._available_pools.put_nowait(self.pools[-1]) |
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
195 |
self._shutting_down = False |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
196 |
if config.quick_start: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
197 |
config.init_cubes(self.get_cubes()) |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
198 |
else: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
199 |
self.hm = self.vreg['hooks'] |
1482 | 200 |
|
0 | 201 |
# internals ############################################################### |
202 |
||
203 |
def get_source(self, uri, source_config): |
|
204 |
source_config['uri'] = uri |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
205 |
return sources.get_source(source_config, self.schema, self) |
1482 | 206 |
|
2963
12ad88615a12
test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2961
diff
changeset
|
207 |
def set_schema(self, schema, resetvreg=True, rebuildinfered=True): |
12ad88615a12
test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2961
diff
changeset
|
208 |
if rebuildinfered: |
12ad88615a12
test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2961
diff
changeset
|
209 |
schema.rebuild_infered_relations() |
0 | 210 |
self.info('set schema %s %#x', schema.name, id(schema)) |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
211 |
if resetvreg: |
2881
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
212 |
if self.config._cubes is None: |
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
213 |
self.config.init_cubes(self.get_cubes()) |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
214 |
# full reload of all appobjects |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
215 |
self.vreg.reset() |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
216 |
self.vreg.set_schema(schema) |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
217 |
else: |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3042
diff
changeset
|
218 |
self.vreg._set_schema(schema) |
0 | 219 |
self.querier.set_schema(schema) |
220 |
for source in self.sources: |
|
221 |
source.set_schema(schema) |
|
222 |
self.schema = schema |
|
223 |
||
224 |
def fill_schema(self): |
|
225 |
"""lod schema from the repository""" |
|
226 |
from cubicweb.server.schemaserial import deserialize_schema |
|
227 |
self.info('loading schema from the repository') |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
228 |
appschema = schema.CubicWebSchema(self.config.appid) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
229 |
self.set_schema(self.config.load_bootstrap_schema(), resetvreg=False) |
0 | 230 |
self.debug('deserializing db schema into %s %#x', appschema.name, id(appschema)) |
231 |
session = self.internal_session() |
|
232 |
try: |
|
233 |
try: |
|
234 |
deserialize_schema(appschema, session) |
|
235 |
except BadSchemaDefinition: |
|
236 |
raise |
|
237 |
except Exception, ex: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
238 |
import traceback |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
239 |
traceback.print_exc() |
1482 | 240 |
raise Exception('Is the database initialised ? (cause: %s)' % |
0 | 241 |
(ex.args and ex.args[0].strip() or 'unknown')), \ |
242 |
None, sys.exc_info()[-1] |
|
243 |
finally: |
|
244 |
session.close() |
|
245 |
self.set_schema(appschema) |
|
1482 | 246 |
|
0 | 247 |
def start_looping_tasks(self): |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
248 |
if not (self.config.creating or self.config.repairing |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
249 |
or self.config.quick_start): |
4958
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
250 |
# call instance level initialisation hooks |
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
251 |
self.hm.call_hooks('server_startup', repo=self) |
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
252 |
# register a task to cleanup expired session |
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
253 |
self.looping_task(self.config['session-time']/3., self.clean_sessions) |
0 | 254 |
assert isinstance(self._looping_tasks, list), 'already started' |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
255 |
for i, (interval, func, args) in enumerate(self._looping_tasks): |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
256 |
self._looping_tasks[i] = task = utils.LoopTask(interval, func, args) |
0 | 257 |
self.info('starting task %s with interval %.2fs', task.name, |
258 |
interval) |
|
259 |
task.start() |
|
260 |
# ensure no tasks will be further added |
|
261 |
self._looping_tasks = tuple(self._looping_tasks) |
|
262 |
||
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
263 |
def looping_task(self, interval, func, *args): |
0 | 264 |
"""register a function to be called every `interval` seconds. |
1482 | 265 |
|
0 | 266 |
looping tasks can only be registered during repository initialization, |
267 |
once done this method will fail. |
|
268 |
""" |
|
269 |
try: |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
270 |
self._looping_tasks.append( (interval, func, args) ) |
0 | 271 |
except AttributeError: |
272 |
raise RuntimeError("can't add looping task once the repository is started") |
|
273 |
||
274 |
def threaded_task(self, func): |
|
275 |
"""start function in a separated thread""" |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
276 |
t = utils.RepoThread(func, self._running_threads) |
0 | 277 |
t.start() |
1482 | 278 |
|
0 | 279 |
#@locked |
280 |
def _get_pool(self): |
|
281 |
try: |
|
282 |
return self._available_pools.get(True, timeout=5) |
|
283 |
except Queue.Empty: |
|
284 |
raise Exception('no pool available after 5 secs, probably either a ' |
|
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
285 |
'bug in code (too many uncommited/rollbacked ' |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
286 |
'connections) or too much load on the server (in ' |
0 | 287 |
'which case you can try to set a bigger ' |
288 |
'connections pools size)') |
|
1482 | 289 |
|
0 | 290 |
def _free_pool(self, pool): |
291 |
self._available_pools.put_nowait(pool) |
|
292 |
||
293 |
def pinfo(self): |
|
294 |
# XXX: session.pool is accessed from a local storage, would be interesting |
|
295 |
# to see if there is a pool set in any thread specific data) |
|
296 |
import threading |
|
297 |
return '%s: %s (%s)' % (self._available_pools.qsize(), |
|
298 |
','.join(session.user.login for session in self._sessions.values() |
|
299 |
if session.pool), |
|
300 |
threading.currentThread()) |
|
301 |
def shutdown(self): |
|
302 |
"""called on server stop event to properly close opened sessions and |
|
303 |
connections |
|
304 |
""" |
|
4776
3bf51379baee
assert we are not already shutting down
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
305 |
assert not self._shutting_down, 'already shutting down' |
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
306 |
self._shutting_down = True |
0 | 307 |
if isinstance(self._looping_tasks, tuple): # if tasks have been started |
308 |
for looptask in self._looping_tasks: |
|
309 |
self.info('canceling task %s...', looptask.name) |
|
310 |
looptask.cancel() |
|
311 |
looptask.join() |
|
312 |
self.info('task %s finished', looptask.name) |
|
313 |
for thread in self._running_threads: |
|
314 |
self.info('waiting thread %s...', thread.name) |
|
315 |
thread.join() |
|
316 |
self.info('thread %s finished', thread.name) |
|
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
317 |
if not (self.config.creating or self.config.repairing |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
318 |
or self.config.quick_start): |
4946
2654cd5c87e0
[repo] don't call server_shutdown hooks when creating/repairing instance to be consistence with server_startup hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4862
diff
changeset
|
319 |
self.hm.call_hooks('server_shutdown', repo=self) |
0 | 320 |
self.close_sessions() |
321 |
while not self._available_pools.empty(): |
|
322 |
pool = self._available_pools.get_nowait() |
|
323 |
try: |
|
324 |
pool.close(True) |
|
325 |
except: |
|
326 |
self.exception('error while closing %s' % pool) |
|
327 |
continue |
|
328 |
if self.pyro_registered: |
|
329 |
pyro_unregister(self.config) |
|
330 |
hits, misses = self.querier.cache_hit, self.querier.cache_miss |
|
331 |
try: |
|
332 |
self.info('rqlt st cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
333 |
(hits * 100) / (hits + misses)) |
|
334 |
hits, misses = self.system_source.cache_hit, self.system_source.cache_miss |
|
335 |
self.info('sql cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
336 |
(hits * 100) / (hits + misses)) |
|
337 |
nocache = self.system_source.no_cache |
|
338 |
self.info('sql cache usage: %s/%s (%s%%)', hits+ misses, nocache, |
|
339 |
((hits + misses) * 100) / (hits + misses + nocache)) |
|
340 |
except ZeroDivisionError: |
|
341 |
pass |
|
1482 | 342 |
|
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4706
diff
changeset
|
343 |
def stats(self): # XXX restrict to managers session? |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4706
diff
changeset
|
344 |
import threading |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
345 |
results = {} |
4862
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
346 |
querier = self.querier |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
347 |
source = self.system_source |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
348 |
for size, maxsize, hits, misses, title in ( |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
349 |
(len(querier._rql_cache), self.config['rql-cache-size'], |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
350 |
querier.cache_hit, querier.cache_miss, 'rqlt_st'), |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
351 |
(len(source._cache), self.config['rql-cache-size'], |
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
352 |
source.cache_hit, source.cache_miss, 'sql'), |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
353 |
): |
4862
0d0e5279f65e
[repo stats] add cache size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
354 |
results['%s_cache_size' % title] = '%s / %s' % (size, maxsize) |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
355 |
results['%s_cache_hit' % title] = hits |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
356 |
results['%s_cache_miss' % title] = misses |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
357 |
results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses) |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
358 |
results['sql_no_cache'] = self.system_source.no_cache |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
359 |
results['nb_open_sessions'] = len(self._sessions) |
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4706
diff
changeset
|
360 |
results['nb_active_threads'] = threading.activeCount() |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4706
diff
changeset
|
361 |
results['looping_tasks'] = ', '.join(str(t) for t in self._looping_tasks) |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
362 |
results['available_pools'] = self._available_pools.qsize() |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
363 |
return results |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
364 |
|
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
365 |
def _login_from_email(self, login): |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
366 |
session = self.internal_session() |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
367 |
try: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
368 |
rset = session.execute('Any L WHERE U login L, U primary_email M, ' |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
369 |
'M address %(login)s', {'login': login}, |
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
370 |
build_descr=False) |
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
371 |
if rset.rowcount == 1: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
372 |
login = rset[0][0] |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
373 |
finally: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
374 |
session.close() |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
375 |
return login |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
376 |
|
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:
3629
diff
changeset
|
377 |
def authenticate_user(self, session, login, **kwargs): |
0 | 378 |
"""validate login / password, raise AuthenticationError on failure |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
379 |
return associated CWUser instance on success |
0 | 380 |
""" |
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
381 |
if self.vreg.config['allow-email-login'] and '@' in login: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
382 |
login = self._login_from_email(login) |
0 | 383 |
for source in self.sources: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
384 |
if source.support_entity('CWUser'): |
0 | 385 |
try: |
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:
3629
diff
changeset
|
386 |
eid = source.authenticate(session, login, **kwargs) |
0 | 387 |
break |
388 |
except AuthenticationError: |
|
389 |
continue |
|
390 |
else: |
|
391 |
raise AuthenticationError('authentication failed with all sources') |
|
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
392 |
cwuser = self._build_user(session, eid) |
0 | 393 |
if self.config.consider_user_state and \ |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
394 |
not cwuser.state in cwuser.AUTHENTICABLE_STATES: |
0 | 395 |
raise AuthenticationError('user is not in authenticable state') |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
396 |
return cwuser |
0 | 397 |
|
398 |
def _build_user(self, session, eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
399 |
"""return a CWUser entity for user with the given eid""" |
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:
2647
diff
changeset
|
400 |
cls = self.vreg['etypes'].etype_class('CWUser') |
0 | 401 |
rql = cls.fetch_rql(session.user, ['X eid %(x)s']) |
402 |
rset = session.execute(rql, {'x': eid}, 'x') |
|
403 |
assert len(rset) == 1, rset |
|
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
404 |
cwuser = rset.get_entity(0, 0) |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
405 |
# pylint: disable-msg=W0104 |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
406 |
# prefetch / cache cwuser's groups and properties. This is especially |
0 | 407 |
# useful for internal sessions to avoid security insertions |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
408 |
cwuser.groups |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
409 |
cwuser.properties |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
410 |
return cwuser |
1482 | 411 |
|
0 | 412 |
# public (dbapi) interface ################################################ |
1482 | 413 |
|
0 | 414 |
def get_schema(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
415 |
"""return the instance schema. This is a public method, not |
0 | 416 |
requiring a session id |
417 |
""" |
|
418 |
try: |
|
419 |
# necessary to support pickling used by pyro |
|
420 |
self.schema.__hashmode__ = 'pickle' |
|
421 |
return self.schema |
|
422 |
finally: |
|
423 |
self.schema.__hashmode__ = None |
|
424 |
||
425 |
def get_cubes(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
426 |
"""return the list of cubes used by this instance. This is a |
0 | 427 |
public method, not requiring a session id. |
428 |
""" |
|
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
429 |
versions = self.get_versions(not (self.config.creating |
4689
4eb1f4490538
[test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4687
diff
changeset
|
430 |
or self.config.repairing |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
431 |
or self.config.quick_start |
4689
4eb1f4490538
[test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4687
diff
changeset
|
432 |
or self.config.mode == 'test')) |
0 | 433 |
cubes = list(versions) |
434 |
cubes.remove('cubicweb') |
|
435 |
return cubes |
|
436 |
||
437 |
@cached |
|
438 |
def get_versions(self, checkversions=False): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
439 |
"""return the a dictionary containing cubes used by this instance |
0 | 440 |
as key with their version as value, including cubicweb version. This is a |
441 |
public method, not requiring a session id. |
|
442 |
""" |
|
443 |
from logilab.common.changelog import Version |
|
444 |
vcconf = {} |
|
445 |
session = self.internal_session() |
|
446 |
try: |
|
447 |
for pk, version in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
448 |
'Any K,V WHERE P is CWProperty, P value V, P pkey K, ' |
0 | 449 |
'P pkey ~="system.version.%"', build_descr=False): |
450 |
cube = pk.split('.')[-1] |
|
451 |
# XXX cubicweb migration |
|
452 |
if cube in CW_MIGRATION_MAP: |
|
453 |
cube = CW_MIGRATION_MAP[cube] |
|
454 |
version = Version(version) |
|
455 |
vcconf[cube] = version |
|
456 |
if checkversions: |
|
457 |
if cube != 'cubicweb': |
|
458 |
fsversion = self.config.cube_version(cube) |
|
459 |
else: |
|
460 |
fsversion = self.config.cubicweb_version() |
|
461 |
if version < fsversion: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
462 |
msg = ('instance has %s version %s but %s ' |
0 | 463 |
'is installed. Run "cubicweb-ctl upgrade".') |
464 |
raise ExecutionError(msg % (cube, version, fsversion)) |
|
465 |
finally: |
|
466 |
session.close() |
|
467 |
return vcconf |
|
1482 | 468 |
|
0 | 469 |
@cached |
470 |
def source_defs(self): |
|
471 |
sources = self.config.sources().copy() |
|
472 |
# remove manager information |
|
473 |
sources.pop('admin', None) |
|
474 |
# remove sensitive information |
|
475 |
for uri, sourcedef in sources.iteritems(): |
|
476 |
sourcedef = sourcedef.copy() |
|
477 |
self.sources_by_uri[uri].remove_sensitive_information(sourcedef) |
|
478 |
sources[uri] = sourcedef |
|
479 |
return sources |
|
480 |
||
481 |
def properties(self): |
|
482 |
"""return a result set containing system wide properties""" |
|
483 |
session = self.internal_session() |
|
484 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
485 |
return session.execute('Any K,V WHERE P is CWProperty,' |
0 | 486 |
'P pkey K, P value V, NOT P for_user U', |
487 |
build_descr=False) |
|
488 |
finally: |
|
489 |
session.close() |
|
490 |
||
5032 | 491 |
# XXX protect this method: anonymous should be allowed and registration |
492 |
# plugged |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
493 |
def register_user(self, login, password, email=None, **kwargs): |
0 | 494 |
"""check a user with the given login exists, if not create it with the |
495 |
given password. This method is designed to be used for anonymous |
|
496 |
registration on public web site. |
|
497 |
""" |
|
498 |
session = self.internal_session() |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
499 |
# for consistency, keep same error as unique check hook (although not required) |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
500 |
errmsg = session._('the value "%s" is already used, use another one') |
0 | 501 |
try: |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
502 |
if (session.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
503 |
build_descr=False) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
504 |
or session.execute('CWUser X WHERE X use_email C, C address %(login)s', |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
505 |
{'login': login}, build_descr=False)): |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
506 |
qname = role_name('login', 'subject') |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
507 |
raise ValidationError(None, {qname: errmsg % login}) |
0 | 508 |
# we have to create the user |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
509 |
user = self.vreg['etypes'].etype_class('CWUser')(session) |
0 | 510 |
if isinstance(password, unicode): |
511 |
# password should *always* be utf8 encoded |
|
512 |
password = password.encode('UTF8') |
|
513 |
kwargs['login'] = login |
|
514 |
kwargs['upassword'] = password |
|
515 |
user.update(kwargs) |
|
516 |
self.glob_add_entity(session, user) |
|
517 |
session.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
|
518 |
{'x': user.eid}) |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
519 |
if email or '@' in login: |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
520 |
d = {'login': login, 'email': email or login} |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
521 |
if session.execute('EmailAddress X WHERE X address %(email)s', d, |
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
522 |
build_descr=False): |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
523 |
qname = role_name('address', 'subject') |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
524 |
raise ValidationError(None, {qname: errmsg % d['email']}) |
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
525 |
session.execute('INSERT EmailAddress X: X address %(email)s, ' |
5032 | 526 |
'U primary_email X, U use_email X ' |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
527 |
'WHERE U login %(login)s', d, build_descr=False) |
0 | 528 |
session.commit() |
529 |
finally: |
|
530 |
session.close() |
|
594
76218d42d21f
return success or not on creation of user
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
479
diff
changeset
|
531 |
return True |
1482 | 532 |
|
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:
3629
diff
changeset
|
533 |
def connect(self, login, **kwargs): |
0 | 534 |
"""open a connection for a given user |
535 |
||
536 |
base_url may be needed to send mails |
|
537 |
cnxtype indicate if this is a pyro connection or a in-memory connection |
|
1482 | 538 |
|
0 | 539 |
raise `AuthenticationError` if the authentication failed |
540 |
raise `ConnectionError` if we can't open a connection |
|
541 |
""" |
|
542 |
# use an internal connection |
|
543 |
session = self.internal_session() |
|
544 |
# try to get a user object |
|
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
545 |
cnxprops = kwargs.pop('cnxprops', None) |
0 | 546 |
try: |
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:
3629
diff
changeset
|
547 |
user = self.authenticate_user(session, login, **kwargs) |
0 | 548 |
finally: |
549 |
session.close() |
|
550 |
session = Session(user, self, cnxprops) |
|
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
551 |
user._cw = user.cw_rset.req = session |
0 | 552 |
user.clear_related_cache() |
553 |
self._sessions[session.id] = session |
|
554 |
self.info('opened %s', session) |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
555 |
self.hm.call_hooks('session_open', session) |
0 | 556 |
# commit session at this point in case write operation has been done |
557 |
# during `session_open` hooks |
|
558 |
session.commit() |
|
559 |
return session.id |
|
560 |
||
561 |
def execute(self, sessionid, rqlstring, args=None, eid_key=None, build_descr=True): |
|
562 |
"""execute a RQL query |
|
563 |
||
564 |
* rqlstring should be an unicode string or a plain ascii string |
|
565 |
* args the optional parameters used in the query |
|
566 |
* build_descr is a flag indicating if the description should be |
|
567 |
built on select queries |
|
568 |
""" |
|
569 |
session = self._get_session(sessionid, setpool=True) |
|
570 |
try: |
|
571 |
try: |
|
572 |
return self.querier.execute(session, rqlstring, args, eid_key, |
|
573 |
build_descr) |
|
574 |
except (Unauthorized, RQLSyntaxError): |
|
575 |
raise |
|
576 |
except ValidationError, ex: |
|
577 |
# need ValidationError normalization here so error may pass |
|
578 |
# through pyro |
|
579 |
if hasattr(ex.entity, 'eid'): |
|
580 |
ex.entity = ex.entity.eid # error raised by yams |
|
581 |
args = list(ex.args) |
|
582 |
args[0] = ex.entity |
|
583 |
ex.args = tuple(args) |
|
584 |
raise |
|
585 |
except: |
|
586 |
# FIXME: check error to catch internal errors |
|
587 |
self.exception('unexpected error') |
|
588 |
raise |
|
589 |
finally: |
|
590 |
session.reset_pool() |
|
1482 | 591 |
|
0 | 592 |
def describe(self, sessionid, eid): |
593 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
594 |
session = self._get_session(sessionid, setpool=True) |
|
595 |
try: |
|
596 |
return self.type_and_source_from_eid(eid, session) |
|
597 |
finally: |
|
598 |
session.reset_pool() |
|
599 |
||
600 |
def check_session(self, sessionid): |
|
5032 | 601 |
"""raise `BadConnectionId` if the connection is no more valid""" |
0 | 602 |
self._get_session(sessionid, setpool=False) |
603 |
||
604 |
def get_shared_data(self, sessionid, key, default=None, pop=False): |
|
605 |
"""return the session's data dictionary""" |
|
606 |
session = self._get_session(sessionid, setpool=False) |
|
607 |
return session.get_shared_data(key, default, pop) |
|
608 |
||
609 |
def set_shared_data(self, sessionid, key, value, querydata=False): |
|
610 |
"""set value associated to `key` in shared data |
|
611 |
||
612 |
if `querydata` is true, the value will be added to the repository |
|
613 |
session's query data which are cleared on commit/rollback of the current |
|
614 |
transaction, and won't be available through the connexion, only on the |
|
615 |
repository side. |
|
616 |
""" |
|
617 |
session = self._get_session(sessionid, setpool=False) |
|
618 |
session.set_shared_data(key, value, querydata) |
|
619 |
||
620 |
def commit(self, sessionid): |
|
621 |
"""commit transaction for the session with the given id""" |
|
622 |
self.debug('begin commit for session %s', sessionid) |
|
623 |
try: |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
624 |
return self._get_session(sessionid).commit() |
1482 | 625 |
except (ValidationError, Unauthorized): |
0 | 626 |
raise |
627 |
except: |
|
628 |
self.exception('unexpected error') |
|
629 |
raise |
|
1482 | 630 |
|
0 | 631 |
def rollback(self, sessionid): |
632 |
"""commit transaction for the session with the given id""" |
|
633 |
self.debug('begin rollback for session %s', sessionid) |
|
634 |
try: |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1664
diff
changeset
|
635 |
self._get_session(sessionid).rollback() |
0 | 636 |
except: |
637 |
self.exception('unexpected error') |
|
638 |
raise |
|
639 |
||
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
640 |
def close(self, sessionid, checkshuttingdown=True): |
0 | 641 |
"""close the session with the given id""" |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
642 |
session = self._get_session(sessionid, setpool=True, |
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
643 |
checkshuttingdown=checkshuttingdown) |
0 | 644 |
# operation uncommited before close are rollbacked before hook is called |
645 |
session.rollback() |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
646 |
self.hm.call_hooks('session_close', session) |
0 | 647 |
# commit session at this point in case write operation has been done |
648 |
# during `session_close` hooks |
|
649 |
session.commit() |
|
650 |
session.close() |
|
651 |
del self._sessions[sessionid] |
|
652 |
self.info('closed session %s for user %s', sessionid, session.user.login) |
|
1482 | 653 |
|
0 | 654 |
def user_info(self, sessionid, props=None): |
655 |
"""this method should be used by client to: |
|
656 |
* check session id validity |
|
657 |
* update user information on each user's request (i.e. groups and |
|
658 |
custom properties) |
|
659 |
""" |
|
660 |
session = self._get_session(sessionid, setpool=False) |
|
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:
2192
diff
changeset
|
661 |
if props is not None: |
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:
2192
diff
changeset
|
662 |
self.set_session_props(sessionid, props) |
0 | 663 |
user = session.user |
664 |
return user.eid, user.login, user.groups, user.properties |
|
1482 | 665 |
|
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:
2192
diff
changeset
|
666 |
def set_session_props(self, sessionid, props): |
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:
2192
diff
changeset
|
667 |
"""this method should be used by client to: |
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:
2192
diff
changeset
|
668 |
* check session id validity |
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:
2192
diff
changeset
|
669 |
* update user information on each user's request (i.e. groups and |
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:
2192
diff
changeset
|
670 |
custom properties) |
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:
2192
diff
changeset
|
671 |
""" |
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:
2192
diff
changeset
|
672 |
session = self._get_session(sessionid, setpool=False) |
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:
2192
diff
changeset
|
673 |
for prop, value in props.items(): |
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:
2192
diff
changeset
|
674 |
session.change_property(prop, value) |
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:
2192
diff
changeset
|
675 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
676 |
def undoable_transactions(self, sessionid, ueid=None, **actionfilters): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
677 |
"""See :class:`cubicweb.dbapi.Connection.undoable_transactions`""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
678 |
session = self._get_session(sessionid, setpool=True) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
679 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
680 |
return self.system_source.undoable_transactions(session, ueid, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
681 |
**actionfilters) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
682 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
683 |
session.reset_pool() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
684 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
685 |
def transaction_info(self, sessionid, txuuid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
686 |
"""See :class:`cubicweb.dbapi.Connection.transaction_info`""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
687 |
session = self._get_session(sessionid, setpool=True) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
688 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
689 |
return self.system_source.tx_info(session, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
690 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
691 |
session.reset_pool() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
692 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
693 |
def transaction_actions(self, sessionid, txuuid, public=True): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
694 |
"""See :class:`cubicweb.dbapi.Connection.transaction_actions`""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
695 |
session = self._get_session(sessionid, setpool=True) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
696 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
697 |
return self.system_source.tx_actions(session, txuuid, public) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
698 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
699 |
session.reset_pool() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
700 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
701 |
def undo_transaction(self, sessionid, txuuid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
702 |
"""See :class:`cubicweb.dbapi.Connection.undo_transaction`""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
703 |
session = self._get_session(sessionid, setpool=True) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
704 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
705 |
return self.system_source.undo_transaction(session, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
706 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
707 |
session.reset_pool() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
708 |
|
0 | 709 |
# public (inter-repository) interface ##################################### |
1482 | 710 |
|
0 | 711 |
def entities_modified_since(self, etypes, mtime): |
712 |
"""function designed to be called from an external repository which |
|
713 |
is using this one as a rql source for synchronization, and return a |
|
714 |
3-uple containing : |
|
715 |
* the local date |
|
716 |
* list of (etype, eid) of entities of the given types which have been |
|
717 |
modified since the given timestamp (actually entities whose full text |
|
718 |
index content has changed) |
|
719 |
* list of (etype, eid) of entities of the given types which have been |
|
720 |
deleted since the given timestamp |
|
721 |
""" |
|
722 |
session = self.internal_session() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
636
diff
changeset
|
723 |
updatetime = datetime.now() |
0 | 724 |
try: |
725 |
modentities, delentities = self.system_source.modified_entities( |
|
726 |
session, etypes, mtime) |
|
727 |
return updatetime, modentities, delentities |
|
728 |
finally: |
|
729 |
session.close() |
|
730 |
||
731 |
# session handling ######################################################## |
|
1482 | 732 |
|
0 | 733 |
def close_sessions(self): |
734 |
"""close every opened sessions""" |
|
735 |
for sessionid in self._sessions.keys(): |
|
736 |
try: |
|
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
737 |
self.close(sessionid, checkshuttingdown=False) |
0 | 738 |
except: |
739 |
self.exception('error while closing session %s' % sessionid) |
|
740 |
||
741 |
def clean_sessions(self): |
|
742 |
"""close sessions not used since an amount of time specified in the |
|
743 |
configuration |
|
744 |
""" |
|
745 |
mintime = time() - self.config['session-time'] |
|
746 |
self.debug('cleaning session unused since %s', |
|
747 |
strftime('%T', localtime(mintime))) |
|
748 |
nbclosed = 0 |
|
749 |
for session in self._sessions.values(): |
|
750 |
if session.timestamp < mintime: |
|
751 |
self.close(session.id) |
|
752 |
nbclosed += 1 |
|
753 |
return nbclosed |
|
1482 | 754 |
|
0 | 755 |
def internal_session(self, cnxprops=None): |
756 |
"""return a dbapi like connection/cursor using internal user which |
|
757 |
have every rights on the repository. You'll *have to* commit/rollback |
|
758 |
or close (rollback implicitly) the session once the job's done, else |
|
759 |
you'll leak connections pool up to the time where no more pool is |
|
760 |
available, causing irremediable freeze... |
|
761 |
""" |
|
762 |
session = InternalSession(self, cnxprops) |
|
763 |
session.set_pool() |
|
764 |
return session |
|
1482 | 765 |
|
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
766 |
def _get_session(self, sessionid, setpool=False, checkshuttingdown=True): |
0 | 767 |
"""return the user associated to the given session identifier""" |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
768 |
if checkshuttingdown and self._shutting_down: |
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
769 |
raise Exception('Repository is shutting down') |
0 | 770 |
try: |
771 |
session = self._sessions[sessionid] |
|
772 |
except KeyError: |
|
773 |
raise BadConnectionId('No such session %s' % sessionid) |
|
774 |
if setpool: |
|
775 |
session.set_pool() |
|
776 |
return session |
|
777 |
||
778 |
# data sources handling ################################################### |
|
779 |
# * correspondance between eid and (type, source) |
|
780 |
# * correspondance between eid and local id (i.e. specific to a given source) |
|
1482 | 781 |
|
0 | 782 |
def type_and_source_from_eid(self, eid, session=None): |
783 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
784 |
try: |
|
785 |
eid = typed_eid(eid) |
|
786 |
except ValueError: |
|
787 |
raise UnknownEid(eid) |
|
788 |
try: |
|
789 |
return self._type_source_cache[eid] |
|
790 |
except KeyError: |
|
791 |
if session is None: |
|
792 |
session = self.internal_session() |
|
793 |
reset_pool = True |
|
794 |
else: |
|
795 |
reset_pool = False |
|
796 |
try: |
|
797 |
etype, uri, extid = self.system_source.eid_type_source(session, |
|
798 |
eid) |
|
799 |
finally: |
|
800 |
if reset_pool: |
|
801 |
session.reset_pool() |
|
802 |
self._type_source_cache[eid] = (etype, uri, extid) |
|
803 |
if uri != 'system': |
|
804 |
self._extid_cache[(extid, uri)] = eid |
|
805 |
return etype, uri, extid |
|
806 |
||
807 |
def clear_caches(self, eids): |
|
808 |
etcache = self._type_source_cache |
|
809 |
extidcache = self._extid_cache |
|
810 |
rqlcache = self.querier._rql_cache |
|
811 |
for eid in eids: |
|
812 |
try: |
|
813 |
etype, uri, extid = etcache.pop(typed_eid(eid)) # may be a string in some cases |
|
814 |
rqlcache.pop('%s X WHERE X eid %s' % (etype, eid), None) |
|
815 |
extidcache.pop((extid, uri), None) |
|
816 |
except KeyError: |
|
817 |
etype = None |
|
818 |
rqlcache.pop('Any X WHERE X eid %s' % eid, None) |
|
819 |
for source in self.sources: |
|
820 |
source.clear_eid_cache(eid, etype) |
|
1482 | 821 |
|
0 | 822 |
def type_from_eid(self, eid, session=None): |
823 |
"""return the type of the entity with id <eid>""" |
|
824 |
return self.type_and_source_from_eid(eid, session)[0] |
|
1482 | 825 |
|
0 | 826 |
def source_from_eid(self, eid, session=None): |
827 |
"""return the source for the given entity's eid""" |
|
828 |
return self.sources_by_uri[self.type_and_source_from_eid(eid, session)[1]] |
|
1482 | 829 |
|
0 | 830 |
def eid2extid(self, source, eid, session=None): |
831 |
"""get local id from an eid""" |
|
832 |
etype, uri, extid = self.type_and_source_from_eid(eid, session) |
|
833 |
if source.uri != uri: |
|
834 |
# eid not from the given source |
|
835 |
raise UnknownEid(eid) |
|
836 |
return extid |
|
837 |
||
1954 | 838 |
def extid2eid(self, source, extid, etype, session=None, insert=True, |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
839 |
recreate=False): |
0 | 840 |
"""get eid from a local id. An eid is attributed if no record is found""" |
1954 | 841 |
cachekey = (extid, source.uri) |
0 | 842 |
try: |
843 |
return self._extid_cache[cachekey] |
|
844 |
except KeyError: |
|
845 |
pass |
|
846 |
reset_pool = False |
|
847 |
if session is None: |
|
848 |
session = self.internal_session() |
|
849 |
reset_pool = True |
|
1954 | 850 |
eid = self.system_source.extid2eid(session, source, extid) |
0 | 851 |
if eid is not None: |
852 |
self._extid_cache[cachekey] = eid |
|
1954 | 853 |
self._type_source_cache[eid] = (etype, source.uri, extid) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
854 |
if recreate: |
1954 | 855 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
856 |
entity._cw_recreating = True |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
857 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
858 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
859 |
# XXX add fti op ? |
1954 | 860 |
source.after_entity_insertion(session, extid, entity) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
861 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
862 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 863 |
if reset_pool: |
864 |
session.reset_pool() |
|
865 |
return eid |
|
866 |
if not insert: |
|
867 |
return |
|
1954 | 868 |
# no link between extid and eid, create one using an internal session |
0 | 869 |
# since the current session user may not have required permissions to |
870 |
# do necessary stuff and we don't want to commit user session. |
|
871 |
# |
|
450
5e14ea0e81c8
a note for later
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
341
diff
changeset
|
872 |
# Moreover, even if session is already an internal session but is |
0 | 873 |
# processing a commit, we have to use another one |
874 |
if not session.is_internal_session: |
|
875 |
session = self.internal_session() |
|
876 |
reset_pool = True |
|
877 |
try: |
|
878 |
eid = self.system_source.create_eid(session) |
|
879 |
self._extid_cache[cachekey] = eid |
|
1954 | 880 |
self._type_source_cache[eid] = (etype, source.uri, extid) |
881 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
3567
c43ce7949acf
add edited_attributes to entities from external sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3539
diff
changeset
|
882 |
if not hasattr(entity, 'edited_attributes'): |
c43ce7949acf
add edited_attributes to entities from external sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3539
diff
changeset
|
883 |
entity.edited_attributes = set() |
0 | 884 |
if source.should_call_hooks: |
2893
5989ce0707bc
set edited_attributes before calling hooks for entities from external sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2881
diff
changeset
|
885 |
entity.edited_attributes = set(entity) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
886 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
450
5e14ea0e81c8
a note for later
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
341
diff
changeset
|
887 |
# XXX call add_info with complete=False ? |
1954 | 888 |
self.add_info(session, entity, source, extid) |
889 |
source.after_entity_insertion(session, extid, entity) |
|
0 | 890 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
891 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 892 |
else: |
893 |
# minimal meta-data |
|
894 |
session.execute('SET X is E WHERE X eid %(x)s, E name %(name)s', |
|
3446
fc5055a09550
use __regid__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3425
diff
changeset
|
895 |
{'x': entity.eid, 'name': entity.__regid__}, 'x') |
0 | 896 |
session.commit(reset_pool) |
897 |
return eid |
|
898 |
except: |
|
899 |
session.rollback(reset_pool) |
|
900 |
raise |
|
1482 | 901 |
|
0 | 902 |
def add_info(self, session, entity, source, extid=None, complete=True): |
903 |
"""add type and source info for an eid into the system table, |
|
904 |
and index the entity with the full text index |
|
905 |
""" |
|
906 |
# begin by inserting eid/type/source/extid into the entities table |
|
5067
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
907 |
hook.set_operation(session, 'neweids', entity.eid, |
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
908 |
hook.CleanupNewEidsCacheOp) |
4806
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
909 |
self.system_source.add_info(session, entity, source, extid, complete) |
1482 | 910 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
911 |
def delete_info(self, session, entity, sourceuri, extid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
912 |
"""called by external source when some entity known by the system source |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
913 |
has been deleted in the external source |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
914 |
""" |
5067
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
915 |
# mark eid as being deleted in session info and setup cache update |
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
916 |
# operation |
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
917 |
hook.set_operation(session, 'pendingeids', entity.eid, |
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
918 |
hook.CleanupDeletedEidsCacheOp) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
919 |
self._delete_info(session, entity, sourceuri, extid) |
1482 | 920 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
921 |
def _delete_info(self, session, entity, sourceuri, extid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
922 |
# attributes=None, relations=None): |
0 | 923 |
"""delete system information on deletion of an entity: |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
924 |
* delete all remaining relations from/to this entity |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
925 |
* call delete info on the system source which will transfer record from |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
926 |
the entities table to the deleted_entities table |
0 | 927 |
""" |
2641
9c33d98a074e
R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2631
diff
changeset
|
928 |
pendingrtypes = session.transaction_data.get('pendingrtypes', ()) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
929 |
# delete remaining relations: if user can delete the entity, he can |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
930 |
# delete all its relations without security checking |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
931 |
with security_enabled(session, read=False, write=False): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
932 |
eid = entity.eid |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
933 |
for rschema, _, role in entity.e_schema.relation_definitions(): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
934 |
rtype = rschema.type |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
935 |
if rtype in schema.VIRTUAL_RTYPES or rtype in pendingrtypes: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
936 |
continue |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
937 |
if role == 'subject': |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
938 |
# don't skip inlined relation so they are regularly |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
939 |
# deleted and so hooks are correctly called |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
940 |
rql = 'DELETE X %s Y WHERE X eid %%(x)s' % rtype |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
941 |
else: |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
942 |
rql = 'DELETE Y %s X WHERE X eid %%(x)s' % rtype |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
943 |
session.execute(rql, {'x': eid}, 'x', build_descr=False) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
944 |
self.system_source.delete_info(session, entity, sourceuri, extid) |
0 | 945 |
|
946 |
def locate_relation_source(self, session, subject, rtype, object): |
|
947 |
subjsource = self.source_from_eid(subject, session) |
|
948 |
objsource = self.source_from_eid(object, session) |
|
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
949 |
if not subjsource is objsource: |
0 | 950 |
source = self.system_source |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
951 |
if not (subjsource.may_cross_relation(rtype) |
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
952 |
and objsource.may_cross_relation(rtype)): |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
953 |
raise MultiSourcesError( |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
954 |
"relation %s can't be crossed among sources" |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
955 |
% rtype) |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
956 |
elif not subjsource.support_relation(rtype): |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
957 |
source = self.system_source |
0 | 958 |
else: |
959 |
source = subjsource |
|
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
960 |
if not source.support_relation(rtype, True): |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
961 |
raise MultiSourcesError( |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
962 |
"source %s doesn't support write of %s relation" |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
963 |
% (source.uri, rtype)) |
0 | 964 |
return source |
1482 | 965 |
|
0 | 966 |
def locate_etype_source(self, etype): |
967 |
for source in self.sources: |
|
968 |
if source.support_entity(etype, 1): |
|
969 |
return source |
|
970 |
else: |
|
971 |
raise ETypeNotSupportedBySources(etype) |
|
1482 | 972 |
|
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
973 |
def init_entity_caches(self, session, entity, source): |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
974 |
"""add entity to session entities cache and repo's extid cache. |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
975 |
Return entity's ext id if the source isn't the system source. |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
976 |
""" |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
977 |
session.set_entity_cache(entity) |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
978 |
suri = source.uri |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
979 |
if suri == 'system': |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
980 |
extid = None |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
981 |
else: |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
982 |
extid = source.get_extid(entity) |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
983 |
self._extid_cache[(str(extid), suri)] = entity.eid |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
984 |
self._type_source_cache[entity.eid] = (entity.__regid__, suri, extid) |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
985 |
return extid |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
986 |
|
0 | 987 |
def glob_add_entity(self, session, entity): |
988 |
"""add an entity to the repository |
|
1482 | 989 |
|
0 | 990 |
the entity eid should originaly be None and a unique eid is assigned to |
991 |
the entity instance |
|
992 |
""" |
|
3584
585c4423a54c
set edited_attributes before call to pre_add_hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3567
diff
changeset
|
993 |
# init edited_attributes before calling before_add_entity hooks |
585c4423a54c
set edited_attributes before call to pre_add_hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3567
diff
changeset
|
994 |
entity._is_saved = False # entity has an eid but is not yet saved |
585c4423a54c
set edited_attributes before call to pre_add_hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3567
diff
changeset
|
995 |
entity.edited_attributes = set(entity) |
4973
1e6536afa855
fix bug with damned transmutation functionality, occuring when using an INSERT query which also add relation to the entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4946
diff
changeset
|
996 |
entity_ = entity.pre_add_hook() |
1e6536afa855
fix bug with damned transmutation functionality, occuring when using an INSERT query which also add relation to the entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4946
diff
changeset
|
997 |
# XXX kill that transmutation feature ! |
1e6536afa855
fix bug with damned transmutation functionality, occuring when using an INSERT query which also add relation to the entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4946
diff
changeset
|
998 |
if not entity_ is entity: |
1e6536afa855
fix bug with damned transmutation functionality, occuring when using an INSERT query which also add relation to the entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4946
diff
changeset
|
999 |
entity.__class__ = entity_.__class__ |
1e6536afa855
fix bug with damned transmutation functionality, occuring when using an INSERT query which also add relation to the entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4946
diff
changeset
|
1000 |
entity.__dict__.update(entity_.__dict__) |
0 | 1001 |
eschema = entity.e_schema |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1002 |
source = self.locate_etype_source(entity.__regid__) |
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1003 |
# allocate an eid to the entity before calling hooks |
0 | 1004 |
entity.set_eid(self.system_source.create_eid(session)) |
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1005 |
# set caches asap |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1006 |
extid = self.init_entity_caches(session, entity, source) |
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1007 |
if server.DEBUG & server.DBG_REPO: |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1008 |
print 'ADD entity', entity.__regid__, entity.eid, dict(entity) |
0 | 1009 |
relations = [] |
2929
51cdfe069e10
fix edited_attributes handling when adding entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
1010 |
if source.should_call_hooks: |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
1011 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
2929
51cdfe069e10
fix edited_attributes handling when adding entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
1012 |
# XXX use entity.keys here since edited_attributes is not updated for |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1013 |
# inline relations XXX not true, right? (see edited_attributes |
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1014 |
# affectation above) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1015 |
for attr in entity.iterkeys(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1016 |
rschema = eschema.subjrels[attr] |
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1017 |
if not rschema.final: # inlined relation |
0 | 1018 |
relations.append((attr, entity[attr])) |
1019 |
entity.set_defaults() |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1020 |
if session.is_hook_category_activated('integrity'): |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4808
diff
changeset
|
1021 |
entity.check(creation=True) |
0 | 1022 |
source.add_entity(session, entity) |
1023 |
self.add_info(session, entity, source, extid, complete=False) |
|
1024 |
entity._is_saved = True # entity has an eid and is saved |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1025 |
# prefill entity relation caches |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1026 |
for rschema in eschema.subject_relations(): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1027 |
rtype = str(rschema) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
1028 |
if rtype in schema.VIRTUAL_RTYPES: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1029 |
continue |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1030 |
if rschema.final: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1031 |
entity.setdefault(rtype, None) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1032 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1033 |
entity.set_related_cache(rtype, 'subject', session.empty_rset()) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1034 |
for rschema in eschema.object_relations(): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1035 |
rtype = str(rschema) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
1036 |
if rtype in schema.VIRTUAL_RTYPES: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1037 |
continue |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1038 |
entity.set_related_cache(rtype, 'object', session.empty_rset()) |
2756
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
1039 |
# set inline relation cache before call to after_add_entity |
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
1040 |
for attr, value in relations: |
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
1041 |
session.update_rel_cache_add(entity.eid, attr, value) |
0 | 1042 |
# trigger after_add_entity after after_add_relation |
1043 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1044 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 1045 |
# call hooks for inlined relations |
1046 |
for attr, value in relations: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1047 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1048 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1049 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1050 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1051 |
return entity.eid |
1482 | 1052 |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1053 |
def glob_update_entity(self, session, entity, edited_attributes): |
0 | 1054 |
"""replace an entity in the repository |
1055 |
the type and the eid of an entity must not be changed |
|
1056 |
""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1057 |
if server.DEBUG & server.DBG_REPO: |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1058 |
print 'UPDATE entity', entity.__regid__, entity.eid, \ |
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1059 |
dict(entity), edited_attributes |
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1060 |
entity.edited_attributes = edited_attributes |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1061 |
if session.is_hook_category_activated('integrity'): |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4808
diff
changeset
|
1062 |
entity.check() |
0 | 1063 |
eschema = entity.e_schema |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1064 |
session.set_entity_cache(entity) |
0 | 1065 |
only_inline_rels, need_fti_update = True, False |
1066 |
relations = [] |
|
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1067 |
for attr in edited_attributes: |
0 | 1068 |
if attr == 'eid': |
1069 |
continue |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1070 |
rschema = eschema.subjrels[attr] |
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1071 |
if rschema.final: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3757
diff
changeset
|
1072 |
if getattr(eschema.rdef(attr), 'fulltextindexed', False): |
0 | 1073 |
need_fti_update = True |
1074 |
only_inline_rels = False |
|
1075 |
else: |
|
1076 |
# inlined relation |
|
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1077 |
previous_value = entity.related(attr) or None |
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1078 |
if previous_value is not None: |
0 | 1079 |
previous_value = previous_value[0][0] # got a result set |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1080 |
if previous_value == entity[attr]: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1081 |
previous_value = None |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1082 |
else: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1083 |
self.hm.call_hooks('before_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1084 |
eidfrom=entity.eid, rtype=attr, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1085 |
eidto=previous_value) |
0 | 1086 |
relations.append((attr, entity[attr], previous_value)) |
1087 |
source = self.source_from_eid(entity.eid, session) |
|
1088 |
if source.should_call_hooks: |
|
1089 |
# call hooks for inlined relations |
|
1090 |
for attr, value, _ in relations: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1091 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1092 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1093 |
if not only_inline_rels: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1094 |
self.hm.call_hooks('before_update_entity', session, entity=entity) |
0 | 1095 |
source.update_entity(session, entity) |
4806
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
1096 |
self.system_source.update_info(session, entity, need_fti_update) |
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
1097 |
if source.should_call_hooks: |
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
1098 |
if not only_inline_rels: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1099 |
self.hm.call_hooks('after_update_entity', session, entity=entity) |
0 | 1100 |
for attr, value, prevvalue in relations: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1101 |
# if the relation is already cached, update existant cache |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1102 |
relcache = entity.relation_cached(attr, 'subject') |
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1103 |
if prevvalue is not None: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1104 |
self.hm.call_hooks('after_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1105 |
eidfrom=entity.eid, rtype=attr, eidto=prevvalue) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1106 |
if relcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1107 |
session.update_rel_cache_del(entity.eid, attr, prevvalue) |
0 | 1108 |
del_existing_rel_if_needed(session, entity.eid, attr, value) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1109 |
if relcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1110 |
session.update_rel_cache_add(entity.eid, attr, value) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1111 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1112 |
entity.set_related_cache(attr, 'subject', |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1113 |
session.eid_rset(value)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1114 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1115 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1116 |
|
1117 |
def glob_delete_entity(self, session, eid): |
|
1118 |
"""delete an entity and all related entities from the repository""" |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1119 |
entity = session.entity_from_eid(eid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1120 |
etype, sourceuri, extid = self.type_and_source_from_eid(eid, session) |
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1121 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1122 |
print 'DELETE entity', etype, eid |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1123 |
source = self.sources_by_uri[sourceuri] |
0 | 1124 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1125 |
self.hm.call_hooks('before_delete_entity', session, entity=entity) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1126 |
self._delete_info(session, entity, sourceuri, extid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1127 |
source.delete_entity(session, entity) |
0 | 1128 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1129 |
self.hm.call_hooks('after_delete_entity', session, entity=entity) |
0 | 1130 |
# don't clear cache here this is done in a hook on commit |
1482 | 1131 |
|
0 | 1132 |
def glob_add_relation(self, session, subject, rtype, object): |
1133 |
"""add a relation to the repository""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1134 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1135 |
print 'ADD relation', subject, rtype, object |
0 | 1136 |
source = self.locate_relation_source(session, subject, rtype, object) |
1137 |
if source.should_call_hooks: |
|
1138 |
del_existing_rel_if_needed(session, subject, rtype, object) |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1139 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1140 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1141 |
source.add_relation(session, subject, rtype, object) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1142 |
rschema = self.schema.rschema(rtype) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1143 |
session.update_rel_cache_add(subject, rtype, object, rschema.symmetric) |
0 | 1144 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1145 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1146 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1147 |
|
1148 |
def glob_delete_relation(self, session, subject, rtype, object): |
|
1149 |
"""delete a relation from the repository""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1150 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1151 |
print 'DELETE relation', subject, rtype, object |
0 | 1152 |
source = self.locate_relation_source(session, subject, rtype, object) |
1153 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1154 |
self.hm.call_hooks('before_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1155 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1156 |
source.delete_relation(session, subject, rtype, object) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1157 |
rschema = self.schema.rschema(rtype) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1158 |
session.update_rel_cache_del(subject, rtype, object, rschema.symmetric) |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1159 |
if rschema.symmetric: |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1160 |
# on symmetric relation, we can't now in which sense it's |
0 | 1161 |
# stored so try to delete both |
1162 |
source.delete_relation(session, object, rtype, subject) |
|
1163 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1164 |
self.hm.call_hooks('after_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1165 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1166 |
|
1167 |
||
1168 |
# pyro handling ########################################################### |
|
1482 | 1169 |
|
0 | 1170 |
def pyro_register(self, host=''): |
1171 |
"""register the repository as a pyro object""" |
|
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3584
diff
changeset
|
1172 |
import tempfile |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3584
diff
changeset
|
1173 |
from logilab.common.pyro_ext import register_object, config |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3584
diff
changeset
|
1174 |
config.PYRO_STORAGE = tempfile.gettempdir() # XXX until lgc > 0.45.1 is out |
3539
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3250
diff
changeset
|
1175 |
appid = self.config['pyro-instance-id'] or self.config.appid |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1176 |
daemon = register_object(self, appid, self.config['pyro-ns-group'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1177 |
self.config['pyro-host'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1178 |
self.config['pyro-ns-host']) |
0 | 1179 |
msg = 'repository registered as a pyro object using group %s and id %s' |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1180 |
self.info(msg, self.config['pyro-ns-group'], appid) |
0 | 1181 |
self.pyro_registered = True |
1182 |
return daemon |
|
1482 | 1183 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1184 |
# multi-sources planner helpers ########################################### |
1482 | 1185 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1186 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1187 |
def rel_type_sources(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1188 |
return [source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1189 |
if source.support_relation(rtype) |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1190 |
or rtype in source.dont_cross_relations] |
1482 | 1191 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1192 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1193 |
def can_cross_relation(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1194 |
return [source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1195 |
if source.support_relation(rtype) |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1196 |
and rtype in source.cross_relations] |
1482 | 1197 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1198 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1199 |
def is_multi_sources_relation(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1200 |
return any(source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1201 |
if not source is self.system_source |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1202 |
and source.support_relation(rtype)) |
1482 | 1203 |
|
0 | 1204 |
|
1205 |
def pyro_unregister(config): |
|
1206 |
"""unregister the repository from the pyro name server""" |
|
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1207 |
from logilab.common.pyro_ext import ns_unregister |
3539
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3250
diff
changeset
|
1208 |
appid = config['pyro-instance-id'] or config.appid |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1209 |
ns_unregister(appid, config['pyro-ns-group'], config['pyro-ns-host']) |
0 | 1210 |
|
1211 |
||
1212 |
from logging import getLogger |
|
1213 |
from cubicweb import set_log_methods |
|
1214 |
set_log_methods(Repository, getLogger('cubicweb.repository')) |