111 lines
4.0 KiB
Python
111 lines
4.0 KiB
Python
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):
|
||
if len(message.text) > 50:
|
||
await bot.send_message(
|
||
message.chat.id,
|
||
("Имя не может быть больше 50 символов!\n"
|
||
"Повторите попытку")
|
||
)
|
||
await Post.name.set()
|
||
return
|
||
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 len(message.text) > 1000:
|
||
await bot.send_message(
|
||
message.chat.id,
|
||
("Описание не может быть больше 1000 символов!\n"
|
||
"Повторите попытку")
|
||
)
|
||
await Post.description.set()
|
||
return
|
||
|
||
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 len(message.text) > 30:
|
||
await bot.send_message(
|
||
message.chat.id,
|
||
("Цена не может быть больше 30 символов!\n"
|
||
"Повторите попытку")
|
||
)
|
||
await Post.price.set()
|
||
return
|
||
|
||
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
|
||
)
|