Spaces:
Runtime error
Runtime error
from aiogram import Router, F | |
from aiogram.types import Message, CallbackQuery | |
from aiogram.fsm.context import FSMContext | |
from app.database import requests as rq | |
from app.keyboards import user_keyboards as kb | |
from app.states import TestStates | |
from app.database.requests import check_user_registered | |
router = Router() | |
async def show_available_tests(message: Message): | |
registration_status = await check_user_registered(message.from_user.id) | |
if registration_status: | |
await message.answer( | |
"Выберите тест:", | |
reply_markup=await kb.get_tests_keyboard() | |
) | |
if not registration_status: | |
await message.answer( | |
"Зарегистрируйтесь, чтобы сохранить Ваши результаты и получить персональные рекомендации!", | |
reply_markup=kb.user_back | |
) | |
async def start_test(callback: CallbackQuery, state: FSMContext): | |
test_id = callback.data.split("_")[2] | |
test_data = await rq.start_test_attempt( | |
callback.from_user.id, | |
test_id | |
) | |
if not test_data: | |
await callback.message.answer("Тест недоступен") | |
return | |
await state.set_state(TestStates.answering) | |
await state.update_data( | |
attempt_id=test_data["attempt_id"], | |
current_question=1, | |
total_questions=test_data["total_questions"] | |
) | |
await callback.message.answer( | |
f"Вопрос 1 из {test_data['total_questions']}:\n\n" | |
f"{test_data['question'].question_content}", | |
reply_markup=await kb.get_test_question_keyboard(test_data["question"]) | |
) | |
async def process_answer(callback: CallbackQuery, state: FSMContext): | |
_, _, question_id, answer = callback.data.split("_") | |
data = await state.get_data() | |
result = await rq.record_answer(data["attempt_id"], | |
int(question_id), | |
answer | |
) | |
if result.get("completed"): | |
await callback.message.answer( | |
f"Тест завершен!\n\n" | |
f"Ваш результат: {result['result']}" | |
) | |
await state.clear() | |
else: | |
current_q = data["current_question"] + 1 | |
await state.update_data(current_question=current_q) | |
await callback.message.answer( | |
f"Вопрос {current_q} из {data['total_questions']}:\n\n" | |
f"{result['next_question'].question_content}", | |
reply_markup=await kb.get_test_question_keyboard(result["next_question"]) | |
) |