Exemplo n.º 1
0
Arquivo: base.py Projeto: toirl/ringo
    def load(self, id, db=None, cache="", uuid=False, field=None):
        """Loads the item with id from the database and returns it.

        :id: ID of the item to be loaded
        :db: DB session to load the item
        :cache: Name of the cache region. If empty then no caching is
                done.
        :uuid: If true the given id is a uuid. Default to false
        :field: If given the item can be loaded by an alternative field.
                Default to None
        :returns: Instance of clazz

        """
        if db is None:
            db = DBSession
        q = db.query(self._clazz)
        if cache in regions.keys():
            q = set_relation_caching(q, self._clazz, cache)
            q = q.options(FromCache(cache))
        for relation in self._clazz._sql_eager_loads:
            q = q.options(joinedload(relation))
        if field:
            return q.filter(getattr(self._clazz, field) == id).one()
        if uuid:
            warnings.warn("Use of 'uuid' is deprecated in load function "
                          "and will be removed in the future. Use "
                          "field='uuid' instead", DeprecationWarning)
            return q.filter(self._clazz.uuid == id).one()
        else:
            return q.filter(self._clazz.id == id).one()
Exemplo n.º 2
0
    def load(self, id, db=DBSession, cache="", uuid=False, field=None):
        """Loads the item with id from the database and returns it.

        :id: Primary key or field value (if field is given) of the item to
             be loaded
        :db: DB session to load the item
        :cache: Name of the cache region. If empty then no caching is
                done.
        :uuid: (deprecated) If True the given id is a uuid. Defaults to False
        :field: If given, id is expected to be a unique value of the given
                field. Defaults to None
        :returns: Instance of clazz

        """
        q = db.query(self._clazz)
        if cache in regions.keys():
            q = set_relation_caching(q, self._clazz, cache)
            q = q.options(FromCache(cache))
        for relation in self._clazz._sql_eager_loads:
            q = q.options(joinedload(relation))
        if field:
            return q.filter(getattr(self._clazz, field) == id).one()
        if uuid:
            warnings.warn(
                "Use of 'uuid' is deprecated in load function "
                "and will be removed in the future. Use "
                "field='uuid' instead", DeprecationWarning)
            return q.filter(self._clazz.uuid == id).one()
        else:
            item = q.get(id)
            if item:
                return item
            else:
                raise NoResultFound
Exemplo n.º 3
0
    def load(self, id, db=None, cache="", uuid=False, field=None):
        """Loads the item with id from the database and returns it.

        :id: ID of the item to be loaded
        :db: DB session to load the item
        :cache: Name of the cache region. If empty then no caching is
                done.
        :uuid: If true the given id is a uuid. Default to false
        :field: If given the item can be loaded by an alternative field.
                Default to None
        :returns: Instance of clazz

        """
        if db is None:
            db = DBSession
        q = db.query(self._clazz)
        if cache in regions.keys():
            q = set_relation_caching(q, self._clazz, cache)
            q = q.options(FromCache(cache))
        for relation in self._clazz._sql_eager_loads:
            q = q.options(joinedload(relation))
        if field:
            return q.filter(getattr(self._clazz, field) == id).one()
        if uuid:
            warnings.warn(
                "Use of 'uuid' is deprecated in load function "
                "and will be removed in the future. Use "
                "field='uuid' instead", DeprecationWarning)
            return q.filter(self._clazz.uuid == id).one()
        else:
            return q.filter(self._clazz.id == id).one()
Exemplo n.º 4
0
    def __init__(self, clazz, db, cache="", items=None):
        """A List object of. A list can be filterd, and sorted.

        :clazz: Class of items which will be loaded.
        :db: DB connection used to load the items.
        :cache: Name of the cache region. If empty then no caching is
                done.
        :items: Set items of the Baselist. If provided no items will be
        loaded.
        """
        self.clazz = clazz
        self.db = db
        if items is None:
            q = self.db.query(self.clazz)

            if cache in regions.keys():
                q = set_relation_caching(q, self.clazz, cache)
                q = q.options(FromCache(cache))

            # Added support for eager loading of items in the overview:
            # http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#relationship-loading-techniques
            # This also support loading along paths to support
            # releations which are deeper than one level.
            for relation in self.clazz._sql_eager_loads:
                joinedload_path = None
                # Load along path
                for rel in relation.split("."):
                    if joinedload_path is None:
                        joinedload_path = joinedload(rel)
                    else:
                        joinedload_path = joinedload_path.joinedload(rel)
                q = q.options(joinedload_path)
            self.items = q.all()
        else:
            self.items = items
        self.search_filter = []

        self._user = None
        """Internal variable which is set by the `filter_itemlist_for_user`
Exemplo n.º 5
0
Arquivo: base.py Projeto: toirl/ringo
    def __init__(self, clazz, db, cache="", items=None):
        """A List object of. A list can be filterd, and sorted.

        :clazz: Class of items which will be loaded.
        :db: DB connection used to load the items.
        :cache: Name of the cache region. If empty then no caching is
                done.
        :items: Set items of the Baselist. If provided no items will be
        loaded.
        """
        self.clazz = clazz
        self.db = db
        if items is None:
            q = self.db.query(self.clazz)
            if cache in regions.keys():
                q = set_relation_caching(q, self.clazz, cache)
                q = q.options(FromCache(cache))
            for relation in self.clazz._sql_eager_loads:
                q = q.options(joinedload(relation))
            self.items = q.all()
        else:
            self.items = items
        self.search_filter = []