author | Florent <florent@secondweb.fr> |
Mon, 18 May 2009 11:50:43 +0200 | |
branch | stable |
changeset 1836 | d3f0c6e868ef |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""Adapter for google appengine source. |
2 |
||
3 |
:organization: Logilab |
|
1132 | 4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
1132 | 9 |
from cubicweb import AuthenticationError, UnknownEid |
0 | 10 |
from cubicweb.server.sources import AbstractSource, ConnectionWrapper |
11 |
from cubicweb.server.pool import SingleOperation |
|
12 |
from cubicweb.server.utils import crypt_password |
|
13 |
from cubicweb.goa.dbinit import set_user_groups |
|
14 |
from cubicweb.goa.rqlinterpreter import RQLInterpreter |
|
15 |
||
1132 | 16 |
from google.appengine.api.datastore import Key, Entity, Put, Delete |
0 | 17 |
from google.appengine.api import datastore_errors, users |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
18 |
|
0 | 19 |
def _init_groups(guser, euser): |
20 |
# set default groups |
|
21 |
if guser is None: |
|
22 |
groups = ['guests'] |
|
23 |
else: |
|
24 |
groups = ['users'] |
|
25 |
if users.is_current_user_admin(): |
|
26 |
groups.append('managers') |
|
27 |
set_user_groups(euser, groups) |
|
28 |
||
29 |
def _clear_related_cache(session, gaesubject, rtype, gaeobject): |
|
30 |
subject, object = str(gaesubject.key()), str(gaeobject.key()) |
|
31 |
for eid, role in ((subject, 'subject'), (object, 'object')): |
|
32 |
# clear related cache if necessary |
|
33 |
try: |
|
34 |
entity = session.entity_cache(eid) |
|
35 |
except KeyError: |
|
36 |
pass |
|
37 |
else: |
|
38 |
entity.clear_related_cache(rtype, role) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
39 |
if gaesubject.kind() == 'CWUser': |
0 | 40 |
for asession in session.repo._sessions.itervalues(): |
41 |
if asession.user.eid == subject: |
|
42 |
asession.user.clear_related_cache(rtype, 'subject') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
43 |
if gaeobject.kind() == 'CWUser': |
0 | 44 |
for asession in session.repo._sessions.itervalues(): |
45 |
if asession.user.eid == object: |
|
46 |
asession.user.clear_related_cache(rtype, 'object') |
|
47 |
||
48 |
def _mark_modified(session, gaeentity): |
|
49 |
modified = session.query_data('modifiedentities', {}, setdefault=True) |
|
50 |
modified[str(gaeentity.key())] = gaeentity |
|
51 |
DatastorePutOp(session) |
|
52 |
||
53 |
def _rinfo(session, subject, rtype, object): |
|
54 |
gaesubj = session.datastore_get(subject) |
|
55 |
gaeobj = session.datastore_get(object) |
|
56 |
rschema = session.vreg.schema.rschema(rtype) |
|
57 |
cards = rschema.rproperty(gaesubj.kind(), gaeobj.kind(), 'cardinality') |
|
58 |
return gaesubj, gaeobj, cards |
|
59 |
||
60 |
def _radd(session, gaeentity, targetkey, relation, card): |
|
61 |
if card in '?1': |
|
62 |
gaeentity[relation] = targetkey |
|
63 |
else: |
|
64 |
try: |
|
65 |
related = gaeentity[relation] |
|
66 |
except KeyError: |
|
67 |
related = [] |
|
68 |
else: |
|
69 |
if related is None: |
|
70 |
related = [] |
|
71 |
related.append(targetkey) |
|
72 |
gaeentity[relation] = related |
|
73 |
_mark_modified(session, gaeentity) |
|
74 |
||
75 |
def _rdel(session, gaeentity, targetkey, relation, card): |
|
76 |
if card in '?1': |
|
77 |
gaeentity[relation] = None |
|
78 |
else: |
|
79 |
related = gaeentity[relation] |
|
80 |
if related is not None: |
|
81 |
related = [key for key in related if not key == targetkey] |
|
82 |
gaeentity[relation] = related or None |
|
83 |
_mark_modified(session, gaeentity) |
|
84 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
85 |
|
0 | 86 |
class DatastorePutOp(SingleOperation): |
87 |
"""delayed put of entities to have less datastore write api calls |
|
88 |
||
89 |
* save all modified entities at precommit (should be the first operation |
|
90 |
processed, hence the 0 returned by insert_index()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
91 |
|
0 | 92 |
* in case others precommit operations modify some entities, resave modified |
93 |
entities at commit. This suppose that no db changes will occurs during |
|
94 |
commit event but it should be the case. |
|
95 |
""" |
|
96 |
def insert_index(self): |
|
97 |
return 0 |
|
98 |
||
99 |
def _put_entities(self): |
|
100 |
pending = self.session.query_data('pendingeids', ()) |
|
101 |
modified = self.session.query_data('modifiedentities', {}) |
|
102 |
for eid, gaeentity in modified.iteritems(): |
|
103 |
assert not eid in pending |
|
104 |
Put(gaeentity) |
|
105 |
modified.clear() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
106 |
|
0 | 107 |
def commit_event(self): |
108 |
self._put_entities() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
109 |
|
0 | 110 |
def precommit_event(self): |
111 |
self._put_entities() |
|
112 |
||
113 |
||
114 |
class GAESource(AbstractSource): |
|
115 |
"""adapter for a system source on top of google appengine datastore""" |
|
116 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
117 |
passwd_rql = "Any P WHERE X is CWUser, X login %(login)s, X upassword P" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
118 |
auth_rql = "Any X WHERE X is CWUser, X login %(login)s, X upassword %(pwd)s" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
119 |
_sols = ({'X': 'CWUser', 'P': 'Password'},) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
120 |
|
0 | 121 |
options = () |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
122 |
|
0 | 123 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
124 |
AbstractSource.__init__(self, repo, appschema, source_config, |
|
125 |
*args, **kwargs) |
|
126 |
if repo.config['use-google-auth']: |
|
127 |
self.info('using google authentication service') |
|
128 |
self.authenticate = self.authenticate_gauth |
|
129 |
else: |
|
130 |
self.authenticate = self.authenticate_local |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
131 |
|
0 | 132 |
def reset_caches(self): |
133 |
"""method called during test to reset potential source caches""" |
|
134 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
135 |
|
0 | 136 |
def init_creating(self): |
137 |
pass |
|
138 |
||
139 |
def init(self): |
|
140 |
# XXX unregister unsupported hooks |
|
141 |
from cubicweb.server.hooks import sync_owner_after_add_composite_relation |
|
142 |
self.repo.hm.unregister_hook(sync_owner_after_add_composite_relation, |
|
143 |
'after_add_relation', '') |
|
144 |
||
145 |
def get_connection(self): |
|
146 |
return ConnectionWrapper() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
147 |
|
0 | 148 |
# ISource interface ####################################################### |
149 |
||
150 |
def compile_rql(self, rql): |
|
151 |
rqlst = self.repo.querier._rqlhelper.parse(rql) |
|
152 |
rqlst.restricted_vars = () |
|
153 |
rqlst.children[0].solutions = self._sols |
|
154 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
155 |
|
0 | 156 |
def set_schema(self, schema): |
157 |
"""set the application'schema""" |
|
158 |
self.interpreter = RQLInterpreter(schema) |
|
159 |
self.schema = schema |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
160 |
if 'CWUser' in schema and not self.repo.config['use-google-auth']: |
0 | 161 |
# rql syntax trees used to authenticate users |
162 |
self._passwd_rqlst = self.compile_rql(self.passwd_rql) |
|
163 |
self._auth_rqlst = self.compile_rql(self.auth_rql) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
164 |
|
0 | 165 |
def support_entity(self, etype, write=False): |
166 |
"""return true if the given entity's type is handled by this adapter |
|
167 |
if write is true, return true only if it's a RW support |
|
168 |
""" |
|
169 |
return True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
170 |
|
0 | 171 |
def support_relation(self, rtype, write=False): |
172 |
"""return true if the given relation's type is handled by this adapter |
|
173 |
if write is true, return true only if it's a RW support |
|
174 |
""" |
|
175 |
return True |
|
176 |
||
177 |
def authenticate_gauth(self, session, login, password): |
|
178 |
guser = users.get_current_user() |
|
179 |
# allowing or not anonymous connection should be done in the app.yaml |
|
180 |
# file, suppose it's authorized if we are there |
|
181 |
if guser is None: |
|
182 |
login = u'anonymous' |
|
183 |
else: |
|
184 |
login = unicode(guser.nickname()) |
|
185 |
# XXX http://code.google.com/appengine/docs/users/userobjects.html |
|
186 |
# use a reference property to automatically work with email address |
|
187 |
# changes after the propagation feature is implemented |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
188 |
key = Key.from_path('CWUser', 'key_' + login, parent=None) |
0 | 189 |
try: |
190 |
euser = session.datastore_get(key) |
|
191 |
# XXX fix user. Required until we find a better way to fix broken records |
|
192 |
if not euser.get('s_in_group'): |
|
193 |
_init_groups(guser, euser) |
|
194 |
Put(euser) |
|
195 |
return str(key) |
|
196 |
except datastore_errors.EntityNotFoundError: |
|
197 |
# create a record for this user |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
198 |
euser = Entity('CWUser', name='key_' + login) |
0 | 199 |
euser['s_login'] = login |
200 |
_init_groups(guser, euser) |
|
201 |
Put(euser) |
|
202 |
return str(euser.key()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
203 |
|
0 | 204 |
def authenticate_local(self, session, login, password): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
205 |
"""return CWUser eid for the given login/password if this account is |
0 | 206 |
defined in this source, else raise `AuthenticationError` |
207 |
||
208 |
two queries are needed since passwords are stored crypted, so we have |
|
209 |
to fetch the salt first |
|
210 |
""" |
|
211 |
args = {'login': login, 'pwd' : password} |
|
212 |
if password is not None: |
|
213 |
rset = self.syntax_tree_search(session, self._passwd_rqlst, args) |
|
214 |
try: |
|
215 |
pwd = rset[0][0] |
|
216 |
except IndexError: |
|
217 |
raise AuthenticationError('bad login') |
|
218 |
# passwords are stored using the bytea type, so we get a StringIO |
|
219 |
if pwd is not None: |
|
220 |
args['pwd'] = crypt_password(password, pwd[:2]) |
|
221 |
# get eid from login and (crypted) password |
|
222 |
rset = self.syntax_tree_search(session, self._auth_rqlst, args) |
|
223 |
try: |
|
224 |
return rset[0][0] |
|
225 |
except IndexError: |
|
226 |
raise AuthenticationError('bad password') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
227 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
228 |
def syntax_tree_search(self, session, union, args=None, cachekey=None, |
0 | 229 |
varmap=None): |
230 |
"""return result from this source for a rql query (actually from a rql |
|
231 |
syntax tree and a solution dictionary mapping each used variable to a |
|
232 |
possible type). If cachekey is given, the query necessary to fetch the |
|
233 |
results (but not the results themselves) may be cached using this key. |
|
234 |
""" |
|
235 |
results, description = self.interpreter.interpret(union, args, |
|
236 |
session.datastore_get) |
|
237 |
return results # XXX description |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
238 |
|
0 | 239 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
240 |
raise NotImplementedError |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
241 |
|
0 | 242 |
def add_entity(self, session, entity): |
243 |
"""add a new entity to the source""" |
|
244 |
# do not delay add_entity as other modifications, new created entity |
|
245 |
# needs an eid |
|
246 |
entity.put() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
247 |
|
0 | 248 |
def update_entity(self, session, entity): |
249 |
"""replace an entity in the source""" |
|
250 |
gaeentity = entity.to_gae_model() |
|
251 |
_mark_modified(session, entity.to_gae_model()) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
252 |
if gaeentity.kind() == 'CWUser': |
0 | 253 |
for asession in self.repo._sessions.itervalues(): |
254 |
if asession.user.eid == entity.eid: |
|
255 |
asession.user.update(dict(gaeentity)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
256 |
|
0 | 257 |
def delete_entity(self, session, etype, eid): |
258 |
"""delete an entity from the source""" |
|
259 |
# do not delay delete_entity as other modifications to ensure |
|
260 |
# consistency |
|
261 |
key = Key(eid) |
|
262 |
Delete(key) |
|
263 |
session.clear_datastore_cache(key) |
|
264 |
session.drop_entity_cache(eid) |
|
265 |
session.query_data('modifiedentities', {}).pop(eid, None) |
|
266 |
||
267 |
def add_relation(self, session, subject, rtype, object): |
|
268 |
"""add a relation to the source""" |
|
269 |
gaesubj, gaeobj, cards = _rinfo(session, subject, rtype, object) |
|
270 |
_radd(session, gaesubj, gaeobj.key(), 's_' + rtype, cards[0]) |
|
271 |
_radd(session, gaeobj, gaesubj.key(), 'o_' + rtype, cards[1]) |
|
272 |
_clear_related_cache(session, gaesubj, rtype, gaeobj) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
273 |
|
0 | 274 |
def delete_relation(self, session, subject, rtype, object): |
275 |
"""delete a relation from the source""" |
|
276 |
gaesubj, gaeobj, cards = _rinfo(session, subject, rtype, object) |
|
277 |
pending = session.query_data('pendingeids', set(), setdefault=True) |
|
278 |
if not subject in pending: |
|
279 |
_rdel(session, gaesubj, gaeobj.key(), 's_' + rtype, cards[0]) |
|
280 |
if not object in pending: |
|
281 |
_rdel(session, gaeobj, gaesubj.key(), 'o_' + rtype, cards[1]) |
|
282 |
_clear_related_cache(session, gaesubj, rtype, gaeobj) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
283 |
|
0 | 284 |
# system source interface ################################################# |
285 |
||
286 |
def eid_type_source(self, session, eid): |
|
287 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
288 |
try: |
|
289 |
key = Key(eid) |
|
290 |
except datastore_errors.BadKeyError: |
|
291 |
raise UnknownEid(eid) |
|
292 |
return key.kind(), 'system', None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
293 |
|
0 | 294 |
def create_eid(self, session): |
295 |
return None # let the datastore generating key |
|
296 |
||
297 |
def add_info(self, session, entity, source, extid=None): |
|
298 |
"""add type and source info for an eid into the system table""" |
|
299 |
pass |
|
300 |
||
301 |
def delete_info(self, session, eid, etype, uri, extid): |
|
302 |
"""delete system information on deletion of an entity by transfering |
|
303 |
record from the entities table to the deleted_entities table |
|
304 |
""" |
|
305 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
306 |
|
0 | 307 |
def fti_unindex_entity(self, session, eid): |
308 |
"""remove text content for entity with the given eid from the full text |
|
309 |
index |
|
310 |
""" |
|
311 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
312 |
|
0 | 313 |
def fti_index_entity(self, session, entity): |
314 |
"""add text content of a created/modified entity to the full text index |
|
315 |
""" |
|
316 |
pass |