Commit 80c3fe50 authored by Pierre-Yves David's avatar Pierre-Yves David
Browse files

[session/transaction] move most undo support into transaction

A small part that need repo access has not been moved into transaction yet.
parent 1b7b4709c0cf
......@@ -368,6 +368,13 @@ class Transaction(object):
self._read_security = DEFAULT_SECURITY # handled by a property
self.write_security = DEFAULT_SECURITY
# undo control
config = session.repo.config
if config.creating or config.repairing or session.is_internal_session:
self.undo_actions = False
else:
self.undo_actions = config['undo-enabled']
# RQLRewriter are not thread safe
self._rewriter = rewriter
......@@ -544,6 +551,22 @@ class Transaction(object):
self.running_dbapi_query = (oldmode is DEFAULT_SECURITY
or activated is DEFAULT_SECURITY)
# undo support ############################################################
def ertype_supports_undo(self, ertype):
return self.undo_actions and ertype not in NO_UNDO_TYPES
def transaction_uuid(self, set=True):
uuid = self.data.get('tx_uuid')
if set and uuid is None:
raise KeyError
return uuid
def transaction_inc_action_counter(self):
num = self.data.setdefault('tx_action_count', 0) + 1
self.data['tx_action_count'] = num
return num
def tx_attr(attr_name, writable=False):
"""return a property to forward attribute access to transaction.
......@@ -692,11 +715,6 @@ class Session(RequestSessionBase):
self.repo = repo
self.timestamp = time()
self.default_mode = 'read'
# undo support
if repo.config.creating or repo.config.repairing or self.is_internal_session:
self.undo_actions = False
else:
self.undo_actions = repo.config['undo-enabled']
# short cut to querier .execute method
self._execute = repo.querier.execute
# shared data, used to communicate extra information between the client
......@@ -1350,24 +1368,17 @@ class Session(RequestSessionBase):
# undo support ############################################################
def ertype_supports_undo(self, ertype):
return self.undo_actions and ertype not in NO_UNDO_TYPES
ertype_supports_undo = tx_meth('ertype_supports_undo')
transaction_inc_action_counter = tx_meth('transaction_inc_action_counter')
def transaction_uuid(self, set=True):
try:
return self._tx.data['tx_uuid']
return self._tx.transaction_uuid(set=set)
except KeyError:
if not set:
return
self._tx.data['tx_uuid'] = uuid = uuid4().hex
self.repo.system_source.start_undoable_transaction(self, uuid)
return uuid
def transaction_inc_action_counter(self):
num = self._tx.data.setdefault('tx_action_count', 0) + 1
self._tx.data['tx_action_count'] = num
return num
# querier helpers #########################################################
rql_rewriter = tx_attr('_rewriter')
......
......@@ -19,6 +19,8 @@
from cubicweb import ValidationError
from cubicweb.devtools.testlib import CubicWebTC
import cubicweb.server.session
from cubicweb.server.session import Transaction as OldTransaction
from cubicweb.transaction import *
from cubicweb.server.sources.native import UndoTransactionException, _UndoException
......@@ -28,12 +30,19 @@ class UndoableTransactionTC(CubicWebTC):
def setup_database(self):
req = self.request()
self.session.undo_actions = True
self.toto = self.create_user(req, 'toto', password='toto', groups=('users',),
commit=False)
self.txuuid = self.commit()
def setUp(self):
class Transaction(OldTransaction):
"""Force undo feature to be turned on in all case"""
undo_actions = property(lambda tx: True, lambda x, y:None)
cubicweb.server.session.Transaction = Transaction
super(UndoableTransactionTC, self).setUp()
def tearDown(self):
cubicweb.server.session.Transaction = OldTransaction
self.restore_connection()
self.session.undo_support = set()
super(UndoableTransactionTC, self).tearDown()
......
......@@ -32,6 +32,8 @@ from cubicweb.devtools.testlib import CubicWebTC
from cubicweb.utils import json_dumps
from cubicweb.uilib import rql_for_eid
from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, RemoteCallFailed
import cubicweb.server.session
from cubicweb.server.session import Transaction as OldTransaction
from cubicweb.entities.authobjs import CWUser
from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
......@@ -781,9 +783,20 @@ class JSonControllerTC(AjaxControllerTC):
class UndoControllerTC(CubicWebTC):
def setUp(self):
class Transaction(OldTransaction):
"""Force undo feature to be turned on in all case"""
undo_actions = property(lambda tx: True, lambda x, y:None)
cubicweb.server.session.Transaction = Transaction
super(UndoControllerTC, self).setUp()
def tearDown(self):
super(UndoControllerTC, self).tearDown()
cubicweb.server.session.Transaction = OldTransaction
def setup_database(self):
req = self.request()
self.session.undo_actions = True
self.toto = self.create_user(req, 'toto', password='toto', groups=('users',),
commit=False)
self.txuuid_toto = self.commit()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment