def group_del(self, group_id, delete_cascade=True): """Remove a group from the vault. Only if no services are associated with it anymore. :force_delete deletes a group even if it has services associated """ transaction.begin() grp = query(Group).options(eagerload('services_assoc')).filter_by(id=int(group_id)).first() if grp is None: return vaultMsg(False, "Group not found: %s" % (group_id,)) if len(grp.services_assoc): if not delete_cascade: return vaultMsg(False, "Group not empty, cannot delete") else: for service in grp.services_assoc: self.service_del(service.id) # Delete UserGroup elements... q1 = usergroups_table.delete(UserGroup.group_id==grp.id) meta.Session.execute(q1) name = grp.name # Delete Group and commit.. meta.Session.delete(grp) transaction.commit() retval = {'name': name, 'group_id': group_id} return vaultMsg(True, 'Removed group "%s" successfully' % name, retval)
def add_fixtures(self): from pysite.models import DbSession from pprint import pprint sess = DbSession() transaction.begin() try: # Add in this sequence for g in self.__class__.FIXT_GROUPS: data = self.fixtures[g] print("***", g) for it in data: it['owner'] = UNIT_TESTER_UID pprint(it) if g == 'roles': usrmanager.add_role(it) elif g == 'principals': usrmanager.add_principal(it) elif g == 'vmail_domains': vmailmanager.add_domain(it) elif g == 'vmail_mailboxes': vmailmanager.add_mailbox(it) elif g == 'vmail_aliases': vmailmanager.add_alias(it) else: raise Exception("Unknown fixture group: '{0}'".format( g)) transaction.commit() except Exception as e: transaction.abort() raise e
def getPeople(self, options): conn = pymongo.Connection('localhost', 27017, tz_aware=False) dm = datamanager.MongoDataManager( conn, default_database='performance', root_database='performance', conflict_handler_factory=conflict.ResolvingSerialConflictHandler) if options.reload: conn.drop_database('performance') dm.root['people'] = people = People() # Profile inserts transaction.begin() t1 = time.time() for idx in xrange(options.size): klass = (self.personKlass if (MULTIPLE_CLASSES and idx % 2) else self.person2Klass) people[None] = klass('Mr Number %.5i' % idx, random.randint(0, 100)) transaction.commit() t2 = time.time() self.printResult('Insert', t1, t2, options.size) else: people = dm.root['people'] return people
def getPeople(self, options): folder = tempfile.gettempdir() #folder = './' # my /tmp is a tmpfs fname = os.path.join(folder, 'performance_data.fs') if options.reload: try: os.remove(fname) except: pass fs = ZODB.FileStorage.FileStorage(fname) db = ZODB.DB(fs) conn = db.open() root = conn.root() if options.reload: root['people'] = people = PeopleZ() transaction.commit() # Profile inserts transaction.begin() t1 = time.time() for idx in xrange(options.size): klass = (self.personKlass if (MULTIPLE_CLASSES and idx % 2) else self.person2Klass) name = 'Mr Number %.5i' % idx people[name] = klass(name, random.randint(0, 100)) transaction.commit() t2 = time.time() self.printResult('Insert', t1, t2, options.size) else: people = root['people'] return people
def setUp(self): transaction.begin() self._policy = PermissiveSecurityPolicy() self._oldPolicy = setSecurityPolicy(self._policy) self.connection = Zope2.DB.open() self.root = self.connection.root()[ 'Application' ] newSecurityManager( None, AnonymousUser().__of__( self.root ) )
def setUp(self): FakeLDAP.clearTree() try: transaction.begin() except AttributeError: # Zope 2.7 get_transaction().begin() self.connection = Zope2.DB.open() self.root = self.connection.root()["Application"] self.root._setObject("luftest", Folder("luftest")) self.folder = self.root.luftest manage_addLDAPUserFolder(self.folder) luf = self.folder.acl_users host, port = dg("server").split(":") luf.manage_addServer(host, port=port) luf.manage_edit( dg("title"), dg("login_attr"), dg("uid_attr"), dg("users_base"), dg("users_scope"), dg("roles"), dg("groups_base"), dg("groups_scope"), dg("binduid"), dg("bindpwd"), binduid_usage=dg("binduid_usage"), rdn_attr=dg("rdn_attr"), local_groups=dg("local_groups"), implicit_mapping=dg("implicit_mapping"), encryption=dg("encryption"), read_only=dg("read_only"), ) FakeLDAP.addTreeItems(dg("users_base")) FakeLDAP.addTreeItems(dg("groups_base"))
def open(self): import ZODB from ZODB.FileStorage import FileStorage from zc.lockfile import LockError self.path = self.conf['rdf.store_conf'] openstr = os.path.abspath(self.path) try: fs = FileStorage(openstr) except IOError: L.exception("Failed to create a FileStorage") raise ZODBSourceOpenFailError(openstr) except LockError: L.exception('Found database "{}" is locked when trying to open it. ' 'The PID of this process: {}'.format(openstr, os.getpid()), exc_info=True) raise DatabaseConflict('Database ' + openstr + ' locked') self.zdb = ZODB.DB(fs, cache_size=1600) self.conn = self.zdb.open() root = self.conn.root() if 'rdflib' not in root: root['rdflib'] = ConjunctiveGraph('ZODB') self.graph = root['rdflib'] try: transaction.commit() except Exception: # catch commit exception and close db. # otherwise db would stay open and follow up tests # will detect the db in error state L.exception('Forced to abort transaction on ZODB store opening', exc_info=True) transaction.abort() transaction.begin() self.graph.open(self.path)
def verify(self, conn, abort_it): transaction.begin() root = conn.root() ob = root['test'] try: ob2 = root['dup'] except KeyError: if abort_it: # Passed the test. return else: raise else: self.assertTrue(not abort_it, 'Did not abort duplication') l1 = list(ob.items()) l1.sort() l2 = list(ob2.items()) l2.sort() l1 = list(map(lambda k_v: (k_v[0], k_v[1][0]), l1)) l2 = list(map(lambda k_v1: (k_v1[0], k_v1[1][0]), l2)) self.assertEqual(l1, l2) self.assertTrue(ob._p_oid != ob2._p_oid) self.assertEqual(ob._p_jar, ob2._p_jar) oids = {} for v in ob.values(): oids[v._p_oid] = 1 for v in ob2.values(): assert v._p_oid not in oids, ( 'Did not fully separate duplicate from original') transaction.commit()
def setUp(self): transaction.begin() self.app = makerequest(Zope2.app()) f = self.app.manage_addProduct['PageTemplates'].manage_addPageTemplate self._addPT = f self.title = 'title of page template' self.text = 'text of page template'
def group_del_service(self, group_id, service_id): """Remove the association between a group and a service, simply.""" transaction.begin() grp = query(Group).filter_by(id=group_id).first() if not grp: return vaultMsg(False, "Group not found: %s" % str(e)) # TODO: DRY out this place, much copy from del_user and stuff sgs = query(ServiceGroup).filter_by(service_id=service_id).all() if grp.id not in [sg.group_id for sg in sgs]: return vaultMsg(False, "Service is not in group: %s" % str(e)) sg = [sg for sg in sgs if grp.id == sg.group_id][0] # Make sure we don't lose all of the service's crypted information. if len(sgs) < 2: return vaultMsg(False, "This is the last group this service is in. Either delete the service, or add it to another group first") # Remove the GroupService from the Group object. meta.Session.delete(sg) transaction.commit() return vaultMsg(True, "Removed service from group successfully")
def __call__(self, name, content_type, data): ctr = getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(name.lower(), '', '') or 'File' # otherwise I get ZPublisher.Conflict ConflictErrors # when uploading multiple files upload_lock.acquire() name = safe_unicode(name) chooser = INameChooser(self.context) newid = chooser.chooseName(name, self.context.aq_parent) try: transaction.begin() obj = ploneutils._createObjectByType(type_, self.context, newid) mutator = obj.getPrimaryField().getMutator(obj) mutator(data, content_type=content_type, filename=name) obj.setTitle(name) if hasattr(obj, 'setFilename'): # if chunk uploaded, needs override obj.setFilename(name) obj.reindexObject() notify(ObjectInitializedEvent(obj)) notify(ObjectModifiedEvent(obj)) transaction.commit() finally: upload_lock.release() return obj
def initialize_sql(engine): DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all(engine) try: transaction.begin() session = DBSession() s1 = Subject('Prob & Stat', 'mast30020') session.add(s1) sec1 = Section('Sigma algebras', 'sigalg', 'mast30020', 1) sec2 = Section('Random variables', 'randvar', 'mast30020', 2) sec3 = Section('Expectation', 'expectation', 'mast30020', 3) session.add(sec1); session.add(sec2); session.add(sec3) para1 = Paragraph('Here is some maths', 1, 1) session.add(para1) comment1 = Comment('AF','[email protected]',datetime.datetime.now(), 'That\'s some cool maths.', 1) session.add(comment1) s2 = Subject('Linear Models', 'mast30025') session.add(s2) sec1 = Section('Linear Algebra', 'linalg', 'mast30025', 1) sec2 = Section('Random variables', 'randvar', 'mast30025', 2) sec3 = Section('Expectation', 'expectation', 'mast30025', 3) session.add(sec1); session.add(sec2); session.add(sec3) transaction.commit() except IntegrityError: pass
def checkMultipleUndoInOneTransaction(self): # Verify that it's possible to perform multiple undo # operations within a transaction. If ZODB performs the undo # operations in a nondeterministic order, this test will often # fail. conn = self._db.open() try: root = conn.root() # Add transactions that set root["state"] to (0..5) for state_num in range(6): transaction.begin() root['state'] = state_num transaction.get().note('root["state"] = %d' % state_num) transaction.commit() # Undo all but the first. Note that no work is actually # performed yet. transaction.begin() log = self._db.undoLog() self._db.undoMultiple([log[i]['id'] for i in range(5)]) transaction.get().note('undo states 1 through 5') # Now attempt all those undo operations. transaction.commit() # Sanity check: we should be back to the first state. self.assertEqual(root['state'], 0) finally: transaction.abort() conn.close()
def testAbortAfterCommit(self): # This is a regression test which used to wedge the transaction # machinery when using PostgreSQL (and perhaps other) connections. # Basically, if a commit failed, there was no way to abort the # transaction. Leaving the transaction wedged. transaction.begin() session = Session() conn = session.connection() # At least PostgresSQL requires a rollback after invalid SQL is executed self.assertRaises(Exception, conn.execute, "BAD SQL SYNTAX") mark_changed(session) try: # Thus we could fail in commit transaction.commit() except: # But abort must succed (and actually rollback the base connection) transaction.abort() pass # Or the next transaction the next transaction will not be able to start! transaction.begin() session = Session() conn = session.connection() conn.execute("SELECT 1 FROM test_users") mark_changed(session) transaction.commit()
def testTwoPhase(self): session = Session() if not session.twophase: return session.add(User(id=1, firstname='udo', lastname='juergens')) session.add(User(id=2, firstname='heino', lastname='n/a')) session.flush() transaction.commit() # Test that we clean up after a tpc_abort t = transaction.get() def target(): return engine.connect().recover_twophase() dummy = DummyDataManager(key='~~~dummy.last', target=target) t.join(dummy) session = Session() query = session.query(User) rows = query.all() session.delete(rows[0]) session.flush() result = None try: t.commit() except DummyTargetResult as e: result = e.args[0] except DummyTargetRaised as e: raise e.args[0] self.assertEqual(len(result), 1, "Should have been one prepared transaction when dummy aborted") transaction.begin() self.assertEqual(len(engine.connect().recover_twophase()), 0, "Test no outstanding prepared transactions")
def testDeepCopyCanInvalidate(self): """ Tests regression for invalidation problems related to missing readers and writers values in cloned objects (see http://mail.zope.org/pipermail/zodb-dev/2008-August/012054.html) """ import ZODB.MappingStorage database = DB(ZODB.blob.BlobStorage( 'blobs', ZODB.MappingStorage.MappingStorage())) connection = database.open() root = connection.root() transaction.begin() root['blob'] = Blob() transaction.commit() stream = StringIO() p = Pickler(stream, 1) p.dump(root['blob']) u = Unpickler(stream) stream.seek(0) clone = u.load() clone._p_invalidate() # it should also be possible to open the cloned blob # (even though it won't contain the original data) clone.open() # tearDown database.close()
def _initialize_tahrir_connection(self): if hasattr(self.l, 'tahrir'): return global_settings = self.hub.config.get("badges_global", {}) database_uri = global_settings.get('database_uri') if not database_uri: raise ValueError('Badges consumer requires a database uri') session_cls = scoped_session(sessionmaker( extension=ZopeTransactionExtension(), bind=create_engine(database_uri), )) self.l.tahrir = tahrir_api.dbapi.TahrirDatabase( session=session_cls(), autocommit=False, notification_callback=fedbadges.utils.notification_callback, ) issuer = global_settings.get('badge_issuer') transaction.begin() self.issuer_id = self.l.tahrir.add_issuer( issuer.get('issuer_origin'), issuer.get('issuer_name'), issuer.get('issuer_org'), issuer.get('issuer_contact') ) transaction.commit()
def __call__(self, name, content_type, data): ctr = getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(name.lower(), '', '') or 'File' # XXX: quick fix for german umlauts name = name.decode("utf8") normalizer = getUtility(IFileNameNormalizer) chooser = INameChooser(self.context) # otherwise I get ZPublisher.Conflict ConflictErrors # when uploading multiple files upload_lock.acquire() # this should fix #8 newid = chooser.chooseName(normalizer.normalize(name), self.context.aq_parent) try: transaction.begin() obj = ploneutils._createObjectByType(type_, self.context, newid) mutator = obj.getPrimaryField().getMutator(obj) mutator(data, content_type=content_type) obj.setTitle(name) obj.reindexObject() notify(ObjectInitializedEvent(obj)) notify(ObjectModifiedEvent(obj)) transaction.commit() finally: upload_lock.release() return obj
def service_passwd(self, service_id, newsecret): """Change the passwd for a given service""" transaction.begin() # number please service_id = int(service_id) serv = query(Service).get(service_id) groups = serv.groups (seckey, ciphertext) = encrypt_secret(newsecret) serv.secret = ciphertext serv.secret_last_modified = datetime.now() # TODO absolutely: verify this requesting user has access to the # password first. YES, he can add a new password, but not modify # something he doesn't have access to, first. # TODO: for traceability, mark the date we changed the password. # for sg in serv.groups_assoc: eg = [g for g in groups if g.id == sg.group_id][0].elgamal() sg.cryptsymkey = encrypt_longmsg(eg, seckey) grouplist = [g.name for g in groups] transaction.commit() return vaultMsg(True, "Password updated for service.", {'service_id': service_id, 'encrypted_for': grouplist})
def group_put(self, group_id, data): """Put a single group's data back to the Vault""" transaction.begin() try: grp = query(Group).filter_by(id=group_id).one() except InvalidReq, e: return vaultMsg(False, "Group not found: %s" % str(e))
def run_migrations_online(): if DBSession.bind is None: raise ValueError( "\nYou must run Kotti's migration using the 'kotti-migrate' script" "\nand not through 'alembic' directly." ) transaction.begin() connection = DBSession.connection() context.configure( connection=connection, target_metadata=metadata, ) try: context.run_migrations() mark_changed(DBSession()) except: traceback.print_exc() transaction.abort() else: transaction.commit() finally: # connection.close() pass
def machine_del(self, machine_id): """Delete a machine from database, bringing on all child services.""" transaction.begin() # Get machine machine = query(model.Machine).get(int(machine_id)) if not machine: return vaultMsg(True, "No such machine: m#%s" % machine_id) # Get all the services that will be deleted servs = query(model.Service).join('machine') \ .filter(model.Machine.id == machine_id).all() servs_ids = [s.id for s in servs] # Make sure no service is child of this one if servs_ids: childs = query(model.Service) \ .filter(model.Service.parent_service_id.in_(servs_ids))\ .all() else: childs = [] # Don't bother for parents/childs if we're going to delete it anyway. remnants = list(set(childs).difference(set(servs))) if len(remnants): # There are still some childs left, we can't delete this one. retval = [] for x in remnants: retval.append({'id': x.id, 'url': x.url}) return vaultMsg(False, "Services still child of this machine's services", {'childs': retval}) if servs_ids: query(model.ServiceGroup)\ .filter(model.ServiceGroup.service_id.in_(servs_ids))\ .delete(synchronize_session=False) query(model.Service)\ .filter(model.Service.id.in_(servs_ids))\ .delete(synchronize_session=False) query(model.Machine).filter(model.Machine.id==machine_id).delete(synchronize_session=False) # Delete all related groupciphers # raise Exception # d = sql.delete(model.servicegroups_table) \ # .where(model.servicegroups_table.c.service_id.in_(servs_ids)) # # Delete the services related to machine_id # d2 = sql.delete(model.services_table) \ # .where(model.services_table.c.id.in_(servs_ids)) # # Delete the machine # d3 = sql.delete(model.machines_table) \ # .where(model.machines_table.c.id == machine_id) # meta.Session.execute(d) # meta.Session.execute(d2) # meta.Session.execute(d3) transaction.commit() return vaultMsg(True, 'Deleted machine m#%s successfully' % machine_id)
def deleteById(self, id): ''' Elimina un Proyecto de la base de datos @param: id del proyecto''' roles = DBSession.query(RolUsuario).filter(RolUsuario.id_proyecto==id) if roles.count()>0: return False transaction.begin() u = self.getById(id) if u.fases != []: for i in u.fases: transaction.begin() recurso = DBSession.query(Recurso).filter(Recurso.id_fase==i.id_fase).one() DBSession.delete(recurso) transaction.commit() transaction.begin() fase = DBSession.query(Fase).filter(Fase.id_fase==i.id_fase).one() DBSession.delete(fase) transaction.commit() transaction.begin() recurso = DBSession.query(Recurso).filter(id == Recurso.id_proyecto).one() DBSession.delete(recurso) transaction.commit() transaction.begin() u = self.getById(id) DBSession.delete(u) transaction.commit() transaction.commit() return True
def actualizarPrefijosItem(self, id_tipo_item, viejo, nuevo): '''Actualiza los prefijos de los items al editar el tipo de item @param id_tipo_item: id del tipo de item @param viejo: prefijo anterior @param nuevo:prefijo nuevo ''' if nuevo == viejo: return 0; if nuevo=="": ti = DBSession.query(TipoItem).filter(TipoItem.id_tipo_item == id_tipo_item).one() nombre = ti.nombre n = nombre.split(' ') nu = "".join(n) nuevo = nu.upper()[0:3] ti.prefijo=nuevo DBSession.merge(ti) transaction.commit() transaction.begin() items = DBSession.query(Item).filter(Item.id_tipo_item==id_tipo_item).all() for i in items: cod = i.codigo lista = cod.split('-') nro = lista[len(lista)-1] n = nro.strip(' ') i.codigo= nuevo + " - " + n self.update(i) transaction.commit()
def _add_to_store(self, g, graph_name=False): if self.conf['rdf.store'] == 'SPARQLUpdateStore': # XXX With Sesame, for instance, it is probably faster to do a PUT over # the endpoint's rest interface. Just need to do it for some common # endpoints try: gs = g.serialize(format="nt") except Exception: gs = _triples_to_bgp(g) if graph_name: s = " INSERT DATA { GRAPH " + graph_name.n3() + " {" + gs + " } } " else: s = " INSERT DATA { " + gs + " } " L.debug("update query = " + s) self.conf['rdf.graph'].update(s) else: gr = self.conf['rdf.graph'] if self.conf['rdf.source'] == 'ZODB': transaction.commit() transaction.begin() for x in g: gr.add(x) if self.conf['rdf.source'] == 'ZODB': transaction.commit() transaction.begin()
def setUp(self): super(ZPTMacros, self).setUp() zope.component.provideAdapter(DefaultTraversable, (None,)) transaction.begin() self.app = makerequest(Zope2.app()) f = self.app.manage_addProduct['PageTemplates'].manage_addPageTemplate self._addPT = f self.title = 'title of page template' self.text = """ <metal:block use-macro="template/macros/themacro"> <p metal:fill-slot="theslot"> This is in the slot </p> </metal:block> <tal:block condition="nothing"> <div metal:define-macro="themacro"> <h1>This is the header</h1> <p metal:define-slot="theslot"> This will be replaced </p> </div> </tal:block> """ self.result = """<div>
def run(): request = get_system_request() if request.user is None: # in test, db connection closed return catalog = find_catalog('dace') global last_transaction cache_key = _get_cache_key() last_transaction = last_transaction_by_machine.setdefault(cache_key, '') last_tid = catalog._p_jar.db().lastTransaction() if last_transaction != last_tid: last_transaction_by_machine[cache_key] = last_tid transaction.begin() try: system_actions = [a for a in getAllSystemActions() if getattr(a, 'process', None) or a.isstart] log.info("new zodb transactions, actions to check: %s", len(system_actions)) for action in system_actions: _call_action(action) except Exception as e: log.exception(e) log.info("actions to check: done") run_crawler()
def testRedo(self): database = DB(self._storage) connection = database.open() root = connection.root() blob = Blob() transaction.begin() blob.open('w').write(b('this is state 1')) root['blob'] = blob transaction.commit() transaction.begin() blob = root['blob'] blob.open('w').write(b('this is state 2')) transaction.commit() database.undo(database.undoLog(0, 1)[0]['id']) transaction.commit() self.assertEqual(blob.open('r').read(), b('this is state 1')) database.undo(database.undoLog(0, 1)[0]['id']) transaction.commit() self.assertEqual(blob.open('r').read(), b('this is state 2')) database.close()
def setUp(self): self._trap_warning_output() transaction.begin() app = self.app = makerequest(Zope2.app()) # Log in as a god :-) newSecurityManager( None, UnrestrictedUser('god', 'god', ['Manager'], '') ) app.manage_addProduct['CMFDefault'].manage_addCMFSite('CalendarTest') self.Site = app.CalendarTest manage_addExternalMethod(app.CalendarTest, id='install_events', title="Install Events", module="CMFCalendar.Install", function="install") ExMethod = app.restrictedTraverse('/CalendarTest/install_events') ExMethod() self.Tool = app.CalendarTest.portal_calendar self.Site.clearCurrentSkin() self.Site.setupCurrentSkin(app.REQUEST) # sessioning setup if getattr(app, 'temp_folder', None) is None: temp_folder = MountedTemporaryFolder('temp_folder') app._setObject('temp_folder', temp_folder) if getattr(app.temp_folder, 'session_data', None) is None: session_data = TransientObjectContainer('session_data') app.temp_folder._setObject('session_data', session_data) app.REQUEST.set_lazy( 'SESSION', app.session_data_manager.getSessionData )
def __init__(self, db, model): """ :param zerodb.DB db: Database to link model to :param model: Data model (subclass of zerodb.models.Model) """ self._model = model self._db = db self._catalog_name = "catalog__" + model.__modelname__ self._intid_name = "store__" + model.__modelname__ if not transaction.manager._txn and \ (self._intid_name not in db._root or self._catalog_name not in db._root): transaction.begin() commit = True else: commit = False if self._intid_name not in db._root: _objects = model.create_store() db._root[self._intid_name] = _objects if self._catalog_name not in db._root: _catalog = model.create_catalog() db._root[self._catalog_name] = _catalog if commit: transaction.commit()
def run_transaction(db): tx = transaction.begin() conn = db.open() try: root = conn.root() root['key'] = 'abcd' * 1000 tx.commit() except: tx.abort() raise finally: conn.close()
def process_oils(session_class): session = session_class() record_ids = [r.adios_oil_id for r in session.query(ImportedRecord)] session.close() logger.info('Adding Oil objects...') for record_id in record_ids: # Note: committing our transaction for every record slows the # import job significantly. But this is necessary if we # want the option of rejecting oil records. session = session_class() transaction.begin() rec = (session.query(ImportedRecord).filter( ImportedRecord.adios_oil_id == record_id).one()) try: add_oil(rec) transaction.commit() except OilRejected as e: logger.warning(repr(e)) transaction.abort()
def checkSavepointRollbackAndReadCurrent(self): ''' savepoint rollback after readcurrent was called on a new object should not raise POSKeyError ''' cn = self._db.open() try: transaction.begin() root = cn.root() added_before_savepoint = P() root['added_before_savepoint'] = added_before_savepoint sp = transaction.savepoint() added_before_savepoint.btree = new_btree = OOBTree() cn.add(new_btree) new_btree['change_to_trigger_read_current'] = P() sp.rollback() transaction.commit() self.assertTrue('added_before_savepoint' in root) finally: transaction.abort() cn.close()
def transaction(): """Create a new transaction for a test. The transaction is automatically marked as doomed to prevent it from being committed accidentily. At the end of the test it will be rolled back. """ import transaction tx = transaction.begin() tx.doom() # Make sure a transaction can never be commited. # Mock out transaction.commit so code can never commit around us. with mock.patch('transaction.Transaction.commit'): yield tx.abort()
def wait_for_annotation(job, name): for i in range(60): t = transaction.begin() try: if name in job.annotations: return job.annotations[name] except ZODB.POSException.ReadConflictError: # storage does not have MVCC pass time_sleep(0.1) else: assert False, 'annotation never found'
def make(self, dependent_resources=None): """See `TestResource`""" if DEBUG_RESOURCES: print "*** Make %s ***" % self.dbname watcher = self.get_watcher() watcher.enable(self.dbname) if self.schemas is None: self.schemas = [s.create_schema() for s in self.schema_modules] store = get_admin_store(self.store_name) transaction.abort() for s in self.schemas: s.upgrade(store) transaction.commit() transaction.begin() for s in reversed(self.schemas): s.delete(store) transaction.commit() self.saw_commit = False watcher.hook(self.dbname, self._notify_change) watcher.reset(self.dbname) return self
def wait_for_result(job, seconds=6): for i in range(seconds * 10): t = transaction.begin() try: if job.status == zc. async .interfaces.COMPLETED: return job.result except ZODB.POSException.ReadConflictError: # storage does not have MVCC pass time_sleep(0.1) else: assert False, 'job never completed'
def page_query(q, n=1000, verbose=False, commit=False): """ http://stackoverflow.com/a/1217947 """ s = time.time() offset = 0 while True: r = False for elem in q.limit(n).offset(offset): r = True yield elem if commit: # pragma: no cover transaction.commit() transaction.begin() offset += n e = time.time() if verbose: print e - s, offset, 'done' # pragma: no cover s = e if not r: break
def testUndo(self): database = DB(self._storage) connection = database.open() root = connection.root() transaction.begin() blob = Blob() blob.open('w').write('this is state 1') root['blob'] = blob transaction.commit() transaction.begin() blob = root['blob'] blob.open('w').write('this is state 2') transaction.commit() database.undo(database.undoLog(0, 1)[0]['id']) transaction.commit() self.assertEqual(blob.open('r').read(), 'this is state 1') database.close()
def test_folder_post_1_level(self): response = requests.post( self.portal_url, headers={"Accept": "application/json"}, auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD), json={ "@type": "Folder", "id": "myfolder", "title": "My Folder", "__children__": [{ "@type": "Document", "id": "mydocument", "title": "My Document" }], }, ) self.assertEqual(201, response.status_code, response.content) transaction.begin() # first level self.assertIsNotNone(self.portal.get("myfolder")) self.assertEqual("My Folder", self.portal.myfolder.Title()) self.assertEqual("Folder", response.json().get("@type")) self.assertEqual("myfolder", response.json().get("id")) self.assertEqual("My Folder", response.json().get("title")) expected_url = self.portal_url + u"/myfolder" self.assertEqual(expected_url, response.json().get("@id")) # second level children_obj = self.portal.myfolder.get("mydocument") self.assertIsNotNone(children_obj) children_json = response.json()["__children__"][0] self.assertEqual("My Document", children_obj.Title()) self.assertEqual("Document", children_json.get("@type")) self.assertEqual("mydocument", children_json.get("id")) self.assertEqual("My Document", children_json.get("title")) expected_url = self.portal_url + u"/myfolder/mydocument" self.assertEqual(expected_url, children_json.get("@id"))
def object_caching(self, people, peopleCnt): # Profile object caching transaction.begin() t1 = time.time() [person.name for person in people.values()] [person.name for person in people.values()] #cProfile.runctx( # '[person.name for person in people.values()]', globals(), locals()) t2 = time.time() transaction.commit() self.printResult('Fast Read (caching x2)', t1, t2, peopleCnt*2) transaction.begin() t1 = time.time() [person.name for person in people.values()] [person.name for person in people.values()] [person.name for person in people.values()] #cProfile.runctx( # '[person.name for person in people.values()]', globals(), locals()) t2 = time.time() transaction.commit() self.printResult('Fast Read (caching x3)', t1, t2, peopleCnt*3) transaction.begin() t1 = time.time() [person.name for person in people.values()] [person.name for person in people.values()] [person.name for person in people.values()] [person.name for person in people.values()] #cProfile.runctx( # '[person.name for person in people.values()]', globals(), locals()) t2 = time.time() transaction.commit() self.printResult('Fast Read (caching x4)', t1, t2, peopleCnt*4)
def test_savepoints_earlier_rollback(self): from transaction.interfaces import InvalidSavepointRollbackError # First of all, make sure we are in a clean transaction transaction.begin() self.assertEquals(len(_listSpools()), 0) email1 = self._makeAndSend() # Now that the email has been sent, there should be two files: The # lock file and the actual email. The lockfile stays until the # transaction commits. self.assertEquals(len(_listSpools()), 2) # create a savepoint savepoint1 = transaction.savepoint() # send a second mail email2 = self._makeAndSend() self.assertEquals(len(_listSpools()), 4) # create another savepoint savepoint2 = transaction.savepoint() # send a third mail email3 = self._makeAndSend() self.assertEquals(len(_listSpools()), 6) # rollback should remove email2 and email3 savepoint1.rollback() self.assertEquals(len(_listSpools()), 2) # out of order rollback, should raise an exception self.assertRaises(InvalidSavepointRollbackError, savepoint2.rollback) # Aborting a transaction should remove the email file and the # lockfile. transaction.abort() self.assertEquals(len(_listSpools()), 0)
def setNodeText(self, jid, node, text): transaction.begin() app = Zope2.app() try: try: portal = app.unrestrictedTraverse(self.portal_id, None) if portal is None: raise DSCException('Portal with id %s not found' % self.portal_id) setSite(portal) settings = getUtility(IRegistry) autosave = settings.get( 'collective.xmpp.autoSaveCollaboration', False) if not autosave: transaction.abort() return acl_users = getToolByName(portal, 'acl_users') user_id = unescapeNode(JID(jid).user) user = acl_users.getUserById(user_id) if user is None: raise DSCException('Invalid user %s' % user_id) newSecurityManager(None, user) ct = getToolByName(portal, 'portal_catalog') uid, html_id = node.split('#') item = ct.unrestrictedSearchResults(UID=uid) if not item: raise DSCException('Content with UID %s not found' % uid) item = ICollaborativelyEditable(item[0].getObject()) item.setNodeTextFromHtmlID(html_id, text) transaction.commit() except: transaction.abort() raise finally: noSecurityManager() setSite(None) app._p_jar.close() return text
def docSetUp(test=None): zope_setup.placefulSetUp() zope_setup.setUpAnnotations() zope_setup.setUpTraversal() test.globs['app'] = setUpSchoolToolSite() setUpRelationships() provideAdapter(getSchoolYearContainer) provideAdapter(getTermContainer, (Interface, )) provideAdapter(getSchoolYearForTerm) provideAdapter(getCourseContainer) provideAdapter(getCourseContainerForApp) provideAdapter(getSectionContainer) provideAdapter(getTermForSection) provideAdapter(getTermForSectionContainer) provideAdapter(getTimetableContainer) provideAdapter(getScheduleContainer) setUpIntIds() provideAdapter(SectionNameChooser, (ISectionContainer, )) transaction.begin()
def __call__(self, title, description, portal_type): context = aq_inner(self.context) charset = context.getCharset() title= title.decode(charset) description = description.decode("utf8") normalizer = component.getUtility(IIDNormalizer) chooser = INameChooser(self.context) newid = chooser.chooseName(normalizer.normalize(title), self.context.aq_parent) # otherwise I get ZPublisher.Conflict ConflictErrors # when uploading multiple files upload_lock.acquire() try: transaction.begin() obj = ploneutils._createObjectByType(portal_type, self.context, newid) obj.setTitle(title) obj.setDescription(description) obj.reindexObject() transaction.commit() finally: upload_lock.release() return obj
def test_alreadyconnected(alreadyconnected_config, alreadyconnected_app, facebook_user, db_session): """Try to connect facebook account to logged in user used by other user check redirect from SocialAccountAlreadyConnected.""" # this user will be logged and trying to connect facebook's user account. fresh_user = User( email='*****@*****.**', password='******', address_ip='127.0.0.1') db_session.add(fresh_user) transaction.commit() user = db_session.merge(facebook_user) fresh_user = db_session.merge(fresh_user) # mock request profile = { 'accounts': [{'domain': u'facebook.com', 'userid': user.provider_id('facebook')}], 'displayName': u'teddy', 'preferredUsername': u'teddy', 'emails': [{'value': u'*****@*****.**'}], 'name': u'ted' } credentials = {'oauthAccessToken': '7897048593434'} provider_name = u'facebook' provider_type = u'facebook' request = testing.DummyRequest() request.user = fresh_user request.registry = alreadyconnected_config.registry request.remote_addr = u'127.0.0.123' request.context = AuthenticationComplete(profile, credentials, provider_name, provider_type) request._ = lambda msg, *args, **kwargs: msg request.login_perform = MagicMock(name='login_perform') request.login_perform.return_value = {'status': True} # call! view = SocialLoginViews(request) out = view() assert out.location == EVENT_PATH.format(SocialAccountAlreadyConnected) transaction.begin() fresh_user = db_session.merge(fresh_user) assert fresh_user.provider_id('facebook') is None
def __enter__(self): import transaction from zope.sqlalchemy.datamanager import join_transaction from sqlalchemy.orm.scoping import ScopedRegistry self._original_registry = self._DBSession.registry self._DBSession.registry = ScopedRegistry( self._DBSession.session_factory, self._get_scope) self.scope = self txn = transaction.begin() session = self._DBSession() join_transaction(session) transaction.manager.free(txn) return self
def wrapped_f(sess_maker, lgg: logging.Logger, user, *args, begin_transaction: bool = True, **kwargs): if begin_transaction: transaction.begin() sess = sess_maker() try: job = Scheduler.find(sess, self.job) """:type: Scheduler""" user = pam.User.find(sess, user) job.start(user) sp = transaction.savepoint() try: out = f(sess, lgg, user, begin_transaction, *args, **kwargs) except Exception as exc: lgg.exception(exc) sp.rollback() out = [ str(exc), traceback.format_exception(*sys.exc_info()) ] job.stop_error(user, out) else: job.stop_ok(user, out) except: if begin_transaction: transaction.abort() raise else: if begin_transaction: transaction.commit() return job
def setUp(self): with open(filedata, 'rb') as fd: self.data = fd.read() self.connection = makeConnection() self.eventCatcher = EventCatcher() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = BytesIO() self.app = makerequest(self.root, stdout=responseOut) self.app.dcm = DummyCacheManager() factory = getattr(self.app, self.factory) factory('file', file=self.data, content_type=self.content_type) self.app.file.ZCacheable_setManagerId('dcm') self.app.file.ZCacheable_setEnabled(enabled=1) setattr(self.app, ZCM_MANAGERS, ('dcm',)) # Hack, we need a _p_mtime for the file, so we make sure that it # has one. transaction.commit() except Exception: transaction.abort() self.connection.close() raise transaction.begin() self.file = getattr(self.app, 'file') # Since we do the create here, let's test the events here too self.assertEqual(1, len(self.eventCatcher.created)) self.assertTrue( aq_base(self.eventCatcher.created[0].object) is aq_base(self.file)) self.assertEqual(1, len(self.eventCatcher.modified)) self.assertTrue( aq_base(self.eventCatcher.created[0].object) is aq_base(self.file)) self.eventCatcher.reset()
def test_post_to_folder_with_apostrophe_dont_return_500(self): response = requests.post( self.portal.folder1.absolute_url(), headers={"Accept": "application/json"}, auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD), json={ "@type": "Document", "id": "mydocument2", "title": "My Document 2", "text": { "content-type": "text/html", "encoding": "utf8", "data": "<p>example with '</p>", }, }, ) self.assertEqual(201, response.status_code) transaction.begin() self.assertEqual("<p>example with '</p>", self.portal.folder1.mydocument2.text.raw) self.assertEqual("<p>example with '</p>", response.json()["text"]["data"])
def test_savepoints(self): # First of all, make sure we are in a clean transaction transaction.begin() self.assertEquals(len(_listSpools()), 0) email1 = self._makeAndSend() # Now that the email has been sent, there should be two files: The # lock file and the actual email. The lockfile stays until the # transaction commits. self.assertEquals(len(_listSpools()), 2) # create a savepoint savepoint1 = transaction.savepoint() # send a second mail email2 = self._makeAndSend() self.assertEquals(len(_listSpools()), 4) # create another savepoint savepoint2 = transaction.savepoint() # send a third mail email3 = self._makeAndSend() self.assertEquals(len(_listSpools()), 6) # rollback, this should remove email3 savepoint2.rollback() self.assertEquals(len(_listSpools()), 4) # rollback again, this should remove email2 savepoint1.rollback() self.assertEquals(len(_listSpools()), 2) # Aborting a transaction should remove the email file and the # lockfile. transaction.abort() self.assertEquals(len(_listSpools()), 0)
def app(): zodb_utils = ZODBUtils() conn = zodb_utils.open_connection('./db_location/zodb_bank.db') root = conn.root() noah = root['noah'] print('Before Deposit Or Withdraw') print('-' * 30) print(noah) jermy = root['jermy'] print(jermy) print('-' * 30) transaction.begin() noah.deposit(300) jermy.withdraw(300) transaction.commit() print('After Deposit Or Withdraw') print('-' * 30) print(noah) print(jermy) print('-' * 30)
def _get_storage(self): settings = { 'filestore': 'local', 'filestore.directory': self.directory } storage = get_storage_factory(settings)() assert hasattr(storage, 'put') assert hasattr(storage, 'url') trxn = transaction.begin() trxn.join(storage) return storage
def setUp(init_db=True, **kwargs): # _turn_warnings_into_errors() from kotti import _resolve_dotted from kotti import conf_defaults tearDown() settings = conf_defaults.copy() settings['kotti.secret'] = 'secret' settings['kotti.secret2'] = 'secret2' settings['kotti.populators'] = 'kotti.testing._populator' settings.update(kwargs.get('settings', {})) settings = _resolve_dotted(settings) kwargs['settings'] = settings config = testing.setUp(**kwargs) config.add_default_renderers() if init_db: _initTestingDB() transaction.begin() return config
def test_remainder_cyclic_reference_restoration(self): # test whether the remainder pickler properly stores cyclic references # back to the object itself. ob1 = TestObject() ob1.myself = ob1 conn1 = self.db.open() try: root = conn1.root() transaction.begin() root['TestRoot2'] = ob1 transaction.commit() conn2 = self.db.open() try: ob2 = conn2.root()['TestRoot2'] self.assert_(ob2.myself is ob2) self.assert_(ob2 is not ob1) # Verify it didn't cheat somehow finally: conn2.close() finally: conn1.close()
def duplicate(self, conn, abort_it): transaction.begin() transaction.get().note(u'duplication') root = conn.root() ob = root['test'] assert len(ob) > 10, 'Insufficient test data' try: import tempfile with tempfile.TemporaryFile(prefix="DUP") as f: ob._p_jar.exportFile(ob._p_oid, f) assert f.tell() > 0, 'Did not export correctly' f.seek(0) new_ob = ob._p_jar.importFile(f) self.assertEqual(new_ob, ob) root['dup'] = new_ob if abort_it: transaction.abort() else: transaction.commit() except: transaction.abort() raise
def test_saving(self): tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path) db["a"] = churro.Persistent() db.save() result = read_json(os.path.join(self.churrodb_path, "a.churro")) self.assertDictEqual( { "__churro_data__": {}, "__churro_class__": "churro.Persistent" }, result)
def testRedoOfCreation(self): database = DB(self._storage) connection = database.open() root = connection.root() blob = Blob() transaction.begin() blob.open('w').write('this is state 1') root['blob'] = blob transaction.commit() database.undo(database.undoLog(0, 1)[0]['id']) transaction.commit() self.assertRaises(KeyError, root.__getitem__, 'blob') database.undo(database.undoLog(0, 1)[0]['id']) transaction.commit() self.assertEqual(blob.open('r').read(), 'this is state 1') database.close()
def add(user): """ Adds the User-entity to the repository. :param user: The User-entity to add. :raises (KeyError, DuplicateIndexError): If a user with one of the same indexes already exists within the repository. >>> list(all()) [] >>> gandalf = User("Gandalf", "pass", '*****@*****.**') >>> dumbledore = User("Dumbledore", "secret", '*****@*****.**') >>> add(gandalf) >>> add(dumbledore) >>> sorted([user.name for user in all()]) ['Dumbledore', 'Gandalf'] """ try: transaction.begin() database_root().users.add(user) transaction.commit() except (KeyError, DuplicateIndexError): transaction.abort() raise
def test_visit_sublocations_check_class_only_not_activate(self): from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage import transaction from zope.interface import alsoProvides cat = self._makeOne() content = PersistentContent() alsoProvides(content, INoAutoIndex) cat.mock_catalog_data.append((4, content)) db = DB(DemoStorage()) self.addCleanup(db.close) transaction.begin() conn = db.open() self.addCleanup(conn.close) conn.root.cat = cat transaction.commit() transaction.begin() conn.cacheMinimize() assert_that(conn.root.cat.mock_catalog_data[1][1], is_(NoIndexContent)) assert_that(conn.root.cat.mock_catalog_data[1][1]._p_status, is_('ghost')) assert_that(conn.root.cat.mock_catalog_data[-1][1], is_(PersistentContent)) assert_that(conn.root.cat.mock_catalog_data[-1][1]._p_status, is_('ghost')) locs = list(cat._visitSublocations()) assert_that(locs, has_length(1)) assert_that(locs[0], contains(1, is_(Content))) # Still a ghost assert_that(conn.root.cat.mock_catalog_data[1][1]._p_status, is_('ghost')) # But the one that alsoProvides() had to wake up assert_that(conn.root.cat.mock_catalog_data[-1][1]._p_status, is_('saved'))