ShopBot/handlers/admin/add_post.py
2022-06-05 15:02:41 +03:00

85 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.types import ContentType
from keyboard.default.admin.main_menu import base_menu, continue_btn, all_right
from keyboard.default.main_menu import back_to_main_menu
from load import bot, dp, messages
from state.post import Post
from utils.database.market import add_item
from utils.helper import download_file
@dp.message_handler(lambda x: x.text == messages.add_post, is_admin=True)
async def cmd_add_post(message: types.Message):
await bot.send_message(message.chat.id, "Укажите имя товара", reply_markup=base_menu())
await Post.name.set()
@dp.message_handler(is_admin=True, state=Post.name)
async def admin_form_name(message: types.Message, state: FSMContext):
await state.update_data(name=message.text)
await bot.send_message(message.chat.id, "Укажите описание товара", reply_markup=base_menu(skip=True))
await Post.description.set()
@dp.message_handler(is_admin=True, state=Post.description)
async def admin_form_desk(message: types.Message, state: FSMContext):
if message.text == messages.skip_message:
await state.update_data(description="")
else:
await state.update_data(description=message.text)
await bot.send_message(message.chat.id, "Укажите цену", reply_markup=base_menu())
await Post.price.set()
@dp.message_handler(is_admin=True, state=Post.price)
async def admin_form_price(message: types.message, state: FSMContext):
if not message.text.replace('.', '').replace(',', '').isdigit():
await Post.price.set()
await state.update_data(price=float(message.text.replace(",", ".")))
await bot.send_message(message.chat.id, "Отправьте фото товара")
await Post.image.set()
@dp.message_handler(is_admin=True, state=Post.image, content_types=[ContentType.PHOTO])
async def admin_form_image(message: types.Message, state: FSMContext):
file_id = message.photo[-1].file_id
file_info = await bot.get_file(file_id)
photo = await download_file(file_info.file_path)
await state.update_data(image=photo.read())
await bot.send_message(
message.chat.id,
"Все данные для поста были заполнены",
reply_markup=continue_btn
)
await state.set_state("finish_post")
@dp.message_handler(lambda x: x.text == messages.continue_, state="finish_post", is_admin=True)
async def finish(message: types.Message, state: FSMContext):
data = await state.get_data()
await bot.send_photo(
message.chat.id,
caption=messages.product_message.format(
name=data["name"],
description=data["description"],
price=data["price"]
),
photo=data["image"],
reply_markup=all_right,
parse_mode='Markdown'
)
@dp.message_handler(lambda x: x.text == messages.all_right_message, is_admin=True, state="finish_post")
async def write_on_db(message: types.Message, state: FSMContext):
data = await state.get_data()
add_item(**data)
await state.finish()
await bot.send_message(
message.chat.id,
"Товар был добавлен",
reply_markup=back_to_main_menu
)