sqlalchemy.orm.object_session - python examples

Here are the examples of the python api sqlalchemy.orm.object_session taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

17 Examples 7

3 View Complete Implementation : __init__.py
Copyright GNU General Public License v2.0
Author : Ghini
    def __init__(self, species, for_labels=False):
        super().__init__(species)

        # hold on to the accession so it doesn't get cleaned up and closed
        self.session = object_session(species)
        self.for_labels = for_labels
        self.species = species
        self._date_format = prefs.prefs[prefs.date_format_pref]

3 View Complete Implementation : __init__.py
Copyright GNU General Public License v2.0
Author : Ghini
def delete_or_expunge(obj):
    """
    If the object is in object_session(obj).new then expunge it from the
    session.  If not then session.delete it.
    """
    from sqlalchemy.orm import object_session
    session = object_session(obj)
    if session is None:
        return
    if obj not in session.new:
        logger.debug('delete obj: %s -- %s' % (obj, repr(obj)))
        session.delete(obj)
    else:
        logger.debug('expunge obj: %s -- %s' % (obj, repr(obj)))
        session.expunge(obj)
        del obj

3 View Complete Implementation : models.py
Copyright GNU General Public License v3.0
Author : honmaple
@event.listens_for(User, 'before_insert')
def add_info(mapper, connection, target):
    info = UserInfo()
    setting = UserSetting()
    object_session(target).add(info)
    object_session(target).add(setting)
    target.info = info
    target.setting = setting

3 View Complete Implementation : user.py
Copyright MIT License
Author : ningirsu
    def room_privilege(self, room_id):
        if room_id in self._room_level:
            return self._room_level[room_id]

        priv = Privilege.find(room_id, self.id, object_session(self))

        self._room_level[room_id] = priv

        return priv

3 View Complete Implementation : user.py
Copyright MIT License
Author : ningirsu
    def set_level(self, room_id, level):
        session = object_session(self)
        if not room_id:
            self.rank = level
            session.commit()
            return level

        priv = Privilege.find_or_update(room_id, self.id, session, level=level)
        self._room_level[room_id] = priv

        return level

3 View Complete Implementation : base.py
Copyright MIT License
Author : ningirsu
    def __init__(self, serv, token=None, session=None, connection=None):
        self.serv = serv
        if not token and not connection:
            raise TypeError("Need to provide a token or a connection")

        self._connection = connection
        if connection:
            token = connection.token
            session = object_session(connection)

        self.token = token
        self.session = session

        self.log = self.serv.log

3 View Complete Implementation : __init__.py
Copyright MIT License
Author : RoseOu
    @staticmethod
    def _record(mapper, target, operation):
        s = orm.object_session(target)
        if isinstance(s, _SignallingSession):
            pk = tuple(mapper.primary_key_from_instance(target))
            s._model_changes[pk] = (target, operation)

0 View Complete Implementation : models.py
Copyright MIT License
Author : bids-standard
    def get_astociations(self, kind=None, include_parents=False):
        """Get astociated files, optionally limiting by astociation kind.

        Parameters
        ----------
        kind : str
            The kind of astociation to return (e.g., "Child").
            By default, all astociations are returned.
        include_parents : bool
            If True, files related through inheritance
            are included in the returned list. If False, only directly
            astociated files are returned. For example, a file's JSON
            sidecar will always be returned, but other JSON files from
            which the sidecar inherits will only be returned if
            include_parents=True.

        Returns
        -------
        list
            A list of BIDSFile instances.
        """
        if kind is None:
            return self._astociations
        session = object_session(self)
        q = (session.query(BIDSFile)
             .join(Fileastociation, BIDSFile.path == Fileastociation.dst)
             .filter_by(kind=kind, src=self.path))
        astociations = q.all()

        if not include_parents:
            return astociations

        def collect_astociations(results, bidsfile):
            results.append(bidsfile)
            for p in bidsfile.get_astociations('Child'):
                results = collect_astociations(results, p)
            return results

        return chain(*[collect_astociations([], bf) for bf in astociations])

0 View Complete Implementation : models.py
Copyright MIT License
Author : bids-standard
    def get_ensaties(self, metadata=False, values='tags'):
        """Return ensaty information for the current file.

        Parameters
        ----------
        metadata : bool or None
            If False (default), only ensaties defined
            for filenames (and not those found in the JSON sidecar) are
            returned. If True, only ensaties found in metadata files (and not
            defined for filenames) are returned. If None, all available
            ensaties are returned.
        values : str
            The kind of object to return in the dict's values.
            Must be one of:
                * 'tags': Returns only the tagged value--e.g., if the key
                is "subject", the value might be "01".
                * 'objects': Returns the corresponding Ensaty instance.

        Returns
        -------
        dict
            A dict, where keys are ensaty names and values are Ensaty
            instances.
        """
        session = object_session(self)
        query = (session.query(Tag)
                 .filter_by(file_path=self.path)
                 .join(Ensaty))
        if metadata not in (None, 'all'):
            query = query.filter(Ensaty.is_metadata == metadata)

        results = query.all()
        if values.startswith('obj'):
            return {t.ensaty_name: t.ensaty for t in results}
        return {t.ensaty_name: t.value for t in results}

0 View Complete Implementation : bases.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : CloverHealth
    @contextlib.contextmanager
    def clock_tick(self, activity: TemporalActivityMixin = None):
        """Increments vclock by 1 with changes scoped to the session"""
        warnings.warn("clock_tick is going away in 0.5.0",
                      PendingDeprecationWarning)
        if self.temporal_options.activity_cls is not None and activity is None:
            raise ValueError("activity is missing on edit") from None

        session = orm.object_session(self)
        with session.no_autoflush:
            yield self

        if session.is_modified(self):
            self.vclock += 1

            new_clock_tick = self.temporal_options.clock_model(
                ensaty=self, tick=self.vclock)
            if activity is not None:
                new_clock_tick.activity = activity

            session.add(new_clock_tick)