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