[session/transaction] move most undo support into transaction
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Wed, 27 Mar 2013 17:09:55 +0100
changeset 8812 52af67a2f0a5
parent 8811 1b7b4709c0cf
child 8813 cf94a7f980fe
[session/transaction] move most undo support into transaction A small part that need repo access has not been moved into transaction yet.
server/session.py
server/test/unittest_undo.py
web/test/unittest_views_basecontrollers.py
--- a/server/session.py	Wed Mar 27 11:42:13 2013 +0100
+++ b/server/session.py	Wed Mar 27 17:09:55 2013 +0100
@@ -368,6 +368,13 @@
         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 @@
         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 @@
         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 @@
 
     # 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')
--- a/server/test/unittest_undo.py	Wed Mar 27 11:42:13 2013 +0100
+++ b/server/test/unittest_undo.py	Wed Mar 27 17:09:55 2013 +0100
@@ -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 @@
 
     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()
--- a/web/test/unittest_views_basecontrollers.py	Wed Mar 27 11:42:13 2013 +0100
+++ b/web/test/unittest_views_basecontrollers.py	Wed Mar 27 17:09:55 2013 +0100
@@ -32,6 +32,8 @@
 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 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()