Ejemplo n.º 1
0
def get_product_data(gtin):
    db = ORM()

    product = db.get_cache(gtin)

    if product:
        return product[0]

    prod = product_data(gtin)
    prod = prod.get('products', [])
    if not prod:
        print(f'There is not products with gtin {gtin}')
        return None
    descr = prod[0].get('description')
    first_word_of_descr = descr.split(' ')[0]
    tpnc = prod[0].get('tpnc')

    offset = 0
    glosery = grocery_search(descr, offset)
    product = get_necessary_data_from_grocery_search(glosery, tpnc)

    while not product and offset < 10:
        offset += 1
        glosery = grocery_search(first_word_of_descr, offset)
        product = get_necessary_data_from_grocery_search(glosery, tpnc)

    if product:
        db.add_cache(gtin, **product)

    return product
Ejemplo n.º 2
0
def extract_stores_from_results(stores: Dict,
                                filter_town: Optional[str] = 'Budapest'):
    """
    The function is used only one time to fill `Shop` table by values.
    :param stores: Result of store_location() function works
    :param filter_town: A town name to filter stores for
    :return: Nothing, just placed target stores to the DB
    """
    all_stores = stores.get('results', [])
    target_stores = []
    for store in all_stores:
        store_data = store.get('location', {})
        coords = store_data.get('geo', {}).get('coordinates', {})
        address = store_data.get('contact', {}).get('address')
        if filter_town and filter_town not in address.get('town'):
            continue
        street = address.get('lines', [{}])[0].get('text')
        target_stores.append({
            '_id':
            store_data.get('id'),
            'address':
            street,
            'lon':
            coords.get('longitude'),
            'lat':
            coords.get('latitude'),
            '_type':
            store_data.get('classification', {}).get('type'),
            'name':
            store_data.get('name')
        })

    orm = ORM()
    for store in target_stores:
        orm.add_shop(**store)
Ejemplo n.º 3
0
def add():
    data = request.get_json()
    gtin = data['gtin']
    quantity = data['quantity']
    shop_id = data['shop_id']
    orm = ORM()
    products = orm.get_cache(gtin)
    orm.add_catalog(gtin, quantity, shop_id)
    return jsonify({'quantity': quantity, 'products': products})
Ejemplo n.º 4
0
def fill_cache_table():
    """
    The function is used only for filling cache table
    """
    products = []
    for query in ['bread', 'milk', 'rice']:
        grocery = grocery_search(query)
        products += get_all_products_from_grocery_search(grocery)

    orm = ORM()
    for product in products:
        orm.add_cache(**product)
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def barcode_processing():
    data = request.get_json()
    gtin = data['gtin']
    shop_id = data['shop_id']
    product = api.get_product_data(gtin)

    how_much_will_be_waist = ml.predict(product)
    # TODO: Send how_much_will_be_waist to mobile

    orm = ORM()
    products = orm.get_cache(gtin)
    orm.add_catalog(gtin, how_much_will_be_waist, shop_id)
    return jsonify({
        'how_much_will_be_waist': how_much_will_be_waist,
        'products': products
    })
Ejemplo n.º 7
0
def fill_catalog_table():
    orm = ORM()

    caches = orm.get_caches()
    shops = orm.get_shops()

    for shop in shops:
        for items in range(1, random.randrange(3, 8)):
            for index in range(random.randrange(0, len(caches))):
                gtin = caches[index].get('gtin')
                orm.add_catalog(gtin, random.randint(1, 6), shop.get('id'))
Ejemplo n.º 8
0
def get_by_shop(shop_id: str):
    orm = ORM()
    return jsonify(orm.get_catalog_by_shop(shop_id))
Ejemplo n.º 9
0
def get_all():
    orm = ORM()
    return jsonify(orm.get_catalog_all())
Ejemplo n.º 10
0
def get_shop_list() -> List[Dict]:
    orm = ORM()
    shops = orm.get_shops()
    return shops