Changeset 259
- Timestamp:
- 12/01/05 13:33:18 (3 years ago)
- Files:
-
- trunk/turbogears/controllers.py (modified) (3 diffs)
- trunk/turbogears/database.py (modified) (8 diffs)
- trunk/turbogears/tests/test_controllers.py (modified) (4 diffs)
- trunk/turbogears/tests/test_database.py (added)
- trunk/turbogears/tests/test_i18n.py (modified) (1 diff)
- trunk/turbogears/validators.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/turbogears/controllers.py
r199 r259 12 12 from turbogears.util import setlike 13 13 import turbogears 14 from turbogears import database 14 15 from turbogears.widgets import js_location 15 16 … … 192 193 except turbogearsvalid.Invalid, error: 193 194 errors.update(error.error_dict) 194 if errors: 195 if errorhandler: 196 output = _call_with_errors(func, self, *args, **kw) 197 elif hasattr(self, "validation_error"): 198 output = self.validation_error(func.func_name, kw, errors) 199 else: 200 raise turbogearsvalid.Invalid(str(errors), kw, None) 195 if not hasattr(cherrypy.request, "in_transaction"): 196 cherrypy.request.in_transaction = True 197 output = database.run_with_transaction(_execute_func, self, 198 errors, errorhandler, func, *args, **kw) 201 199 else: 202 if errorhandler: 203 output = func(self, has_errors=False, *args, **kw) 204 else: 205 output = func(self, *args, **kw) 200 output = _execute_func(self, errors, errorhandler, func, 201 *args, **kw) 206 202 return controllers._process_output(tg_format, output, html) 207 203 … … 214 210 215 211 return decorator 212 213 def _execute_func(self, errors, errorhandler, func, *args, **kw): 214 if errors: 215 if errorhandler: 216 output = _call_with_errors(func, self, *args, **kw) 217 elif hasattr(self, "validation_error"): 218 output = self.validation_error(func.func_name, kw, errors) 219 else: 220 raise turbogearsvalid.Invalid(str(errors), kw, None) 221 else: 222 if errorhandler: 223 output = func(self, has_errors=False, *args, **kw) 224 else: 225 output = func(self, *args, **kw) 226 return output 227 216 228 217 229 def flash(message): trunk/turbogears/database.py
r68 r259 4 4 from sqlobject.dbconnection import ConnectionHub, Transaction, TheURIOpener 5 5 import cherrypy 6 from sets import Set 7 8 hub_registry = Set() 6 9 7 10 class AutoConnectHub(ConnectionHub): … … 15 18 uri = cherrypy.config.get("sqlobject.dburi") 16 19 self.uri = uri 20 hub_registry.add(self) 17 21 ConnectionHub.__init__(self) 18 22 … … 20 24 try: 21 25 conn = self.threadingLocal.connection 22 return conn26 return self.begin(conn) 23 27 except AttributeError: 24 28 if self.uri: … … 30 34 TheURIOpener.cachedURIs = {} 31 35 self.threadingLocal.connection = conn 32 return conn36 return self.begin(conn) 33 37 try: 34 38 return self.processConnection … … 43 47 self.threadingLocal = threading_local() 44 48 45 def begin(self ):49 def begin(self, conn=None): 46 50 "Starts a transaction." 47 conn = self.getConnection() 51 if not conn: 52 conn = self.getConnection() 48 53 if isinstance(conn, Transaction): 49 54 if conn._obsolete: 50 55 conn.begin() 51 return 56 return conn 52 57 self.threadingLocal.old_conn = conn 53 self.threadingLocal.connection = conn.transaction() 58 trans = conn.transaction() 59 self.threadingLocal.connection = trans 60 return trans 54 61 55 62 def commit(self): 56 63 "Commits the current transaction." 57 conn = self.threadingLocal.connection 64 try: 65 conn = self.threadingLocal.connection 66 except AttributeError: 67 return 58 68 if isinstance(conn, Transaction): 59 69 self.threadingLocal.connection.commit() … … 61 71 def rollback(self): 62 72 "Rolls back the current transaction." 63 conn = self.threadingLocal.connection 73 try: 74 conn = self.threadingLocal.connection 75 except AttributeError: 76 return 64 77 if isinstance(conn, Transaction) and not conn._obsolete: 65 78 self.threadingLocal.connection.rollback() … … 67 80 def end(self): 68 81 "Ends the transaction, returning to a standard connection." 69 conn = self.threadingLocal.connection 82 try: 83 conn = self.threadingLocal.connection 84 except AttributeError: 85 return 70 86 if not isinstance(conn, Transaction): 71 87 return … … 137 153 {"sqlobject.dburi" : dburi} 138 154 }) 139 140 __all__ = ["PackageHub", "AutoConnectHub", "set_db_uri"] 155 156 def commit_all(): 157 "Commits the Transactions in all registered hubs (for this thread)" 158 for hub in hub_registry: 159 hub.commit() 160 161 def rollback_all(): 162 "Rolls back the Transactions in all registered hubs (for this thread)" 163 for hub in hub_registry: 164 hub.rollback() 165 166 def end_all(): 167 "Ends the Transactions in all registered hubs (for this thread)" 168 for hub in hub_registry: 169 hub.end() 170 171 def run_with_transaction(func, *args, **kw): 172 try: 173 try: 174 retval = func(*args, **kw) 175 commit_all() 176 return retval 177 except cherrypy.HTTPRedirect: 178 commit_all() 179 raise 180 except cherrypy.InternalRedirect: 181 commit_all() 182 raise 183 except: 184 rollback_all() 185 raise 186 finally: 187 end_all() 188 189 __all__ = ["PackageHub", "AutoConnectHub", "set_db_uri", 190 "commit_all", "rollback_all", "end_all"] trunk/turbogears/tests/test_controllers.py
r189 r259 4 4 from turbogears import url 5 5 from turbogears import validators 6 from turbogears import database 6 7 import cherrypy 7 8 from turbogears.tests import util … … 40 41 istrue = turbogears.expose( 41 42 validators={'value': validators.StringBoolean()})(istrue) 43 44 def callsanother(self): 45 return self.istrue(True) 46 callsanother = turbogears.expose()(callsanother) 42 47 43 48 def returnjson(self): … … 86 91 return dict(tg_template="turbogears.tests.othertemplate") 87 92 useother = turbogears.expose(html="turbogears.tests.simple")(useother) 93 94 rwt_called = 0 95 def rwt(self, func, *args, **kw): 96 self.rwt_called += 1 97 func(*args, **kw) 88 98 89 99 class TestRoot(unittest.TestCase): … … 200 210 util.createRequest("/useother") 201 211 assert "This is the other template" in cherrypy.response.body[0] 212 213 def test_runwithtrans(self): 214 "run_with_transaction is called only on topmost exposed method" 215 oldrwt = database.run_with_transaction 216 database.run_with_transaction = cherrypy.root.rwt 217 util.createRequest("/callsanother") 218 database.run_with_transaction = oldrwt 219 assert cherrypy.root.value 220 assert cherrypy.root.rwt_called == 1 202 221 203 222 class TestURLs(unittest.TestCase): trunk/turbogears/tests/test_i18n.py
r247 r259 10 10 dt = datetime(2005, 2, 5) # Saturday, 5rd Feb 2005 11 11 def get_test_locale(): 12 12 import cherrypy 13 13 return cherrypy.config.get('i18n.defaultLocale', 'en') 14 14 trunk/turbogears/validators.py
r92 r259 6 6 from formencode.validators import * 7 7 from formencode.compound import * 8 from formencode.api import Invalid