ShopBot/handlers/admin/add_post.py

111 lines
4.0 KiB
Python
Raw Permalink Normal View History

2022-06-04 22:02:51 +03:00
from aiogram import types
from aiogram.dispatcher import FSMContext
2022-06-05 15:02:41 +03:00
from aiogram.types import ContentType
2022-06-04 22:02:51 +03:00
from keyboard.default.admin.main_menu import base_menu, continue_btn, all_right
from keyboard.default.main_menu import back_to_main_menu
2022-06-05 15:02:41 +03:00
from load import bot, dp, messages
2022-06-04 22:02:51 +03:00
from state.post import Post
from utils.database.market import add_item
2022-06-05 15:02:41 +03:00
from utils.helper import download_file
2022-06-04 22:02:51 +03:00
@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):
2022-06-16 00:32:17 +03:00
if len(message.text) > 50:
await bot.send_message(
message.chat.id,
("Имя не может быть больше 50 символов!\n"
"Повторите попытку")
)
await Post.name.set()
return
2022-06-04 22:02:51 +03:00
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):
2022-06-16 00:32:17 +03:00
if len(message.text) > 1000:
await bot.send_message(
message.chat.id,
("Описание не может быть больше 1000 символов!\n"
"Повторите попытку")
)
await Post.description.set()
return
2022-06-04 22:02:51 +03:00
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):
2022-06-16 00:32:17 +03:00
if len(message.text) > 30:
await bot.send_message(
message.chat.id,
("Цена не может быть больше 30 символов!\n"
"Повторите попытку")
)
await Post.price.set()
return
2022-06-05 15:02:41 +03:00
if not message.text.replace('.', '').replace(',', '').isdigit():
2022-06-04 22:02:51 +03:00
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])
2022-06-05 15:02:41 +03:00
async def admin_form_image(message: types.Message, state: FSMContext):
2022-06-04 22:02:51 +03:00
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
)
2022-06-05 15:02:41 +03:00
await state.set_state("finish_post")
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.continue_, state="finish_post", is_admin=True)
2022-06-04 22:02:51 +03:00
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'
)
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.all_right_message, is_admin=True, state="finish_post")
2022-06-04 22:02:51 +03:00
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
)