コード例 #1
0
def get_goods_list(shops: List[Dict]) -> List[Dict]:
    orm = ORM()

    goods_list = []
    _cashes = orm.get_caches()
    cashes = {cache.get('gtin'): cache for cache in _cashes}
    _catalog = orm.get_catalog_all()
    catalog = defaultdict(list)
    for product in _catalog:
        catalog[product.get('shop_id')].append(product)

    for shop in shops:
        goods = []
        shop_catalog = catalog.get(shop.get('id'))
        if not shop_catalog:
            shop.update({'count': 0})
            goods_list.append({'id': shop.get('id'), 'goods': goods})
            continue

        categories_dict = defaultdict(list)
        already_checked_products = set()
        for current_product in shop_catalog:
            product = cashes.get(current_product.get('gtin'))
            if not product:
                continue
            qtt = sum([
                _product['quantity'] for _product in shop_catalog
                if _product.get('gtin') == product.get('gtin')
            ])

            if product.get('image') not in already_checked_products:
                categories_dict[product.get('department')].append({
                    'image':
                    product.get('image'),
                    'name':
                    product.get('name'),
                    'description':
                    product.get('description'),
                    'qtt':
                    qtt
                })
                already_checked_products.add(product.get('image'))

        shop.update({'count': len(already_checked_products)})

        for category, products in categories_dict.items():
            goods.append({'category': category, 'products': products})

        goods_list.append({'id': shop.get('id'), 'goods': goods})

    return goods_list
コード例 #2
0
def get_all():
    orm = ORM()
    return jsonify(orm.get_catalog_all())