Ejemplo n.º 1
0
async def delete_product_callback_handler(query: CallbackQuery,
                                          callback_data: dict):

    product_idx = callback_data['id']
    db.query('DELETE FROM products WHERE idx=?', (product_idx, ))
    await query.answer('Удалено!')
    await query.message.delete()
Ejemplo n.º 2
0
async def process_confirm(message: Message, state: FSMContext):

    enough_money = True  # enough money on the balance sheet
    markup = ReplyKeyboardRemove()

    if enough_money:

        logging.info('Deal was made.')

        async with state.proxy() as data:

            cid = message.chat.id
            products = [
                idx + '=' + str(quantity) for idx, quantity in db.fetchall(
                    '''SELECT idx, quantity FROM cart
            WHERE cid=?''', (cid, ))
            ]  # idx=quantity

            db.query('INSERT INTO orders VALUES (?, ?, ?, ?)',
                     (cid, data['name'], data['address'], ' '.join(products)))

            db.query('DELETE FROM cart WHERE cid=?', (cid, ))

            await message.answer('Ок! Ваш заказ уже в пути 🚀\nИмя: <b>' +
                                 data['name'] + '</b>\nАдрес: <b>' +
                                 data['address'] + '</b>',
                                 reply_markup=markup)
    else:

        await message.answer(
            'У вас недостаточно денег на счете. Пополните баланс!',
            reply_markup=markup)

    await state.finish()
Ejemplo n.º 3
0
async def set_category_title_handler(message: Message, state: FSMContext):

    category = message.text
    idx = md5(category.encode('utf-8')).hexdigest()
    db.query('INSERT INTO categories VALUES (?, ?)', (idx, category))

    await state.finish()
    await process_settings(message)
Ejemplo n.º 4
0
async def add_product_callback_handler(query: CallbackQuery,
                                       callback_data: dict):

    db.query('INSERT INTO cart VALUES (?, ?, 1)',
             (query.message.chat.id, callback_data['id']))

    await query.answer('Товар добавлен в корзину!')
    await query.message.delete()
Ejemplo n.º 5
0
async def delete_category_handler(message: Message, state: FSMContext):

    async with state.proxy() as data:

        if 'category_index' in data.keys():

            idx = data['category_index']

            db.query(
                'DELETE FROM products WHERE tag IN (SELECT title FROM categories WHERE idx=?)',
                (idx, ))
            db.query('DELETE FROM categories WHERE idx=?', (idx, ))

            await message.answer('Готово!', reply_markup=ReplyKeyboardRemove())
            await process_settings(message)
Ejemplo n.º 6
0
async def product_callback_handler(query: CallbackQuery, callback_data: dict,
                                   state: FSMContext):

    idx = callback_data['id']
    action = callback_data['action']

    if 'count' == action:

        async with state.proxy() as data:

            if 'products' not in data.keys():

                await process_cart(query.message, state)

            else:

                await query.answer('Количество - ' + data['products'][idx][2])

    else:

        async with state.proxy() as data:

            if 'products' not in data.keys():

                await process_cart(query.message, state)

            else:

                data['products'][idx][2] += 1 if 'increase' == action else -1
                count_in_cart = data['products'][idx][2]

                if count_in_cart == 0:

                    db.query(
                        '''DELETE FROM cart
                    WHERE cid = ? AND idx = ?''', (query.message.chat.id, idx))

                    await query.message.delete()
                else:

                    db.query(
                        '''UPDATE cart 
                    SET quantity = ? 
                    WHERE cid = ? AND idx = ?''',
                        (count_in_cart, query.message.chat.id, idx))

                    await query.message.edit_reply_markup(
                        product_markup(idx, count_in_cart))
Ejemplo n.º 7
0
async def process_send_answer(message: Message, state: FSMContext):

    async with state.proxy() as data:

        answer = data['answer']
        cid = data['cid']

        question = db.fetchone('SELECT question FROM questions WHERE cid=?',
                               (cid, ))[0]
        db.query('DELETE FROM questions WHERE cid=?', (cid, ))
        text = f'Вопрос: <b>{question}</b>\n\nОтвет: <b>{answer}</b>'

        await message.answer('Отправлено!', reply_markup=ReplyKeyboardRemove())
        await bot.send_message(cid, text)

    await state.finish()
Ejemplo n.º 8
0
async def process_submit(message: Message, state: FSMContext):

    cid = message.chat.id

    if db.fetchone('SELECT * FROM questions WHERE cid=?', (cid,)) == None:

        async with state.proxy() as data:
            db.query('INSERT INTO questions VALUES (?, ?)',
                     (cid, data['question']))

        await message.answer('Отправлено!', reply_markup=ReplyKeyboardRemove())

    else:

        await message.answer('Превышен лимит на количество задаваемых вопросов.', reply_markup=ReplyKeyboardRemove())

    await state.finish()
Ejemplo n.º 9
0
async def process_cart(message: Message, state: FSMContext):

    cart_data = db.fetchall('SELECT * FROM cart WHERE cid=?',
                            (message.chat.id, ))

    if len(cart_data) == 0:

        await message.answer('Ваша корзина пуста.')

    else:

        await bot.send_chat_action(message.chat.id, ChatActions.TYPING)
        async with state.proxy() as data:
            data['products'] = {}

        order_cost = 0

        for _, idx, count_in_cart in cart_data:

            product = db.fetchone('SELECT * FROM products WHERE idx=?',
                                  (idx, ))

            if product == None:

                db.query('DELETE FROM cart WHERE idx=?', (idx, ))

            else:
                _, title, body, image, price, _ = product
                order_cost += price

                async with state.proxy() as data:
                    data['products'][idx] = [title, price, count_in_cart]

                markup = product_markup(idx, count_in_cart)
                text = f'<b>{title}</b>\n\n{body}\n\nЦена: {price}₽.'

                await message.answer_photo(photo=image,
                                           caption=text,
                                           reply_markup=markup)

        if order_cost != 0:
            markup = ReplyKeyboardMarkup(resize_keyboard=True, selective=True)
            markup.add('📦 Оформить заказ')

            await message.answer('Перейти к оформлению?', reply_markup=markup)
Ejemplo n.º 10
0
async def process_confirm(message: Message, state: FSMContext):

    async with state.proxy() as data:

        title = data['title']
        body = data['body']
        image = data['image']
        price = data['price']

        tag = db.fetchone('SELECT title FROM categories WHERE idx=?',
                          (data['category_index'], ))[0]
        idx = md5(' '.join([title, body, price,
                            tag]).encode('utf-8')).hexdigest()

        db.query('INSERT INTO products VALUES (?, ?, ?, ?, ?, ?)',
                 (idx, title, body, image, int(price), tag))

    await state.finish()
    await message.answer('Готово!', reply_markup=ReplyKeyboardRemove())
    await process_settings(message)