def __call__(self, environ, start_response): environ[ekey] = True transaction.begin() ctx = {} def save_status_and_headers(status, headers, exc_info=None): ctx.update(status=status, headers=headers) return start_response(status, headers, exc_info) try: result = self.application(environ, save_status_and_headers) except: self.abort() raise else: # ZODB 3.8 + has isDoomed if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): self.abort() if self.commit_veto is not None: try: if self.commit_veto(environ, ctx['status'], ctx['headers']): self.abort() except: self.abort() raise else: self.commit() else: self.commit() return result
def test_aborted_via_doom(self): resource = DummyResource() app = DummyApplication(resource, doom=True) tm = self._makeOne(app) result = tm({}, self._start_response) self.assertEqual(result, ['hello']) self.assertEqual(transaction.isDoomed(), False) self.assertEqual(resource.committed, False) self.assertEqual(resource.aborted, True)
def close_db(self, exception): """Added as a `~flask.Flask.teardown_request` to applications to commit the transaction and disconnect ZODB if it was used during the request.""" if self.is_connected: if exception is None and not transaction.isDoomed(): transaction.commit() else: transaction.abort() self.connection.close()
def read_only_doomed_transaction(session, sqla_txn, connection): ''' Doomed transactions can be read-only. ``transaction.doom()`` must be called before the connection is used. ''' if not transaction.isDoomed(): return if connection.engine.url.drivername != 'postgresql': return connection.execute("SET TRANSACTION READ ONLY;")
def __call__(self, environ, start_response): transaction = self.transaction environ[ekey] = True transaction.begin() ctx = {} def save_status_and_headers(status, headers, exc_info=None): ctx.update(status=status, headers=headers) return start_response(status, headers, exc_info) iterable = None try: iterable = self.application(environ, save_status_and_headers) for chunk in iterable: yield chunk except Exception: """Saving the exception""" try: type_, value, tb = sys.exc_info() self.abort() reraise(type_, value, tb) finally: del type_, value, tb finally: if hasattr(iterable, 'close'): try: iterable.close() except Exception: """Saving the exception""" try: type_, value, tb = sys.exc_info() self.abort() reraise(type_, value, tb) finally: del type_, value, tb # ZODB 3.8 + has isDoomed if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): self.abort() else: if self.commit_veto is not None: try: status, headers = ctx['status'], ctx['headers'] veto = self.commit_veto(environ, status, headers) except: self.abort() raise if veto: self.abort() else: self.commit() else: self.commit()
def test_no_permission(self): import simplejson import transaction testing.registerDummySecurityPolicy("chris", permissive=False) context = self._make_context() request = testing.DummyRequest(params={"file": DummyUpload(), "title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"You do not have permission to upload files here.") self.failUnless(transaction.isDoomed())
def test_no_upload(self): import simplejson import transaction testing.registerDummySecurityPolicy("chris") context = self._make_context() request = testing.DummyRequest(params={"file": testing.DummyModel(), "title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"You must select a file before clicking Upload.") self.failUnless(transaction.isDoomed())
def test_no_filename(self): import simplejson import transaction testing.registerDummySecurityPolicy("chris") context = self._make_context() request = testing.DummyRequest(params={"file": DummyUpload(filename=""), "title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"The filename must not be empty") self.failUnless(transaction.isDoomed())
def test_wrong_imagedata(self): import simplejson import transaction testing.registerDummySecurityPolicy("chris") context = self._make_context() request = testing.DummyRequest(params={"file": DummyUpload(IMAGE_DATA="NOT_AN_IMAGE"), "title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"File test.jpg is not an image") self.failUnless(transaction.isDoomed())
def test_duplicate_filename(self): import simplejson import transaction testing.registerDummySecurityPolicy("chris") context = self._make_context() context["test.jpg"] = testing.DummyModel() request = testing.DummyRequest(params={"file": DummyUpload(), "title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"Filename test.jpg already exists in this folder") self.failUnless(transaction.isDoomed())
def test_missing_input(self): """Input is missing from the posted form""" import simplejson import transaction karl.testing.registerDummySecurityPolicy("chris") context = self._make_context() request = testing.DummyRequest(params={"title": "Title"}) response = self._call_fut(context, request) self.assertEqual(response.status, "200 OK") data = simplejson.loads(response.body) self.assertEqual(data["error"], u"You must select a file before clicking Upload.") self.failUnless(transaction.isDoomed())
def test_no_filename(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest(params={ 'file': DummyUpload(filename='', ), 'title': 'Title', }, ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'The filename must not be empty') self.failUnless(transaction.isDoomed())
def test_wrong_mimetype(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest(params={ 'file': DummyUpload(type='not/animage', ), 'title': 'Title', }, ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'File test.jpg is not an image') self.failUnless(transaction.isDoomed())
def test_missing_input(self): """Input is missing from the posted form""" import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest(params={ 'title': 'Title', }, ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'You must select a file before clicking Upload.') self.failUnless(transaction.isDoomed())
def test_no_permission(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris', permissive=False) context = self._make_context() request = testing.DummyRequest(params={ 'file': DummyUpload(), 'title': 'Title', }) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'You do not have permission to upload files here.') self.failUnless(transaction.isDoomed())
def test_no_permission(self): import simplejson import transaction testing.registerDummySecurityPolicy('chris', permissive=False) context = self._make_context() request = testing.DummyRequest( params={ 'file': DummyUpload(), } ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'You do not have permission to upload files here.') self.failUnless(transaction.isDoomed())
def test_duplicate_filename(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() context['test.jpg'] = testing.DummyModel() request = testing.DummyRequest(params={ 'file': DummyUpload(), 'title': 'Title', }) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'Filename test.jpg already exists in this folder') self.failUnless(transaction.isDoomed())
def test_no_upload(self): import simplejson import transaction testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest( params={ 'file': testing.DummyModel(), } ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'You must select a file before clicking Upload.') self.failUnless(transaction.isDoomed())
def test_bad_upload(self): import simplejson import transaction testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest( params={ 'file': testing.DummyModel( filename='', file=None, type=None ), } ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'The filename must not be empty') self.failUnless(transaction.isDoomed())
def test_duplicate_filename(self): import simplejson import transaction testing.registerDummySecurityPolicy('chris') context = self._make_context() context['test.jpg'] = testing.DummyModel() request = testing.DummyRequest( params={ 'file': DummyUpload(), } ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'Filename test.jpg already exists in this folder') self.failUnless(transaction.isDoomed())
def test_no_filename(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest( params = { 'file': DummyUpload( filename = '', ), 'title': 'Title', }, ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'The filename must not be empty') self.failUnless(transaction.isDoomed())
def end(self): # ZODB 3.8 + has isDoomed if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): self.tm.abort() if self.tm.commit_veto is not None: try: if self.tm.commit_veto(None, cherrypy.response.status, cherrypy.response.headers): log.debug("Commit veto, calling abort!") self.tm.abort() except: self.tm.abort() raise else: self.tm.commit() else: self.tm.commit() request.in_transaction = False
def test_wrong_imagedata(self): import simplejson import transaction karl.testing.registerDummySecurityPolicy('chris') context = self._make_context() request = testing.DummyRequest( params = { 'file': DummyUpload( IMAGE_DATA = 'NOT_AN_IMAGE', ), 'title': 'Title', }, ) response = self._call_fut(context, request) self.assertEqual(response.status, '200 OK') data = simplejson.loads(response.body) self.assertEqual(data['error'], u'File test.jpg is not an image') self.failUnless(transaction.isDoomed())
def __call__(self, environ, start_response): transaction = self.transaction environ[ekey] = True transaction.begin() ctx = {} def save_status_and_headers(status, headers, exc_info=None): ctx.update(status=status, headers=headers) return start_response(status, headers, exc_info) try: for chunk in self.application(environ, save_status_and_headers): yield chunk except Exception: """Saving the exception""" try: type_, value, tb = sys.exc_info() self.abort() reraise(type_, value, tb) finally: del type_, value, tb # ZODB 3.8 + has isDoomed if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): self.abort() else: if self.commit_veto is not None: try: status, headers = ctx['status'], ctx['headers'] veto = self.commit_veto(environ, status, headers) except: self.abort() raise if veto: self.abort() else: self.commit() else: self.commit()
def __call__(self, environ, start_response): transaction = self.transaction environ[ekey] = True transaction.begin() ctx = {} def save_status_and_headers(status, headers, exc_info=None): ctx.update(status=status, headers=headers) return start_response(status, headers, exc_info) try: result = self.application(environ, save_status_and_headers) except: self.abort() raise # ZODB 3.8 + has isDoomed if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): self.abort() return result if self.commit_veto is not None: try: status, headers = ctx['status'], ctx['headers'] veto = self.commit_veto(environ, status, headers) except: self.abort() raise if veto: self.abort() else: self.commit() return result self.commit() return result
def __exit__(self, t, v, tb): if v is None and not transaction.isDoomed(): transaction.commit() else: transaction.abort()
def undoom_transaction(): import transaction if transaction.isDoomed(): tns = transaction.get() tns.status = "Active"
def commit(self): if transaction.isDoomed(): transaction.abort() else: transaction.commit()
def commit(self): if hasattr(transaction, 'isDoomed') and transaction.isDoomed(): transaction.abort() else: transaction.commit()