def super_discounts(self, gender=None, amount=10) -> List[Product]: s = Product.search(using=self.es) s = s[:amount] if gender is not None: s = s.filter("term", gender=gender.lower()) s = s.query({ "range": { "price.retail": { "gt": 0.0 } } }).query({"range": { "price.outlet": { "gt": 0.0 } }}) s = s.sort({ "_script": { "type": "number", "script": "(1.0 - (doc['price.outlet'].value / " "doc['price.retail'].value)) * 100", "order": "desc" } }) results = s.execute() if not results: raise NoContentError() else: return results
def __count_products(self, sessionid) -> int: s = Product.search(using=self.es) s = s.filter({"term": {"sessionid.keyword": sessionid}}) s = s[:0] results = s.execute() return results.hits.total
def select_by_item_list(self, item_list) -> Tuple[List[Product], dict]: item_id_list = [item["item_id"] for item in item_list] s = Product.search(using=self.es) s = s[:len(item_list)] s = s.filter("terms", _id=item_id_list) results = s.execute() products_id = [product.meta["id"] for product in results] for p_id in item_id_list: if p_id not in products_id: raise ValidationError("Product id '%s' not registered." % p_id) total = { "outlet": 0.0, "retail": 0.0, "symbol": results[0].price.get_dict()["symbol"] } for item in item_list: product = next(p for p in results if p.meta["id"] == item["item_id"]) amount = item["amount"] total["outlet"] += round(product.price.outlet * amount, 2) total["retail"] += round(product.price.retail * amount, 2) return results, total
def select_by_id(self, id_) -> Product: s = Product.search(using=self.es) s = s[:1] s = s.filter("term", _id=id_) results = s.execute() if not results: raise NotFoundError() else: return results[0]
def test_product_get(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) assert res is not None assert type(res) is Product assert res.meta["id"] == obj.meta["id"] assert res.meta["index"] == "store" assert res.meta["doc_type"] == "products"
def test_product_dict_min(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) obj_dict_min = res.get_dict_min() for key in ["id", "name", "image", "price", "discount"]: assert key in obj_dict_min assert len(obj_dict_min.keys()) == 5 for pkey in ["outlet", "retail", "symbol"]: assert pkey in obj_dict_min["price"] assert len(obj_dict_min["price"].keys()) == 3
def products_count(self) -> int: s = Product.search(using=self.es) s = s.filter("match_all")[:0] s = s.query({ "range": { "price.retail": { "gt": 0.0 } } }).query({"range": { "price.outlet": { "gt": 0.0 } }}) results = s.execute() return results.hits.total
def __search(self, query=None, gender=None, sessionid=None, sessionname=None, brand=None, kind=None, pricerange=None) -> Search: s = Product.search(using=self.es) if query is not None: q = Q("multi_match", query=query, type="most_fields", fields=["kind", "brand", "gender", "name"]) s = s.query(q) if gender is not None: s = s.filter("term", gender=gender.lower()) if sessionid is not None: s = s.filter({"term": {"sessionid.keyword": sessionid}}) if sessionname is not None: s = s.query("match_phrase", sessionname="\"%s\"" % sessionname) if brand is not None: s = s.query("match_phrase", brand="\"%s\"" % brand) if kind is not None: s = s.query("match_phrase", kind="\"%s\"" % kind) if pricerange is not None: s = s.query({ "range": { "price.outlet": { "gte": pricerange["min"], "lte": pricerange["max"] } } }) else: s = s.query({ "range": { "price.retail": { "gt": 0.0 } } }).query({"range": { "price.outlet": { "gt": 0.0 } }}) return s
def test_product_dict(es_object): obj = ProductFactory.create() obj.save(using=es_object.connection) res = Product.get(using=es_object.connection, id=obj.meta["id"]) obj_dict = res.get_dict() for key in [ "id", "name", "link", "kind", "brand", "details", "care", "about", "images", "sessionid", "sessionname", "gender", "price" ]: assert key in obj_dict assert len(obj_dict.keys()) == 13 for pkey in ["outlet", "retail", "symbol"]: assert pkey in obj_dict["price"] assert len(obj_dict["price"].keys()) == 3
def insert(self, user_slug: str, item_list: List[dict]) -> bool: try: order = Order(user_slug=user_slug) self.db_session.add(order) for item in item_list: product = self.db_session.query(Product).filter( Product.es_id == item["item_id"]).one_or_none() if product is None: product = Product(es_id=item["item_id"]) self.db_session.add(product) OrderProduct(order=order, product=product, amount=item["amount"]) self.db_session.commit() return True except DatabaseError: self.db_session.rollback() raise