Python sqlalchemy.event.contains() Examples

The following are 9 code examples of sqlalchemy.event.contains(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.event , or try the search function .
Example #1
Source File: validator.py    From Flask-Validator with Mozilla Public License 2.0 5 votes vote down vote up
def __create_event(self):
        """ Create an SQLAlchemy event listening the 'set' in a particular column.

        :rtype : object
        """
        if not event.contains(self.field, 'set', self.__validate):
            event.listen(self.field, 'set', self.__validate, retval=True) 
Example #2
Source File: validator.py    From Flask-Validator with Mozilla Public License 2.0 5 votes vote down vote up
def stop(self):
        """ Remove the listener to stop the validation
        """
        if event.contains(self.field, 'set', self.__validate):
            event.remove(self.field, 'set', self.__validate) 
Example #3
Source File: validator.py    From Flask-Validator with Mozilla Public License 2.0 5 votes vote down vote up
def start(self):
        """ Restart the listener
        """
        if not event.contains(self.field, 'set', self.__validate):
            self.__create_event() 
Example #4
Source File: events.py    From sqlalchemy_mptt with MIT License 5 votes vote down vote up
def register_events(self, remove=False):
        for e, h in (
            ('before_insert', self.before_insert),
            ('before_update', self.before_update),
            ('before_delete', self.before_delete),
        ):
            is_event_exist = event.contains(self.base_class, e, h)
            if remove and is_event_exist:
                event.remove(self.base_class, e, h)
            elif not is_event_exist:
                event.listen(self.base_class, e, h, propagate=True)
        return self 
Example #5
Source File: mutable.py    From planespotter with MIT License 5 votes vote down vote up
def _setup_composite_listener():
    def _listen_for_type(mapper, class_):
        for prop in mapper.iterate_properties:
            if (hasattr(prop, 'composite_class') and
                    isinstance(prop.composite_class, type) and
                    issubclass(prop.composite_class, MutableComposite)):
                prop.composite_class._listen_on_attribute(
                    getattr(class_, prop.key), False, class_)
    if not event.contains(Mapper, "mapper_configured", _listen_for_type):
        event.listen(Mapper, 'mapper_configured', _listen_for_type) 
Example #6
Source File: mutable.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _setup_composite_listener():
    def _listen_for_type(mapper, class_):
        for prop in mapper.iterate_properties:
            if (
                hasattr(prop, "composite_class")
                and isinstance(prop.composite_class, type)
                and issubclass(prop.composite_class, MutableComposite)
            ):
                prop.composite_class._listen_on_attribute(
                    getattr(class_, prop.key), False, class_
                )

    if not event.contains(Mapper, "mapper_configured", _listen_for_type):
        event.listen(Mapper, "mapper_configured", _listen_for_type) 
Example #7
Source File: mutable.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _setup_composite_listener():
    def _listen_for_type(mapper, class_):
        for prop in mapper.iterate_properties:
            if (hasattr(prop, 'composite_class') and
                    isinstance(prop.composite_class, type) and
                    issubclass(prop.composite_class, MutableComposite)):
                prop.composite_class._listen_on_attribute(
                    getattr(class_, prop.key), False, class_)
    if not event.contains(Mapper, "mapper_configured", _listen_for_type):
        event.listen(Mapper, 'mapper_configured', _listen_for_type) 
Example #8
Source File: mutable.py    From android_universal with MIT License 5 votes vote down vote up
def _setup_composite_listener():
    def _listen_for_type(mapper, class_):
        for prop in mapper.iterate_properties:
            if (hasattr(prop, 'composite_class') and
                    isinstance(prop.composite_class, type) and
                    issubclass(prop.composite_class, MutableComposite)):
                prop.composite_class._listen_on_attribute(
                    getattr(class_, prop.key), False, class_)
    if not event.contains(Mapper, "mapper_configured", _listen_for_type):
        event.listen(Mapper, 'mapper_configured', _listen_for_type) 
Example #9
Source File: db.py    From seafobj with Apache License 2.0 4 votes vote down vote up
def create_engine_from_conf(config):
    need_connection_pool_fix = True

    if not config.has_section('database'):
        seafile_data_dir = os.environ['SEAFILE_CONF_DIR']
        if seafile_data_dir:
            path = os.path.join(seafile_data_dir, 'seafile.db')
        else:
            logging.warning('SEAFILE_CONF_DIR not set, can not load sqlite database.')
            return None
        db_url = "sqlite:///%s" % path
        need_connection_pool_fix = False
    else:
        backend = config.get('database', 'type')

        if backend == 'mysql':
            if config.has_option('database', 'host'):
                host = config.get('database', 'host').lower()
            else:
                host = 'localhost'

            if config.has_option('database', 'port'):
                port = config.getint('database', 'port')
            else:
                port = 3306
            username = config.get('database', 'user')
            passwd = config.get('database', 'password')
            dbname = config.get('database', 'db_name')
            db_url = "mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8" % (username, quote_plus(passwd), host, port, dbname)
        elif backend == 'oracle':
            if config.has_option('database', 'host'):
                host = config.get('database', 'host').lower()
            else:
                host = 'localhost'

            if config.has_option('database', 'port'):
                port = config.getint('database', 'port')
            else:
                port = 1521
            username = config.get('database', 'username')
            passwd = config.get('database', 'password')
            service_name = config.get('database', 'service_name')
            db_url = "oracle://%s:%s@%s:%s/%s" % (username, quote_plus(passwd),
                    host, port, service_name)
        else:
            raise RuntimeError("Unknown database backend: %s" % backend)

    # Add pool recycle, or mysql connection will be closed by mysqld if idle
    # for too long.
    kwargs = dict(pool_recycle=300, echo=False, echo_pool=False)

    engine = create_engine(db_url, **kwargs)

    if need_connection_pool_fix and not has_event_listener(Pool, 'checkout', ping_connection):
        # We use has_event_listener to double check in case we call create_engine
        # multipe times in the same process.
        add_event_listener(Pool, 'checkout', ping_connection)

    return engine