Esempio n. 1
0
def action_catalog_category(query, data):
    user = get_user(query.from_user.id, session)
    category_id = data['id']

    if category_id is None:
        product_ids = session.query(
            UserProduct.product_id).filter_by(user_id=user.id).distinct()
        products = session.query(Product).filter(
            Product.id.in_(product_ids), Product.catalog_category_ids == '{}')

        for product in products:
            query.message.reply_html(get_product_card(product),
                                     reply_markup=get_product_markup(
                                         user.id, product))

    else:
        rows = get_catalog(session, user.id, data['level'], category_id)

        if len(rows) < 2:
            product_ids = session.query(
                UserProduct.product_id).filter_by(user_id=user.id).distinct()
            products = session.query(Product).filter(
                Product.id.in_(product_ids),
                Product.catalog_category_ids.any(category_id))
            for product in products:
                query.message.reply_html(get_product_card(product),
                                         reply_markup=get_product_markup(
                                             user.id, product))

        else:
            return query.message.reply_html(
                '🗂️ Категории:', reply_markup=get_catalog_markup(rows))
Esempio n. 2
0
def logout(update, context):
    user = get_user(update.message.from_user.id, session)

    session.query(UserProduct).filter_by(user_id=user.id).delete()
    session.delete(user)
    session.commit()

    update.message.reply_html(
        '👋 Все Ваши данные удалены, бот больше не будет Вас беспокоить')
Esempio n. 3
0
def products_list_all(update, context):
    user = get_user(update.message.from_user.id, session)

    product_ids = session.query(
        UserProduct.product_id).filter_by(user_id=user.id).distinct()
    products = session.query(Product).filter(
        Product.id.in_(product_ids)).distinct()

    return products_list(update, context, user, products)
Esempio n. 4
0
def product_list_by_notify(update, context, is_notify):
    user = get_user(update.message.from_user.id, session)

    product_ids = session.query(UserProduct.product_id).filter_by(
        user_id=user.id).join(UserProduct.settings).filter(
            UserProductSettings.is_price_notify == is_notify).distinct()

    products = session.query(Product).filter(
        Product.id.in_(product_ids)).distinct()

    return products_list(update, context, user, products)
Esempio n. 5
0
def products_catalog(update, context):
    user = get_user(update.message.from_user.id, session)
    rows = get_catalog(session, user.id, 1)
    wo_category_count = get_count_wo_category(session, user.id)

    if len(rows) == 0 and wo_category_count == 0:
        return update.message.reply_html('Нет данных')

    if wo_category_count != 0:
        rows.append((None, wo_category_count, 'Прочие'))

    update.message.reply_html('🗂️ Категории:',
                              reply_markup=get_catalog_markup(rows))
Esempio n. 6
0
def action_brand_list(query, data):
    brand_id = data['brand_id']
    user = get_user(query.from_user.id, session)

    products = session.query(Product).filter(
        and_(
            Product.id.in_([
                user_product.product_id for user_product in user.user_products
            ]), Product.brand_id == brand_id))

    for product in products:
        query.message.reply_html(get_product_card(product),
                                 reply_markup=get_product_markup(
                                     user.id, product))
Esempio n. 7
0
def message_add_product(update, context):
    user = get_user(update.message.from_user.id, session)
    matches = re.findall(env.PRODUCT_REGEXP, update.message.text)

    if matches:
        parsed_uri = parse.urlparse(matches[0])
    else:
        return

    domain = parsed_uri.netloc.split('.')[-1]
    code = int("".join(filter(lambda c: c.isdigit(), parsed_uri.path)))

    product = Product.get_product(domain, code, session)
    user_product = session.query(UserProduct).filter_by(
        user_id=user.id, product_id=product.id).first()

    if user_product:
        return update.message.reply_text('Вы уже отслеживаете этот товар')

    if session.query(UserProduct).filter_by(
            user_id=user.id).count() >= user.max_product_count:
        return update.message.reply_text(
            f'Вы отслеживаете максимально допустимое количество товаров: {user.max_product_count})'
        )

    session.add(
        UserProduct(user_id=user.id,
                    product_id=product.id,
                    settings=UserProductSettings(),
                    price=UserProductPrice()))

    product.ref_count += 1
    session.commit()

    # todo move to rmq. make class that returns channel
    connection = pika.BlockingConnection(rmq.get_url_parameters())
    channel = connection.channel()

    channel.basic_publish(exchange=env.RMQ_EXCHANGE,
                          routing_key=env.RMQ_QUEUE_NEW_PRODUCT,
                          body=json.dumps({
                              'user_id': user.id,
                              'product_id': product.id
                          }),
                          properties=pika.BasicProperties(delivery_mode=2))

    return update.message.reply_html(
        f'✅ Товар <a href="{code}">{product.url}</a> добавлен в список.')
Esempio n. 8
0
def action_price_notify(query, data):
    user = get_user(query.from_user.id, session)
    user_product = session.query(UserProduct).filter_by(
        user_id=user.id, product_id=data['product_id']).first()

    if not user_product:
        return

    user_product.settings.is_price_notify = not data['n']
    session.commit()

    if user_product.settings.is_price_notify:
        text = f'🔔 Включены уведомления для {user_product.product.header}'
    else:
        text = f'🔕 Отключены уведомления для {user_product.product.header}'

    return query.message.reply_html(text,
                                    reply_markup=get_product_markup(
                                        user.id, product=user_product.product))
Esempio n. 9
0
def action_prices_history(query, data):
    user = get_user(query.from_user.id, session)

    product = session.query(Product).filter_by(id=data['product_id']).first()
    product_prices = product.prices[:30]

    text = f'📈 Цены на {product.header}\n\n'

    if not product_prices:
        text += 'нет данных'

    for product_price in product_prices:
        price_icon = get_price_icon(product_price.value,
                                    product_price.prev_value)
        price_value = ProductPrice.format_price_value(product_price.value,
                                                      product.domain)

        text += f'{product_price.created_at.date()}  {price_icon} {price_value}\n'

    return query.message.reply_html(text,
                                    reply_markup=get_product_markup(
                                        user.id, product))
Esempio n. 10
0
def action_delete_product(query, data):
    user = get_user(query.from_user.id, session)
    product_id = data['product_id']

    product = session.query(Product).filter_by(id=product_id).first()
    user_product = session.query(UserProduct).filter_by(
        user_id=user.id, product_id=product_id).first()

    if user_product:
        session.query(UserProductSettings).filter_by(
            user_product_id=user_product.id).delete()
        session.query(UserProduct).filter_by(user_id=user.id,
                                             product_id=product_id).delete()
        session.query(UserProductPrice).filter_by(
            user_id=user.id, product_id=product_id).delete()
        product.ref_count -= 1
        session.commit()
    else:
        return query.message.reply_text('❗ Товар не найден')

    return query.message.reply_html(
        f'❌ Товар {product.header} удален из списка')
Esempio n. 11
0
def message_any(update, context):
    if context.user_data.get('action') == 'search':
        context.user_data['action'] = None
        user = get_user(update.message.from_user.id, session)

        products_ids = session.query(
            UserProduct.product_id).filter_by(user_id=user.id).distinct()

        q = '%' + update.message.text + '%'
        products = session.query(Product)\
            .filter(and_(or_(Product.name.ilike(q), Brand.title.ilike(q)), Product.id.in_(products_ids)))\
            .join(Brand, Product.brand_id == Brand.id)\
            .all()

        if not products:
            update.message.reply_text('Совпадений не найдено')

        for product in products:
            update.message.reply_html(get_product_card(product),
                                      reply_markup=get_product_markup(
                                          user.id, product))
    else:
        return menu.menu_item_select(update, context)
Esempio n. 12
0
def brands_list(update, context):
    user = get_user(update.message.from_user.id, session)
    user_product_ids = session.query(
        UserProduct.product_id).filter_by(user_id=user.id).distinct()

    group = session.query(Brand.title, Brand.id, func.count(Product.brand_id)) \
        .join(Product, Product.brand_id == Brand.id) \
        .filter(Product.id.in_(user_product_ids)) \
        .group_by(Brand.title, Brand.id, Product.brand_id) \
        .order_by(Brand.title.asc())

    buttons = []
    for brand_title, brand_id, count in group:
        buttons.append([
            InlineKeyboardButton(f'{brand_title}: {count}',
                                 callback_data=json.dumps({
                                     'action': 'brand_list',
                                     'brand_id': brand_id
                                 }))
        ])

    update.message.reply_html('👓 Бренды:',
                              reply_markup=InlineKeyboardMarkup(buttons))