Esempio n. 1
0
 def count(self, cls=None):
     '''
         A method to count the number of objects in storage.
     '''
     c = util.convert_class(cls)
     if c is None:
         return len(self.all())
     else:
         return len(self.all(cls))
Esempio n. 2
0
    def get(self, cls, id):
        '''
            A method to retrieve one object.
        '''
        c = util.convert_class(cls)
        key = "{}.{}".format(c, id)
        obj = self.__objects.get(key)

        return obj
Esempio n. 3
0
    def get(self, cls, id):
        '''
            A method to retrieve one object.

            Args:
                cls: string representing the class name.
                id: string representing the object ID.
        '''
        c = util.convert_class(cls, "class")
        obj = self.__session.query(c).filter_by(id=id).first()
        return (obj)
Esempio n. 4
0
    def all(self, cls=None):
        '''
            Return the dictionary
        '''
        new_dict = {}
        cls = util.convert_class(cls)

        if cls is not None:
            for k, v in self.__objects.items():
                if cls == k.split(".")[0]:
                    new_dict[k] = v
            return new_dict
        else:
            return self.__objects
Esempio n. 5
0
    def all(self, cls=None):
        '''
            Query current database session
        '''
        db_dict = {}

        cls = util.convert_class(cls)

        if cls is not None:
            objs = self.__session.query(models.classes[cls]).all()
            for obj in objs:
                key = "{}.{}".format(obj.__class__.__name__, obj.id)
                db_dict[key] = obj
            return db_dict
        else:
            for k, v in models.classes.items():
                if k != "BaseModel":
                    objs = self.__session.query(v).all()
                    if len(objs) > 0:
                        for obj in objs:
                            key = "{}.{}".format(obj.__class__.__name__,
                                                 obj.id)
                            db_dict[key] = obj
            return db_dict