from db.mongo import Model class Product(Model): def __init__(self, name, price, available, tags): self.__id = None self.__name = name self.__price = price self.__available = available self.__tags = tags @staticmethod def get_collection(): return Model.get_db().get_collection('products') @staticmethod def dict_to_object(dictionary): pass def to_dict(self): dict = {} if self.__id is not None: dict["_id"] = self.__id if self.__name is not None: dict["name"] = self.__name if self.__price is not None: dict["price"] = self.__price if self.__available is not None: dict["available"] = self.__available if self.__tags is not None: dict["tags"] = self.__tags return dict def store(self): add_document = Product.get_collection().insert_one(self.to_dict()) self.__id = str(add_document.inserted_id) def search_by_tags(self): pass @staticmethod def search(count, tags): print(len(tags)) print(tags) if len(tags) == 0: objects = Product.get_collection().find().limit(count) else: objects = Product.get_collection().find({"tags": {"$all": tags}}).sort([("price", -1)]).limit(count) result = [] for obj in objects: obj["_id"] = str(obj["_id"]) result.append(obj) return result def get_id(self): return self.__id