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