234 |
234 |
235 """ |
235 """ |
236 is_request = False |
236 is_request = False |
237 hooks_in_progress = False |
237 hooks_in_progress = False |
238 |
238 |
239 def __init__(self, session): |
239 def __init__(self, repo, user): |
240 super(Connection, self).__init__(session.repo.vreg) |
240 super(Connection, self).__init__(repo.vreg) |
241 #: connection unique id |
241 #: connection unique id |
242 self._open = None |
242 self._open = None |
243 self.session = session |
|
244 |
243 |
245 #: server.Repository object |
244 #: server.Repository object |
246 self.repo = session.repo |
245 self.repo = repo |
247 self.vreg = self.repo.vreg |
246 self.vreg = self.repo.vreg |
248 self._execute = self.repo.querier.execute |
247 self._execute = self.repo.querier.execute |
249 |
248 |
250 # internal (root) session |
249 # internal (root) session |
251 self.is_internal_session = isinstance(session.user, InternalManager) |
250 self.is_internal_session = isinstance(user, InternalManager) |
252 |
251 |
253 #: dict containing arbitrary data cleared at the end of the transaction |
252 #: dict containing arbitrary data cleared at the end of the transaction |
254 self.transaction_data = {} |
253 self.transaction_data = {} |
255 self._session_data = session.data |
|
256 #: ordered list of operations to be processed on commit/rollback |
254 #: ordered list of operations to be processed on commit/rollback |
257 self.pending_operations = [] |
255 self.pending_operations = [] |
258 #: (None, 'precommit', 'postcommit', 'uncommitable') |
256 #: (None, 'precommit', 'postcommit', 'uncommitable') |
259 self.commit_state = None |
257 self.commit_state = None |
260 |
258 |
271 # security control attributes |
269 # security control attributes |
272 self._read_security = DEFAULT_SECURITY # handled by a property |
270 self._read_security = DEFAULT_SECURITY # handled by a property |
273 self.write_security = DEFAULT_SECURITY |
271 self.write_security = DEFAULT_SECURITY |
274 |
272 |
275 # undo control |
273 # undo control |
276 config = session.repo.config |
274 config = repo.config |
277 if config.creating or config.repairing or self.is_internal_session: |
275 if config.creating or config.repairing or self.is_internal_session: |
278 self.undo_actions = False |
276 self.undo_actions = False |
279 else: |
277 else: |
280 self.undo_actions = config['undo-enabled'] |
278 self.undo_actions = config['undo-enabled'] |
281 |
279 |
282 # RQLRewriter are not thread safe |
280 # RQLRewriter are not thread safe |
283 self._rewriter = RQLRewriter(self) |
281 self._rewriter = RQLRewriter(self) |
284 |
282 |
285 # other session utility |
283 # other session utility |
286 if session.user.login == '__internal_manager__': |
284 if user.login == '__internal_manager__': |
287 self.user = session.user |
285 self.user = user |
288 else: |
286 else: |
289 self._set_user(session.user) |
287 self._set_user(user) |
290 |
288 |
291 @_open_only |
289 @_open_only |
292 def get_schema(self): |
290 def get_schema(self): |
293 """Return the schema currently used by the repository.""" |
291 """Return the schema currently used by the repository.""" |
294 return self.repo.source_defs() |
292 return self.repo.source_defs() |
390 self.hooks_in_progress = prevmode |
388 self.hooks_in_progress = prevmode |
391 |
389 |
392 # shared data handling ################################################### |
390 # shared data handling ################################################### |
393 |
391 |
394 @property |
392 @property |
|
393 @deprecated('[3.25] use transaction_data or req.session.data', stacklevel=3) |
395 def data(self): |
394 def data(self): |
396 return self._session_data |
395 return self.transaction_data |
397 |
396 |
398 @property |
397 @property |
399 def rql_rewriter(self): |
398 def rql_rewriter(self): |
400 return self._rewriter |
399 return self._rewriter |
401 |
400 |
404 def get_shared_data(self, key, default=None, pop=False, txdata=False): |
403 def get_shared_data(self, key, default=None, pop=False, txdata=False): |
405 """return value associated to `key` in session data""" |
404 """return value associated to `key` in session data""" |
406 if txdata: |
405 if txdata: |
407 data = self.transaction_data |
406 data = self.transaction_data |
408 else: |
407 else: |
409 data = self._session_data |
408 data = self.data |
410 if pop: |
409 if pop: |
411 return data.pop(key, default) |
410 return data.pop(key, default) |
412 else: |
411 else: |
413 return data.get(key, default) |
412 return data.get(key, default) |
414 |
413 |
417 def set_shared_data(self, key, value, txdata=False): |
416 def set_shared_data(self, key, value, txdata=False): |
418 """set value associated to `key` in session data""" |
417 """set value associated to `key` in session data""" |
419 if txdata: |
418 if txdata: |
420 self.transaction_data[key] = value |
419 self.transaction_data[key] = value |
421 else: |
420 else: |
422 self._session_data[key] = value |
421 self.data[key] = value |
423 |
422 |
424 def clear(self): |
423 def clear(self): |
425 """reset internal data""" |
424 """reset internal data""" |
426 self.transaction_data = {} |
425 self.transaction_data = {} |
427 #: ordered list of operations to be processed on commit/rollback |
426 #: ordered list of operations to be processed on commit/rollback |
447 @_open_only |
446 @_open_only |
448 @deprecated('[3.21] a cnxset is automatically set on __enter__ call now.' |
447 @deprecated('[3.21] a cnxset is automatically set on __enter__ call now.' |
449 ' stop using .ensure_cnx_set') |
448 ' stop using .ensure_cnx_set') |
450 def ensure_cnx_set(self): |
449 def ensure_cnx_set(self): |
451 yield |
450 yield |
452 |
|
453 @property |
|
454 def anonymous_connection(self): |
|
455 return self.session.anonymous_session |
|
456 |
451 |
457 # Entity cache management ################################################# |
452 # Entity cache management ################################################# |
458 # |
453 # |
459 # The connection entity cache as held in cnx.transaction_data is removed at the |
454 # The connection entity cache as held in cnx.transaction_data is removed at the |
460 # end of the connection (commit and rollback) |
455 # end of the connection (commit and rollback) |