7
from enum import Enum, auto
8
from pathlib import Path
9
from pybotx_fsm import FSMCollector
10
from app.bot.middlewares.cancel_creation import cancel_creation_middleware
11
from app.bot.middlewares.db_sessions import db_session_middleware
12
from app.interactors.create import CreateFidbackInteractor
13
from app.services.file_storage import FileStorage
14
from app.bot.commands.answer.cancel import get_cancel_keyboard_button
15
from app.bot.constants import constants
16
from schemas.shemas import FidbackCreate
17
from resources import strings
19
collector = HandlerCollector()
21
class CreateFidback(Enum):
22
WAITING_FIDBACK_SUBDIVISION = auto()
23
WAITING_FIDBACK_DIVISION = auto()
24
WAITING_FIDBACK_DISCRIPTION = auto()
25
WAITING_FIDBACK_APPROVE = auto()
27
collector = HandlerCollector()
28
file_storage = FileStorage(Path(constants.FILE_STORAGE_PATH))
29
fsm = FSMCollector(CreateFidback)
31
@collector.command("/создать", description="Оставить обратную связь")
32
async def create_fidback(message: IncomingMessage, bot: Bot) -> None:
33
await message.state.fsm.change_state(
34
CreateFidback.WAITING_FIDBACK_SUBDIVISION,
35
task=FidbackCreate(subdivision=message.body)
37
await bot.answer_message(body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button())
39
@fsm.on(CreateFidback.WAITING_FIDBACK_SUBDIVISION, middlewares=[cancel_creation_middleware])
40
async def waiting_fidback_subdivision(message: IncomingMessage, bot: Bot) -> None:
41
if len(message.body) > 2:
42
await bot.answer_message(
43
body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button()
47
task = message.state.fsm_storage.task
48
task.subdivision = message.body
50
await message.state.fsm.change_state(
51
CreateFidback.WAITING_FIDBACK_DIVISION,
54
await bot.answer_message(
55
body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button()
60
@fsm.on(CreateFidback.WAITING_FIDBACK_DIVISION, middlewares=[cancel_creation_middleware])
61
async def waiting_fidback_division(message: IncomingMessage, bot: Bot) -> None:
62
if len(message.body) > 3:
63
await bot.answer_message(
64
body=strings.FIDBACK_DIV, keyboard=get_cancel_keyboard_button()
68
task = message.state.fsm_storage.task
69
task.division = message.body
71
await message.state.fsm.change_state(
72
CreateFidback.WAITING_FIDBACK_DISCRIPTION, task=task
75
@fsm.on(CreateFidback.WAITING_FIDBACK_DISCRIPTION, middlewares=[db_session_middleware,cancel_creation_middleware])
76
async def waiting_fidback_discription(message: IncomingMessage, bot: Bot) -> None:
77
if len(message.body) > 5:
78
await bot.answer_message(
79
body=strings.FIDBACK_TEXT, keyboard=get_cancel_keyboard_button()
82
task = message.state.fsm_storage.task
83
task.description = message.body
85
await message.state.fsm.change_state(
86
CreateFidback.WAITING_FIDBACK_APPROVE, task=task
89
@fsm.on(CreateFidback.WAITING_FIDBACK_APPROVE, middlewares=[db_session_middleware,cancel_creation_middleware])
90
async def waiting_fidback_approve(message: IncomingMessage, bot: Bot) -> None:
91
task = message.state.fsm_storage.task
93
db_session = message.state.db_session
95
if message.body == strings.CONFIRM_TASK_COMMAND:
96
interactor = CreateFidbackInteractor(db_session)
97
task = await interactor.execute(task)
99
await message.state.fsm.drop_state()
100
await bot.send(message=strings.SUCCESS_FIDBACK
102
elif message.body == strings.CANCEL_TASK_COMMAND:
103
await message.state.fsm.drop_state(
104
CreateFidback.WAITING_FIDBACK_SUBDIVISION,
105
task=FidbackCreate(subdivision=message.body)