Exemplo n.º 1
27
    def run(self):
        self._prepare()
        self._logger.info('Running task {}.. (delay: {})'.format(self._task.id, self._executionDelay))

        try:
            for i, retry in enumerate(transaction.attempts(self._config.task_max_tries)):
                with retry:
                    self._logger.info('Task attempt #{}'.format(i))
                    if i > 0:
                        self._prepare_retry()
                    try:
                        self._process_task()
                        break
                    except ConflictError:
                        transaction.abort()
                    except ClientDisconnected:
                        self._logger.warning("Retrying for the {}th time in {} secs..".format(i + 1, seconds))
                        transaction.abort()
                        time.sleep(i * 10)
                    except TaskDelayed, e:
                        self._logger.info("{} delayed by {} seconds".format(self._task, e.delaySeconds))
                        self._delayed = True
                        self._executionDelay = 0
                        time.sleep(e.delaySeconds)
            flush_after_commit_queue(True)
            GenericMailer.flushQueue(True)
Exemplo n.º 2
0
    def test_conflict_del_1(self):
        """Check conflict detection. We modify and delete the same object in
        different transactions, simulating separate processes."""

        foo = Foo('foo-first')
        self.dm.root['foo'] = foo

        transaction.commit()

        conn1 = testing.getConnection(testing.DBNAME)
        dm1 = datamanager.PJDataManager(conn1)

        self.assertEqual(dm1.root['foo'].name, 'foo-first')

        dm1.root['foo'].name = 'foo-second'

        conn2 = testing.getConnection(testing.DBNAME)
        dm2 = datamanager.PJDataManager(conn2)

        self.assertEqual(dm2.root['foo'].name, 'foo-first')
        del dm2.root['foo']

        #Finish in order 2 - 1
        dm2.tpc_begin(None)
        dm2.commit(None)
        dm2.tpc_vote(None)
        dm2.tpc_finish(None)
        dm1.tpc_begin(None)
        with self.assertRaises(interfaces.ConflictError):
            dm1.commit()

        transaction.abort()

        conn2.close()
        conn1.close()
Exemplo n.º 3
0
 def publish(self):
     """Publish each queued object and empty queue"""
     _return = []
     while self.context:
         try:
             item = self.context.pull()
             transaction.commit()
         except ConflictError: # queue concurrency exception...expected
             if logger.getEffectiveLevel() == logging.DEBUG:
                 logger.exception("ConflictError while publishing queue, " +\
                     "transaction aborted.  This error is an expected " +\
                     "runtime condition and does not necessarily " +\
                     "indicate an application issue")
             transaction.abort()
             """If we plan on reusing this database session we must create a new transaction"""
             self.connection.newTransaction()
             continue # skip to next loop
         # TODO: Add tests for re-queing on publishing errors
         try:
             IPublisher(item).publish()
             _return.append(item)
         except RecoverablePublishingError:
             if item:
                 self.enqueue(item) # add item back into queue for publishing exceptions
             logger.exception("A recoverable publishing error has occured "+\
                              "for queued item %s.  The item will be " +\
                              "added back in the publishing queue." )
     notify(PublisherQueuePublishedEvent(self))
     return _return
Exemplo n.º 4
0
Arquivo: app.py Projeto: dmdm/PySite
 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
Exemplo n.º 5
0
    def test_conflict_del_4(self):
        """Check conflict detection. We modify and delete the same object in
        different transactions, simulating separate processes."""
        foo = Foo('foo-first')
        self.dm.root['foo'] = foo

        transaction.commit()

        conn1 = testing.getConnection(testing.DBNAME)
        dm1 = datamanager.PJDataManager(conn1)
        conn2 = testing.getConnection(testing.DBNAME)
        dm2 = datamanager.PJDataManager(conn2)

        self.assertEqual(dm2.root['foo'].name, 'foo-first')
        del dm2.root['foo']

        self.assertEqual(dm1.root['foo'].name, 'foo-first')
        dm1.root['foo'].name = 'foo-second'

        #Finish in order 1 - 2
        # well, try to... dm1.tpc_finish will block until dm2 is done

        dm1.tpc_begin(None)
        dm2.tpc_begin(None)

        @testing.run_in_thread
        def background_commit():
            dm1.tpc_finish(None)
        with self.assertRaises(interfaces.ConflictError):
            dm2.tpc_finish(None)

        transaction.abort()

        conn2.close()
        conn1.close()
Exemplo n.º 6
0
def process_vc_room_association(plugin, event, vc_room, form, event_vc_room=None, allow_same_room=False):
    # disable autoflush, so that the new event_vc_room does not influence the result
    with db.session.no_autoflush:
        if event_vc_room is None:
            event_vc_room = VCRoomEventAssociation()

        plugin.update_data_association(event, vc_room, event_vc_room, form.data)

        existing = set()
        if event_vc_room.link_object is not None:
            # check whether there is a room-event association already present
            # for the given event, room and plugin
            q = VCRoomEventAssociation.find(
                VCRoomEventAssociation.event_new == event,
                VCRoomEventAssociation.link_object == event_vc_room.link_object,
                _join=VCRoom
            )
            if allow_same_room:
                q = q.filter(VCRoom.id != vc_room.id)
            existing = {x.vc_room for x in q}

    if event_vc_room.link_type != VCRoomLinkType.event and existing:
        transaction.abort()
        flash(_("There is already a VC room attached to '{link_object_title}'.").format(
            link_object_title=resolve_title(event_vc_room.link_object)), 'error')
        return None
    elif event_vc_room.link_type == VCRoomLinkType.event and vc_room in existing:
        transaction.abort()
        flash(_("This {plugin_name} room is already attached to the event.").format(plugin_name=plugin.friendly_name),
              'error')
        return None
    else:
        return event_vc_room
Exemplo n.º 7
0
def main(argv=sys.argv):
    logging.basicConfig()
    log.setLevel(logging.INFO)

    parser = OptionParser(
        description="Generate some statistics about Karl usage",
        usage="%prog [options]",
        )

    parser.add_option('-C', '--config', dest='config', default=None,
        help="Specify a paster config file. Defaults to $CWD/etc/karl.ini")
    options, args = parser.parse_args()

    if len(args):
        parser.error("Command does not take arguments.")

    config = options.config
    if not config:
        config = get_default_config()
    root, closer = open_root(config)

    try:
        gen_stats(root)
    finally:
        transaction.abort()
Exemplo n.º 8
0
def populate_database():
    """Populate the database with some data useful for development."""
    if User.fetch_by(username='******'):
        return

    # Admin user
    admin = User(name='Administrator', password='******',
                 username='******', is_admin=True)
    # Class
    class_ = Class(name='CS32')
    Session.add(class_)
    Session.flush()

    # Project
    project = Project(name='Project 1', class_id=class_.id)
    Session.add(project)
    Session.flush()

    # File verification
    fv = FileVerifier(filename='test.c', min_size=3, min_lines=1,
                      project_id=project.id)

    Session.add_all([admin, fv])
    try:
        transaction.commit()
        print('Admin user created')
    except IntegrityError:
        transaction.abort()
Exemplo n.º 9
0
    def testSimplePopulation(self):
        session = self.db.session
        query = session.query(User)
        rows = query.all()
        self.assertEqual(len(rows), 0)

        session.save(User(id=1, firstname='udo', lastname='juergens'))
        session.save(User(id=2, firstname='heino', lastname='n/a'))
        session.flush()

        rows = query.order_by(query.table.c.id).all()
        self.assertEqual(len(rows), 2)
        row1 = rows[0]
        d = row1.asDict()
        self.assertEqual(
            d, {'firstname': 'udo', 'lastname': 'juergens', 'id': 1})

        # bypass the session machinary
        stmt = sql.select(query.table.columns).order_by('id')
        results = self.db.engine.connect().execute(stmt)
        self.assertEqual(
            results.fetchall(),
            [(1, u'udo', u'juergens'),
             (2, u'heino', u'n/a')]
        )

        # and rollback
        transaction.abort()
        self.db.invalidate()
        results = self.db.connection.execute(stmt)
        self.assertEqual(results.fetchall(), [])
Exemplo n.º 10
0
 def tearDown(self):
     try:
         transaction.abort()
     except AttributeError:
         # Zope 2.7
         get_transaction().abort()
     self.connection.close()
Exemplo n.º 11
0
def setup_db():
    """Crée toutes les tables du modèle dans la BDD."""
    #tmpdir = tempfile.mkdtemp(prefix="tests-vigiconf-")
    #settings["database"]["sqlalchemy_url"] = "sqlite:///%s/vigilo.db" % tmpdir
    transaction.abort()

    # La vue GroupPath dépend de Group et GroupHierarchy.
    # SQLAlchemy ne peut pas détecter correctement la dépendance.
    # On crée le schéma en 2 fois pour contourner ce problème.
    # Idem pour la vue UserSupItem (6 dépendances).
    from vigilo.models.tables.grouppath import GroupPath
    from vigilo.models.tables.usersupitem import UserSupItem
    mapped_tables = metadata.tables.copy()
    del mapped_tables[GroupPath.__tablename__]
    del mapped_tables[UserSupItem.__tablename__]
    metadata.create_all(tables=mapped_tables.itervalues())
    metadata.create_all(tables=[GroupPath.__table__, UserSupItem.__table__])

    DBSession.add(StateName(statename=u'OK', order=1))
    DBSession.add(StateName(statename=u'UNKNOWN', order=2))
    DBSession.add(StateName(statename=u'WARNING', order=3))
    DBSession.add(StateName(statename=u'CRITICAL', order=4))
    DBSession.add(StateName(statename=u'UP', order=1))
    DBSession.add(StateName(statename=u'UNREACHABLE', order=2))
    DBSession.add(StateName(statename=u'DOWN', order=4))
    MapGroup(name=u'Root')
    DBSession.flush()
Exemplo n.º 12
0
    def prescanUpdate(self):
        """Update the factory's view of the world before each scan.

        For the basic BuilderFactory this means ending the transaction
        to ensure that data retrieved is up to date.
        """
        transaction.abort()
Exemplo n.º 13
0
  def _checkExpand(self):
    """Check that expand() would not fail nor do major changes to the subobjects

    Transaction is aborted after 'expand' is called.

    See also SimulationTool._checkExpandAll
    """
    property_dict = {'Applied Rule': ('specialise',),
                     'Simulation Movement': ('delivery', 'quantity')}
    def fillRuleDict():
      rule_dict = {}
      object_list = deque((self,))
      while object_list:
        document = object_list.popleft()
        portal_type = document.getPortalType()
        document_dict = {'portal_type': portal_type}
        for property in property_dict[portal_type]:
          document_dict[property] = document.getProperty(property)
        rule_dict[document.getRelativeUrl()] = document_dict
        object_list += document.objectValues()
      return rule_dict
    initial_rule_dict = fillRuleDict()
    try:
      self.expand("immediate")
    except ConflictError:
      raise
    except Exception:
      msg = ''.join(ExceptionFormatter.format_exception(*sys.exc_info())[1:])
    else:
      final_rule_dict = fillRuleDict()
      msg = "%r != %r" % (initial_rule_dict, final_rule_dict) \
            if initial_rule_dict != final_rule_dict else None
    transaction.abort()
    return msg
Exemplo n.º 14
0
def initialize_sql(engine, admin_user=u'admin_user'):
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    try:
        populate(admin_user)
    except IntegrityError:
        transaction.abort()
Exemplo n.º 15
0
def _teardown_db():
    import transaction
    transaction.abort()
    import sqlahelper
    sqlahelper.get_session().remove()
    from . import models as m
    m.Base.metadata.drop_all()
Exemplo n.º 16
0
    def _process(self):
        room = self._reservation.room
        form = ModifyBookingForm(obj=self._reservation, old_start_date=self._reservation.start_dt.date())
        form.used_equipment.query = room.find_available_vc_equipment()

        if not room.notification_for_assistance and not self._reservation.needs_assistance:
            del form.needs_assistance

        if form.is_submitted() and not form.validate():
            occurrences = {}
            candidates = {}
            conflicts = {}
            pre_conflicts = {}
        else:
            occurrences, candidates = self._get_all_occurrences([room.id], form, reservation_id=self._reservation.id)
            conflicts, pre_conflicts = self._get_all_conflicts(room, form, self._reservation.id)

        if form.validate_on_submit() and not form.submit_check.data:
            try:
                self._reservation.modify(form.data, session.user)
                flash(_(u'Booking updated'), 'success')
            except NoReportError as e:
                transaction.abort()
                return jsonify(success=False, msg=unicode(e))
            return jsonify(success=True, url=self._get_success_url())

        return self._get_view(form=form, room=room, rooms=Room.find_all(), occurrences=occurrences,
                              candidates=candidates, conflicts=conflicts, pre_conflicts=pre_conflicts,
                              start_dt=form.start_dt.data, end_dt=form.end_dt.data, only_conflicts=False,
                              repeat_frequency=form.repeat_frequency.data, repeat_interval=form.repeat_interval.data,
                              reservation=self._reservation,
                              can_override=room.can_be_overridden(session.user)).display()
def update_table_dbnl_ids(doubles=doubles, repo=repo):
    db = repo.db
    session = db.get_session()
    total = len(doubles)
    i = 0
#    doubles = doubles[:10]
    session.query(DBNLIds).delete()
    for dbnl_id in doubles:
        ls = doubles[dbnl_id]
        #ls is a list of lcoal ids
        #find the biography for each item in the list
        i += 1
        print i, 'of', total, '-', dbnl_id, ':', ls
        bios = [repo.get_biography(local_id=id) for id in ls]
        while bios:
            bio = bios[0]
            person = bio.get_person()
            bioport_id = person.bioport_id
            bios = bios[1:]
            for bio1 in bios:
                person1 = bio1.get_person()
                bioport_id1 = person1.bioport_id
                
                id1 = min(bioport_id, bioport_id1)
                id2 = max(bioport_id, bioport_id1)
                print 'add to dbnl_ids',id1, id2
                session.add(DBNLIds(bioport_id1=id1, bioport_id2=id2, source1=bio.source_id, source2=bio1.source_id, dbnl_id=dbnl_id))
                try:
                    transaction.commit()
                except IntegrityError:
                    transaction.abort()
Exemplo n.º 18
0
 def __checkDatabase(self, db_name):
     db = self.neo.getSQLConnection(db_name)
     # wait for the sql transaction to be commited
     def callback(last_try):
         db.commit() # to get a fresh view
         # One revision per object and two for the root, before and after
         (object_number,), = db.query('SELECT count(*) FROM obj')
         return object_number == OBJECT_NUMBER + 2, object_number
     self.neo.expectCondition(callback)
     # no more temporarily objects
     (t_objects,), = db.query('SELECT count(*) FROM tobj')
     self.assertEqual(t_objects, 0)
     # One object more for the root
     query = 'SELECT count(*) FROM (SELECT * FROM obj GROUP BY oid) AS t'
     (objects,), = db.query(query)
     self.assertEqual(objects, OBJECT_NUMBER + 1)
     # Check object content
     db, conn = self.neo.getZODBConnection()
     root = conn.root()
     for i in xrange(OBJECT_NUMBER):
         obj = root[i]
         self.assertEqual(obj.value, i)
     transaction.abort()
     conn.close()
     db.close()
 def tearDown(self):
     transaction.abort()
     if self.jar is not None:
         self.dbclose()
     if self.db is not None:
         self.db.close()
         self.db = None
Exemplo n.º 20
0
 def transformIterable(self, result, encoding):
     if IPromises(self.request):
         transaction.abort()  # apparently safe in IPubBeforeCommitEvent
         return PromiseWorkerStreamIterator(
             IPromises(self.request), self.request, self.request.response)
     else:
         return None
Exemplo n.º 21
0
    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 test_allocation_mode_unique_by_network_check_serialize_called(self):
    """
    Test that on being_requested serialise is being called
    code stolen from testERP5Security:test_MultiplePersonReferenceConcurrentTransaction
    """
    class DummyTestException(Exception):
      pass

    def verify_serialize_call(self):
      # it is checking that anything below computer_module raises exception
      # thanks to this this test do not have to be destructive
      if self.getPortalType() == "Hosting Subscription":
        raise DummyTestException
      else:
        return self.serialize_call()

    self._makeTree()
    self.software_instance.setSlaXml("""<?xml version='1.0' encoding='utf-8'?>
        <instance>
        <parameter id='mode'>unique_by_network</parameter>
        </instance>""")

    from Products.ERP5Type.Base import Base
    Base.serialize_call = Base.serialize
    try:
      Base.serialize = verify_serialize_call
      self.assertRaises(DummyTestException,
        self.software_instance.SoftwareInstance_tryToAllocatePartition)
    finally:
      Base.serialize = Base.serialize_call

    transaction.abort()
Exemplo n.º 23
0
    def testThread(self):
        transaction.abort()
        global thread_error
        thread_error = None

        def target():
            try:
                session = Session()
                metadata.drop_all(engine)
                metadata.create_all(engine)

                query = session.query(User)
                rows = query.all()
                self.assertEqual(len(rows), 0)

                session.add(User(id=1, firstname='udo', lastname='juergens'))
                session.add(User(id=2, firstname='heino', lastname='n/a'))
                session.flush()

                rows = query.order_by(User.id).all()
                self.assertEqual(len(rows), 2)
                row1 = rows[0]
                d = row1.asDict()
                self.assertEqual(d, {'firstname': 'udo', 'lastname': 'juergens', 'id': 1})
            except Exception as err:
                global thread_error
                thread_error = err
            transaction.abort()

        thread = threading.Thread(target=target)
        thread.start()
        thread.join()
        if thread_error is not None:
            raise thread_error  # reraise in current thread
Exemplo n.º 24
0
 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()
Exemplo n.º 25
0
def initialize(context):
    """Initializer called when used as a Zope 2 product."""
    root = Zope2.app()
    sites = root.objectValues("Plone Site")
    version = os.environ.get("EEA_KGS_VERSION", "")
    if not version:
        return

    changed = False
    for site in sites:
        anno = queryAdapter(site, IAnnotations)
        if not anno:
            continue

        if not anno.get("EEA_KGS_VERSION", None):
            anno["EEA_KGS_VERSION"] = OOBTree()
            changed = True

        if not anno["EEA_KGS_VERSION"].get(version, None):
            anno["EEA_KGS_VERSION"][version] = datetime.now()
            changed = True

    if changed:
        transaction.get().note('eea.design: updating EEA_KGS_VERSION')
        try:
            transaction.commit()
        except Exception as err:
            logger.warn("EEA_KGS_VERSION already updated elsewhere: %s", err)
            transaction.abort()
        else:
            logger.info("EEA_KGS_VERSION updated to: %s", version)
Exemplo n.º 26
0
def editModule(self, **kwargs):
      rme = self
      context = self.context
      response = self.request.RESPONSE
      # Perform the import
      try:
          payload = context.REQUEST['BODY']
          if payload:
            kwargs = {'original_file_name':'sword-import-file', 'user_name':getSecurityManager().getUser().getUserName()}
            text, subobjs, meta = doTransform(rme, "sword_to_folder", payload, meta=1, **kwargs)
            #context.plone_log("SWORD Import with id=%s: Transformed metadata and transformed document to cnxml." % (new_id))
            if text:
              rme.manage_delObjects([rme.default_file,])

              rme.invokeFactory('CNXML Document', rme.default_file, file=text, idprefix='zip-')
            makeContent(rme, subobjs)

            # Parse the returned mdml and set attributes up on the ModuleEditor object
            # Add any additional, unmatched, aka uncredited authors
            props = meta['properties']
            rme.updateProperties(props)
            # Make sure the metadata gets into the cnxml
            rme.editMetadata()

          #context.plone_log("SWORD Import with id=%s: Completed." % (new_id))
          response.setStatus('Created')
          return None#state.set(status='SwordImportSuccess', context=rme)

      except OOoImportError, e:
          transaction.abort()
          #context.plone_log("SWORD Import with id=%s: Aborted. There were problems transforming the openoffice or word document." % (new_id))
          message = context.translate("message_could_not_import", {"errormsg":e}, domain="rhaptos",
                                      default="Could not import file. %s" % e)
          response.setStatus('BadRequest')
          return None#state.set(status='SwordImportError', portal_status_message=message)
Exemplo n.º 27
0
def abort_transaction():
    # FIXME: aborting a transaction means it could be commited later on. The
    # transaction should be doom()ed instead, but transaction.doom() is not
    # available on Zope 2.8. We should provide our own doom() implementation
    # which raises an exception on pre-commit-hook, which does exist
    # in Zope 2.8
    transaction.abort()
Exemplo n.º 28
0
def set_snapshot(xmin, snapshot_id):
    global current_xmin_snapshot_id
    if current_xmin_snapshot_id == (xmin, snapshot_id):
        return
    clear_snapshot()
    current_xmin_snapshot_id = (xmin, snapshot_id)

    while True:
        txn = transaction.begin()
        txn.doom()
        if snapshot_id is not None:
            txn.setExtendedInfo('snapshot_id', snapshot_id)
        session = app.registry[DBSESSION]()
        connection = session.connection()
        db_xmin = connection.execute(
            "SELECT txid_snapshot_xmin(txid_current_snapshot());").scalar()
        if db_xmin >= xmin:
            break
        transaction.abort()
        log.info('Waiting for xmin %r to reach %r', db_xmin, xmin)
        time.sleep(0.1)

    registry = app.registry
    request = app.request_factory.blank('/_indexing_pool')
    request.registry = registry
    request.datastore = 'database'
    apply_request_extensions(request)
    request.invoke_subrequest = app.invoke_subrequest
    request.root = app.root_factory(request)
    request._stats = {}
    manager.push({'request': request, 'registry': registry})
Exemplo n.º 29
0
def clear_snapshot(signum=None, frame=None):
    global current_xmin_snapshot_id
    if current_xmin_snapshot_id is None:
        return
    transaction.abort()
    manager.pop()
    current_xmin_snapshot_id = None
Exemplo n.º 30
0
    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()
Exemplo n.º 31
0
    def __call__(self):

        if self.request.form.has_key("submitted"):

            def error(field, message):
                if field:
                    message = "%s: %s" % (field, message)
                self.context.plone_utils.addPortalMessage(message, 'error')
                return self.template()

            form = self.request.form
            contact = self.context

            password = safe_unicode(form.get('password', '')).encode('utf-8')
            username = safe_unicode(form.get('username', '')).encode('utf-8')
            confirm = form.get('confirm', '')
            email = safe_unicode(form.get('email', '')).encode('utf-8')

            if not username:
                return error('username',
                             PMF("Input is required but not given."))

            if not email:
                return error('email', PMF("Input is required but not given."))

            reg_tool = self.context.portal_registration
            properties = self.context.portal_properties.site_properties

            ##            if properties.validate_email:
            ##                password = reg_tool.generatePassword()
            ##            else:
            if password != confirm:
                return error('password', PMF("Passwords do not match."))

            if not password:
                return error('password',
                             PMF("Input is required but not given."))

            if not confirm:
                return error('password', PMF("Passwords do not match."))

            if len(password) < 5:
                return error('password',
                             PMF("Passwords must contain at least 5 letters."))

            try:
                reg_tool.addMember(username,
                                   password,
                                   properties={
                                       'username': username,
                                       'email': email,
                                       'fullname': username
                                   })
            except ValueError, msg:
                return error(None, msg)

            contact.setUsername(username)
            contact.setEmailAddress(email)

            # If we're being created in a Client context, then give
            # the contact an Owner local role on client.
            if contact.aq_parent.portal_type == 'Client':
                contact.aq_parent.manage_setLocalRoles(username, [
                    'Owner',
                ])
                if hasattr(aq_base(contact.aq_parent),
                           'reindexObjectSecurity'):
                    contact.aq_parent.reindexObjectSecurity()

                # add user to Clients group
                group = self.context.portal_groups.getGroupById('Clients')
                group.addMember(username)

            # Additional groups for LabContact users.
            # not required (not available for client Contact)
            if 'groups' in self.request and self.request['groups']:
                groups = self.request['groups']
                if not type(groups) in (list, tuple):
                    groups = [
                        groups,
                    ]
                for group in groups:
                    group = self.portal_groups.getGroupById(group)
                    group.addMember(username)

            contact.reindexObject()

            if properties.validate_email or self.request.get('mail_me', 0):
                try:
                    reg_tool.registeredNotify(username)
                except:
                    import transaction
                    transaction.abort()
                    return error(None, PMF("SMTP server disconnected."))

            message = PMF("Member registered.")
            self.context.plone_utils.addPortalMessage(message, 'info')
            return self.template()
Exemplo n.º 32
0
 def tearDown(self):
     transaction.abort()
     metadata.drop_all(engine)
     orm.clear_mappers()
Exemplo n.º 33
0
 def tearDown(self):
     testing.tearDown()
     transaction.abort()
     Base.metadata.drop_all(self.engine)
Exemplo n.º 34
0
 def abort(self):
     transaction.abort()
Exemplo n.º 35
0
    def tearDown(self):
        from .models.meta import Base

        testing.tearDown()
        transaction.abort()
        Base.metadata.drop_all(self.engine)
Exemplo n.º 36
0
 def tearDown(self):
     self.logger.removeHandler(self.log)
     self.logger.setLevel(self.old_level)
     transaction.abort()
     self.app._p_jar.close()
Exemplo n.º 37
0
class _Worker(object):
    def __init__(self, taskId, configData, delay):
        super(_Worker, self).__init__()

        self._logger = logging.getLogger('worker/%s' % taskId)
        self._taskId = taskId
        self._config = configData
        self._executionDelay = delay
        self._app = None

    def _prepare(self):
        """
        This acts as a second 'constructor', that is executed in the
        context of the thread (due to database reasons)
        """
        self._prepareDB()
        self._dbi.startRequest()
        self._delayed = False

        with self._dbi.transaction():
            schedMod = SchedulerModule.getDBInstance()
            self._task = schedMod.getTaskById(self._taskId)

            # open a logging channel
            self._task.plugLogger(self._logger)

        # XXX: potentially conflict-prone
        with self._dbi.transaction(sync=True):
            self._task.prepare()

    def _prepare_retry(self):
        self._dbi.abort()
        self._dbi.sync()
        flush_after_commit_queue(False)
        GenericMailer.flushQueue(False)
        self._task.plugLogger(self._logger)

    def _prepareDB(self):
        self._dbi = DBMgr.getInstance()

    def _process_task(self):
        with self._dbi.transaction():
            with self._app.app_context():
                fossilize.clearCache()
                self._task.start(self._executionDelay)
                transaction.commit()

    def run(self):
        # Import it here to avoid circular import
        from indico.web.flask.app import make_app
        self._app = make_app(True)

        self._prepare()
        self._logger.info('Running task {}.. (delay: {})'.format(
            self._task.id, self._executionDelay))

        try:
            for i, retry in enumerate(
                    transaction.attempts(self._config.task_max_tries)):
                with retry:
                    self._logger.info('Task attempt #{}'.format(i))
                    if i > 0:
                        self._prepare_retry()
                    try:
                        self._process_task()
                        break
                    except ConflictError:
                        transaction.abort()
                    except ClientDisconnected:
                        self._logger.warning(
                            "Retrying for the {}th time in {} secs..".format(
                                i + 1, i * 10))
                        transaction.abort()
                        time.sleep(i * 10)
                    except TaskDelayed, e:
                        self._logger.info("{} delayed by {} seconds".format(
                            self._task, e.delaySeconds))
                        self._delayed = True
                        self._executionDelay = 0
                        time.sleep(e.delaySeconds)
            flush_after_commit_queue(True)
            GenericMailer.flushQueue(True)

        except Exception as e:
            self._logger.exception("{} failed with exception '{}'".format(
                self._task, e))
            transaction.abort()
Exemplo n.º 38
0
 def tearDown(self):
     transaction.abort()
     bound_metadata1.drop_all()
     bound_metadata2.drop_all()
     orm.clear_mappers()
 def tearDown(self):
     noSecurityManager()
     transaction.abort()
     self.app._p_jar.close()
Exemplo n.º 40
0
 def tearDown(self):
     super(TestSQLiteCache, self).tearDown()
     transaction.abort()
     self.sql.query(SQLPackage).delete()
     transaction.commit()
     self.request._process_finished_callbacks()
Exemplo n.º 41
0
 def tearDown(self):
     transaction.abort()
     self.connection.close()
Exemplo n.º 42
0
 def tearDown(self):
     import transaction
     from shootout.models import DBSession
     transaction.abort()
     DBSession.remove()
     testing.tearDown()
Exemplo n.º 43
0
 def beforeTearDown(self):
   transaction.abort()
Exemplo n.º 44
0
 def get(self, path):
     transaction.abort()
     try:
         return self.traverse_getset(path)
     finally:
         transaction.abort()
Exemplo n.º 45
0
 def wrapper(self, *args, **kwargs):
     self.login_user(username, password)
     transaction.abort()
     func(self, *args, **kwargs)
     self.logout_user()
Exemplo n.º 46
0
    def test_index_git_object_hash(self):
        tx = transaction.begin()
        db = churrodb.ChurroDb(self.churrodb_path)

        db["a"] = IndexedCollection()
        db["a"].init_index()
        db["a"]["_index"]["_a"] = churrodb.GitObjectHashIndex(True)
        db["a"]["_index"]["_b"] = churrodb.GitObjectHashIndex()
        db["a"]["b"] = Dummy("c")
        db["a"]["c"] = Dummy("d")
        db["a"]["d"] = Dummy("e")
        db["a"]["e"] = Dummy("f")

        db.save()

        db = churro.Churro(self.churrodb_path)
        db = db.root()
        coll = db.get("a")

        self.assertEqual("46d49b1a588f3684e0dc9f5ea6426a60512fd89d",
                         coll.idx_find("b")[0])
        self.assertEqual("ab3a9aad770bc930fef6b1fd4eb03ad6d67fd407",
                         coll.idx_find("c")[0])
        self.assertEqual("c6326067e195c781848cdca50797407bdbc6faeb",
                         coll.idx_find("d")[0])
        self.assertEqual("3534c705a203d4ef38e2e4d1b6b6d2a63dd3866d",
                         coll.idx_find("e")[0])
        self.assertEqual(
            "c", coll[coll.idx_find("46d49b1a588f3684e0dc9f5ea6426a60512fd89d")
                      [0]].value)
        self.assertEqual(
            "d", coll[coll.idx_find("ab3a9aad770bc930fef6b1fd4eb03ad6d67fd407")
                      [0]].value)
        self.assertEqual(
            "e", coll[coll.idx_find("c6326067e195c781848cdca50797407bdbc6faeb")
                      [0]].value)
        self.assertEqual(
            "f", coll[coll.idx_find("3534c705a203d4ef38e2e4d1b6b6d2a63dd3866d")
                      [0]].value)
        self.assertEqual(
            coll[coll.idx_find("46d49b1a588f3684e0dc9f5ea6426a60512fd89d")
                 [0]].value, coll[coll["_index"]["_a"].idx_find_first(
                     "46d49b1a588f3684e0dc9f5ea6426a60512fd89d")].value)

        tx = transaction.begin()

        db = churrodb.ChurroDb(self.churrodb_path)
        db["a"]["b"] = Dummy("x")

        db.save()

        db = churro.Churro(self.churrodb_path)
        db = db.root()
        coll = db.get("a")

        self.assertEqual(
            "x", coll[coll.idx_find("a58a8f0b987cbb685ac125a060fb4ad0be7e76a0")
                      [0]].value)

        tx = transaction.begin()

        db = churrodb.ChurroDb(self.churrodb_path)
        db["a"]["f"] = Dummy("d")

        self.assertRaises(churrodb.IndexUpdateError, tx.commit)

        transaction.abort()
Exemplo n.º 47
0
 def rollback():
     print("ROLLING BACK")
     trans.rollback()
     print("ABORTING")
     abort()
Exemplo n.º 48
0
    def test_with_captcha(self):
        """
        Test for captcha: does it show up when it's supposed to? Is it really
        verified?
        """
        self.portal.acl_users._doAddUser('other_user', 'other_user', [], '',
                                    'Other', 'User', '*****@*****.**')
        zperm = self.portal.get_pluggable_item(self.meta_type)['permission']
        p = Permission(zperm, (), self.portal)
        p.setRoles(p.getRoles() + ['Authenticated'])
        transaction.commit()

        self.login_user('other_user', 'other_user')

        self.selenium.open('/portal/info/', True)
        self.selenium.select('typetoadd', 'label=%s' % self.meta_label)
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)

        assert self.selenium.is_element_present(
                '//input[@name="test-captcha-response"]')

        self._fill_add_form()

        # submit with no captcha response
        self.selenium.click("//input[@value='Submit']")
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)
        assert self.selenium.is_text_present("Verification words do not match "
                                             "the ones in the picture.")

        # submit with incorrect captcha response
        self.selenium.type('test-captcha-response', "blah blah")
        self.selenium.click("//input[@value='Submit']")
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)
        assert self.selenium.is_text_present("Verification words do not match "
                                             "the ones in the picture.")

        challenge = self.selenium.get_text(
                '//span[@id="test-captcha-challenge"]')
        response = mock_captcha.solve(challenge)
        self.selenium.type('test-captcha-response', response)

        # submit with proper response
        self.selenium.click("//input[@value='Submit']")
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)
        assert self.selenium.is_text_present('The administrator will analyze')

        transaction.abort()
        self._assert_object_added_properly(self.portal['info'],
                                           submitter='other_user')

        # "skip captcha" permission
        p = Permission('Naaya - Skip Captcha', (), self.portal)
        p.setRoles(p.getRoles() + ['Authenticated'])
        transaction.commit()

        self.selenium.open('/portal/info/', True)
        self.selenium.select('typetoadd', 'label=%s' % self.meta_label)
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)

        assert not self.selenium.is_element_present(
                '//input[@name="test-captcha-response"]')

        self.logout_user()
Exemplo n.º 49
0
class Document(PortalContent, DefaultDublinCoreImpl):
    """A Document - Handles both StructuredText and HTML.
    """

    implements(IMutableDocument, IDocument, IDAVAware)
    __implements__ = (z2IMutableDocument, z2IDocument,
                      PortalContent.__implements__,
                      DefaultDublinCoreImpl.__implements__)

    effective_date = expiration_date = None
    cooked_text = text = text_format = ''
    _size = 0
    _isDiscussable = 1

    _stx_level = 1  # Structured text level

    _last_safety_belt_editor = ''
    _last_safety_belt = ''
    _safety_belt = ''

    security = ClassSecurityInfo()

    def __init__(self, id, title='', description='', text_format='', text=''):
        DefaultDublinCoreImpl.__init__(self)
        self.id = id
        self.title = title
        self.description = description
        self.setFormat(text_format)
        self._edit(text)

    security.declareProtected(ModifyPortalContent, 'manage_edit')
    manage_edit = DTMLFile('zmi_editDocument', _dtmldir)

    security.declareProtected(ModifyPortalContent, 'manage_editDocument')

    def manage_editDocument(self, text, text_format, file='', REQUEST=None):
        """ A ZMI (Zope Management Interface) level editing method """
        Document.edit(self, text_format=text_format, text=text, file=file)
        if REQUEST is not None:
            REQUEST['RESPONSE'].redirect(
                self.absolute_url() + '/manage_edit' +
                '?manage_tabs_message=Document+updated')

    def _edit(self, text):
        """ Edit the Document and cook the body.
        """
        self.text = text
        self._size = len(text)

        text_format = self.text_format
        if text_format == 'html':
            self.cooked_text = text
        elif text_format == 'plain':
            self.cooked_text = html_quote(text).replace('\n', '<br />')
        else:
            self.cooked_text = HTML(text, level=self._stx_level, header=0)

    #
    #   IMutableDocument method
    #

    security.declareProtected(ModifyPortalContent, 'edit')

    def edit(self, text_format, text, file='', safety_belt=''):
        """ Update the document.

        To add webDav support, we need to check if the content is locked, and if
        so return ResourceLockedError if not, call _edit.

        Note that this method expects to be called from a web form, and so
        disables header processing
        """
        self.failIfLocked()
        if not self._safety_belt_update(safety_belt=safety_belt):
            msg = _(u'Intervening changes from elsewhere detected. '
                    u'Please refetch the document and reapply your changes. '
                    u'(You may be able to recover your version using the '
                    u"browser 'back' button, but will have to apply them to "
                    u'a freshly fetched copy.)')
            raise EditingConflict(msg)
        if file and (type(file) is not type('')):
            contents = file.read()
            if contents:
                text = contents
        if html_headcheck(text) and text_format.lower() != 'plain':
            text = bodyfinder(text)
        self.setFormat(text_format)
        self._edit(text)
        self.reindexObject()

    security.declareProtected(ModifyPortalContent, 'setMetadata')

    def setMetadata(self, headers):
        headers['Format'] = self.Format()
        new_subject = keywordsplitter(headers)
        headers['Subject'] = new_subject or self.Subject()
        new_contrib = contributorsplitter(headers)
        headers['Contributors'] = new_contrib or self.Contributors()
        for key, value in self.getMetadataHeaders():
            if not key in headers:
                headers[key] = value
        self._editMetadata(
            title=headers['Title'],
            subject=headers['Subject'],
            description=headers['Description'],
            contributors=headers['Contributors'],
            effective_date=headers['Effective_date'],
            expiration_date=headers['Expiration_date'],
            format=headers['Format'],
            language=headers['Language'],
            rights=headers['Rights'],
        )

    security.declarePrivate('guessFormat')

    def guessFormat(self, text):
        """ Simple stab at guessing the inner format of the text """
        if html_headcheck(text): return 'html'
        else: return 'structured-text'

    security.declarePrivate('handleText')

    def handleText(self, text, format=None, stx_level=None):
        """ Handles the raw text, returning headers, body, format """
        headers = {}
        if not format:
            format = self.guessFormat(text)
        if format == 'html':
            parser = SimpleHTMLParser()
            parser.feed(text)
            headers.update(parser.metatags)
            if parser.title:
                headers['Title'] = parser.title
            body = bodyfinder(text)
        else:
            headers, body = parseHeadersBody(text, headers)
            if stx_level:
                self._stx_level = stx_level
        return headers, body, format

    security.declarePublic('getMetadataHeaders')

    def getMetadataHeaders(self):
        """Return RFC-822-style header spec."""
        hdrlist = DefaultDublinCoreImpl.getMetadataHeaders(self)
        hdrlist.append(('SafetyBelt', self._safety_belt))
        return hdrlist

    security.declarePublic('SafetyBelt')

    def SafetyBelt(self):
        """Return the current safety belt setting.
        For web form hidden button."""
        return self._safety_belt

    security.declarePrivate('isValidSafetyBelt')

    def isValidSafetyBelt(self, safety_belt):
        """Check validity of safety belt.
        """
        if not safety_belt:
            # we have no safety belt value
            return True
        if self._safety_belt is None:
            # the current object has no safety belt (ie - freshly made)
            return True
        if safety_belt == self._safety_belt:
            # the safety belt does match the current one
            return True
        this_user = getSecurityManager().getUser().getId()
        if ((safety_belt == self._last_safety_belt)
                and (this_user == self._last_safety_belt_editor)):
            # safety belt and user match last safety belt and user
            return True
        return False

    security.declarePrivate('updateSafetyBelt')

    def updateSafetyBelt(self, safety_belt):
        """Update safety belt tracking.
        """
        this_user = getSecurityManager().getUser().getId()
        self._last_safety_belt_editor = this_user
        self._last_safety_belt = safety_belt
        self._safety_belt = str(self._p_mtime)

    def _safety_belt_update(self, safety_belt=''):
        """Check validity of safety belt and update tracking if valid.

        Return 0 if safety belt is invalid, 1 otherwise.

        Note that the policy is deliberately lax if no safety belt value is
        present - "you're on your own if you don't use your safety belt".

        When present, either the safety belt token:
         - ... is the same as the current one given out, or
         - ... is the same as the last one given out, and the person doing the
           edit is the same as the last editor."""

        if not self.isValidSafetyBelt(safety_belt):
            return 0
        self.updateSafetyBelt(safety_belt)
        return 1

    ### Content accessor methods

    #
    #   IContentish method
    #

    security.declareProtected(View, 'SearchableText')

    def SearchableText(self):
        """ Used by the catalog for basic full text indexing """
        return "%s %s %s" % (self.Title(), self.Description(),
                             self.EditableBody())

    #
    #   IDocument methods
    #

    security.declareProtected(View, 'CookedBody')

    def CookedBody(self, stx_level=None, setlevel=0):
        """ Get the "cooked" (ready for presentation) form of the text.

        The prepared basic rendering of an object.  For Documents, this
        means pre-rendered structured text, or what was between the
        <BODY> tags of HTML.

        If the format is html, and 'stx_level' is not passed in or is the
        same as the object's current settings, return the cached cooked
        text.  Otherwise, recook.  If we recook and 'setlevel' is true,
        then set the recooked text and stx_level on the object.
        """
        if (self.text_format == 'html' or self.text_format == 'plain'
                or (stx_level is None) or (stx_level == self._stx_level)):
            return self.cooked_text
        else:
            cooked = HTML(self.text, level=stx_level, header=0)
            if setlevel:
                self._stx_level = stx_level
                self.cooked_text = cooked
            return cooked

    security.declareProtected(View, 'EditableBody')

    def EditableBody(self):
        """ Get the "raw" (as edited) form of the text.

        The editable body of text.  This is the raw structured text, or
        in the case of HTML, what was between the <BODY> tags.
        """
        return self.text

    #
    #   IDublinCore method
    #

    security.declareProtected(View, 'Format')

    def Format(self):
        """ Dublin Core Format element - resource format.
        """
        if self.text_format == 'html':
            return 'text/html'
        else:
            return 'text/plain'

    #
    #   IMutableDublinCore method
    #

    security.declareProtected(ModifyPortalContent, 'setFormat')

    def setFormat(self, format):
        """ Set text format and Dublin Core resource format.
        """
        value = str(format)
        old_value = self.text_format

        if value == 'text/html' or value == 'html':
            self.text_format = 'html'
        elif value == 'text/plain':
            if self.text_format not in ('structured-text', 'plain'):
                self.text_format = 'structured-text'
        elif value == 'plain':
            self.text_format = 'plain'
        else:
            self.text_format = 'structured-text'

        # Did the format change? We might need to re-cook the content.
        if value != old_value:
            if html_headcheck(self.text) and value != 'plain':
                self.text = bodyfinder(self.text)

            self._edit(self.text)

    ## FTP handlers
    security.declareProtected(ModifyPortalContent, 'PUT')

    def PUT(self, REQUEST, RESPONSE):
        """ Handle HTTP (and presumably FTP?) PUT requests """
        self.dav__init(REQUEST, RESPONSE)
        self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)

        try:
            self.failIfLocked()
        except ResourceLockedError, msg:
            transaction.abort()
            RESPONSE.setStatus(423)
            return RESPONSE

        body = REQUEST.get('BODY', '')
        if REQUEST.get_header('Content-Type', '') == 'text/html':
            format = 'html'
        else:
            format = None
        headers, body, format = self.handleText(body, format)

        safety_belt = headers.get('SafetyBelt', '')
        if not self._safety_belt_update(safety_belt):
            # XXX Can we get an error msg through?  Should we be raising an
            #     exception, to be handled in the FTP mechanism?  Inquiring
            #     minds...
            transaction.abort()
            RESPONSE.setStatus(450)
            return RESPONSE

        self.setFormat(format)
        self.setMetadata(headers)
        self._edit(body)
        RESPONSE.setStatus(204)
        self.reindexObject()
        return RESPONSE
Exemplo n.º 50
0
    def test_edit_description(self):
        """ An elaborate test that uploads an image in the TinyMCE editor. """
        container = self.portal['info']
        title = "Test Object %s" % self.rnd
        ob_id = self._add_object_plain(container, title=title)
        transaction.commit()
        ob = container[ob_id]
        assert self._get_image_container(ob).objectIds() == []

        self.selenium.open('/portal/info/%s/edit_html' % ob_id, True)
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)

        # 'span.mceIcon.mce_image' is the TinyMCE button for inserting an image
        self.selenium.wait_for_condition("""
            window.jQuery('span.mceIcon.mce_image').length > 0
            """, self._selenium_page_timeout)

        self.selenium.click("//span[@class='mceIcon mce_image']")
        # wait for the iframe to load
        self.selenium.wait_for_condition("""
            (window.jQuery('div.mceMiddle iframe')[0]
             .contentDocument.readyState) == 'complete'
            """, self._selenium_page_timeout)

        img_selector = "//body[@id='tinymce']//p/img"
        #img_selector = "//body[@id='tinymce']//img[@id='fe_img_preview']"
        self.selenium.select_frame('//iframe[@id="description_ifr"]')
        assert not self.selenium.is_element_present(img_selector)
        self.selenium.select_frame('relative=top') # back to main frame

        self.selenium.select_frame('//div[@class="mceMiddle"]//iframe')
        self.selenium.click('//li[@id="computer_tab"]//a')

        img_path = path.join(fixtures_path, 'img.gif')
        self.selenium.type('file', img_path)

        #_wait_for(lambda: self.selenium.is_element_present(
        #                '//body/div/div/img[@id="fe_img_preview"]'))
        #_wait_for(lambda: self.selenium.is_element_present(
        #                '//input[@value="Insert"]'))
        import time; time.sleep(1) # TODO: use some "wait_for" instead of sleep

        self.selenium.click('//input[@value="Insert"]')
        self.selenium.select_frame('relative=top') # back to main frame

        self.selenium.select_frame('//iframe[@id="description_ifr"]')
        _wait_for(lambda: self.selenium.is_element_present(img_selector))
        self.selenium.select_frame('relative=top')
        self.selenium.click("//input[@value='Save changes']")
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)

        transaction.abort()
        p = r'<img[^>]* src="/portal/images/img.gif"'
        assert re.search(p, ob.description) is not None
        assert self._get_image_container(ob).objectIds() == ['img.gif']

        self.selenium.click('link=Back to index')
        self.selenium.wait_for_page_to_load(self._selenium_page_timeout)
        assert self.selenium.is_element_present(
            '//div[@id="middle_port"]//p/img')
        assert self.selenium.is_element_present(
            '//div[@id="middle_port"]//p/img[@src="/portal/images/img.gif"]')

        img_complete = self.selenium.get_eval(
                    "window.jQuery('div#middle_port p img')[0].complete")
        img_src = self.selenium.get_eval(
                    "window.jQuery('div#middle_port p img')[0].src")

        assert img_complete == "true", "Image does not show up in the browser"
        assert img_src.endswith("img.gif"), ("Not the right image, perhaps "
                                             "there are more")
Exemplo n.º 51
0
 def testFlushQueueOnAbort(self):
     self.queue.index('foo')
     abort()
     self.assertEqual(self.queue.getState(), [])
     self.assertEqual(self.queue.processed, None)
     self.assertEqual(self.queue.state, 'aborted')
Exemplo n.º 52
0
 def abort(self):
     transaction.abort()
     if self.interaction:
         zope.security.management.endInteraction()
Exemplo n.º 53
0
    def _process(self):
        if not self.plugin.can_manage_vc_rooms(session.user, self.event_new):
            flash(
                _('You are not allowed to modify {} rooms for this event.').
                format(self.plugin.friendly_name), 'error')
            return redirect(url_for('.manage_vc_rooms', self.event_new))

        form = self.plugin.create_form(
            self.event_new,
            existing_vc_room=self.vc_room,
            existing_event_vc_room=self.event_vc_room)

        if form.validate_on_submit():

            self.plugin.update_data_vc_room(self.vc_room, form.data)

            event_vc_room = process_vc_room_association(
                self.plugin,
                self.event_new,
                self.vc_room,
                form,
                event_vc_room=self.event_vc_room,
                allow_same_room=True)
            if not event_vc_room:
                return redirect(url_for('.manage_vc_rooms', self.event_new))

            self.vc_room.modified_dt = now_utc()

            try:
                self.plugin.update_room(self.vc_room, self.event_new)
            except VCRoomNotFoundError as err:
                Logger.get('modules.vc').warning(
                    "VC room {} not found. Setting it as deleted.".format(
                        self.vc_room))
                self.vc_room.status = VCRoomStatus.deleted
                flash(err.message, 'error')
                return redirect(url_for('.manage_vc_rooms', self.event_new))
            except VCRoomError as err:
                if err.field is None:
                    raise
                field = getattr(form, err.field)
                field.errors.append(err.message)
                transaction.abort()
            else:
                # TODO
                # notify_modified(self.vc_room, self.event_new, session.user)

                flash(
                    _("{plugin_name} room '{room.name}' updated").format(
                        plugin_name=self.plugin.friendly_name,
                        room=self.vc_room), 'success')
                return jsonify_data(flash=False)

        form_html = self.plugin.render_form(plugin=self.plugin,
                                            event=self.event_new,
                                            form=form,
                                            existing_vc_room=self.vc_room,
                                            skip_fields=form.skip_fields
                                            | {'name'})

        return jsonify(html=form_html, js=_pop_injected_js())
 def tearDown(self):
     import transaction
     from AccessControl.SecurityManagement import noSecurityManager
     noSecurityManager()
     transaction.abort()
Exemplo n.º 55
0
def commit_transaction():
    try:
        transaction.commit()
    except Exception as e:
        log.critical(e)
        transaction.abort()
Exemplo n.º 56
0
           break
    notify(ZenDMDStartedEvent())
    if opts.script:
        if not os.path.exists(opts.script):
            print "Unable to open script file '%s' -- exiting" % opts.script
            sys.exit(1)
        # copy globals() to temporary dict
        allVars = dict(globals().iteritems())
        allVars.update(vars)
        execfile(opts.script, allVars)
        if opts.commit:
            from transaction import commit
            commit()
        else:
            try:
                transaction.abort()
            except:
                pass
        sys.exit(0)

    audit.audit('Shell.Script.Run')
    _banner = ("Welcome to the Zenoss dmd command shell!\n"
             "'dmd' is bound to the DataRoot. 'zhelp()' to get a list of "
             "commands.")
    try:
        if IPShellEmbed:
            sys.argv[1:] = args
            ipshell = IPShellEmbed(banner=_banner)
            ipshell(local_ns=vars)
        else:
            if readline is not None:
Exemplo n.º 57
0
 def tearDown(self):
     transaction.abort()
     self.connection.close()
     noSecurityManager()
     setSecurityPolicy(self._oldPolicy)
Exemplo n.º 58
0
def rollback_transaction():
    transaction.abort()
Exemplo n.º 59
0
    def upgrade(self, REQUEST=None, dry_run=None, swallow_errors=True):
        # Perform the upgrade.
        setup = getToolByName(self, 'portal_setup')

        # This sets the profile version if it wasn't set yet
        version = self.getInstanceVersion()
        upgrades = setup.listUpgrades(_DEFAULT_PROFILE)
        steps = []
        for u in upgrades:
            if isinstance(u, list):
                steps.extend(u)
            else:
                steps.append(u)

        try:
            stream = StringIO()
            handler = logging.StreamHandler(stream)
            handler.setLevel(logging.DEBUG)
            logger.addHandler(handler)
            gslogger = logging.getLogger('GenericSetup')
            gslogger.addHandler(handler)

            if dry_run:
                logger.info("Dry run selected.")

            logger.info("Starting the migration from version: %s" % version)

            for step in steps:
                try:
                    step['step'].doStep(setup)
                    setup.setLastVersionForProfile(_DEFAULT_PROFILE,
                                                   step['dest'])
                    logger.info("Ran upgrade step: %s" % step['title'])
                except (ConflictError, KeyboardInterrupt):
                    raise
                except:
                    logger.error("Upgrade aborted. Error:\n", exc_info=True)

                    if not swallow_errors:
                        raise
                    else:
                        # abort transaction to safe the zodb
                        transaction.abort()
                        break

            logger.info("End of upgrade path, main migration has finished.")

            if self.needUpgrading():
                logger.error("The upgrade path did NOT reach current version.")
                logger.error("Migration has failed")
            else:
                logger.info("Starting upgrade of core addons.")
                ADDON_LIST.upgrade_all(self)
                logger.info("Done upgrading core addons.")

                # do this once all the changes have been done
                if self.needRecatalog():
                    logger.info("Recatalog needed. This may take a while...")
                    try:
                        catalog = self.portal_catalog
                        # Reduce threshold for the reindex run
                        old_threshold = catalog.threshold
                        pg_threshold = getattr(catalog, 'pgthreshold', 0)
                        catalog.pgthreshold = 300
                        catalog.threshold = 2000
                        catalog.refreshCatalog(clear=1)
                        catalog.threshold = old_threshold
                        catalog.pgthreshold = pg_threshold
                        self._needRecatalog = 0
                    except (ConflictError, KeyboardInterrupt):
                        raise
                    except:
                        logger.error(
                            "Exception was thrown while cataloging:"
                            "\n",
                            exc_info=True)
                        if not swallow_errors:
                            raise

                if self.needUpdateRole():
                    logger.info("Role update needed. This may take a while...")
                    try:
                        self.portal_workflow.updateRoleMappings()
                        self._needUpdateRole = 0
                    except (ConflictError, KeyboardInterrupt):
                        raise
                    except:
                        logger.error(
                            "Exception was thrown while updating "
                            "role mappings",
                            exc_info=True)
                        if not swallow_errors:
                            raise
                logger.info("Your Plone instance is now up-to-date.")

            if dry_run:
                logger.info("Dry run selected, transaction aborted")
                transaction.abort()

            return stream.getvalue()

        finally:
            logger.removeHandler(handler)
            gslogger.removeHandler(handler)
Exemplo n.º 60
0
def run_in_transaction():
    transaction.begin()
    yield
    transaction.abort()