Add search handler
This commit is contained in:
parent
aaf3330d22
commit
8f77438b7d
21
handlers/projects/__init__.py
Normal file
21
handlers/projects/__init__.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from aiogram import Dispatcher
|
||||
from aiogram.dispatcher.filters import Text
|
||||
|
||||
from states.project import ProjectStates
|
||||
from . import create, search
|
||||
|
||||
|
||||
def register_projects_handlers(dp: Dispatcher):
|
||||
dp.register_message_handler(
|
||||
create.ask_for_project_fields, Text("Создать проект")
|
||||
)
|
||||
dp.register_message_handler(
|
||||
create.create_project, state=ProjectStates.set_fields
|
||||
)
|
||||
|
||||
dp.register_message_handler(
|
||||
search.ask_for_project_name, Text("Найти проект")
|
||||
)
|
||||
dp.register_message_handler(
|
||||
search.search_for_projects, state=ProjectStates.search
|
||||
)
|
|
@ -1,9 +1,8 @@
|
|||
from loguru import logger
|
||||
import re
|
||||
|
||||
from aiogram import types, Dispatcher
|
||||
from aiogram import types
|
||||
from aiogram.dispatcher import FSMContext
|
||||
from aiogram.dispatcher.filters import Text
|
||||
from asyncpg import BitString
|
||||
|
||||
from states.project import ProjectStates
|
||||
|
@ -33,6 +32,7 @@ async def ask_for_project_fields(message: types.Message, state: FSMContext):
|
|||
async def create_project(
|
||||
message: types.Message, repo: Repos, state: FSMContext
|
||||
):
|
||||
await state.reset_state()
|
||||
field_patterns = {
|
||||
"Название проекта": str, "Описание": str, "Контакты": str,
|
||||
"Кол-во участников": int, "Статус": BitString, "Теги": str,
|
||||
|
@ -53,9 +53,3 @@ async def create_project(
|
|||
await repo.get_repo(ProjectRepo).add(
|
||||
*field_values, user_id=message.from_user.id
|
||||
)
|
||||
await state.reset_state()
|
||||
|
||||
|
||||
def register_projects_handlers(dp: Dispatcher):
|
||||
dp.register_message_handler(ask_for_project_fields, Text("Создать проект"))
|
||||
dp.register_message_handler(create_project, state=ProjectStates.set_fields)
|
25
handlers/projects/search.py
Normal file
25
handlers/projects/search.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
from loguru import logger
|
||||
|
||||
from aiogram import types
|
||||
from aiogram.dispatcher import FSMContext
|
||||
|
||||
from states.project import ProjectStates
|
||||
from services.repositories import Repos, ProjectRepo
|
||||
|
||||
|
||||
async def ask_for_project_name(message: types.Message, state: FSMContext):
|
||||
await state.set_state(ProjectStates.search)
|
||||
await message.answer("Введите название проекта")
|
||||
|
||||
|
||||
async def search_for_projects(
|
||||
message: types.Message, state: FSMContext, repo: Repos
|
||||
):
|
||||
await state.reset_state()
|
||||
projects = await repo.get_repo(ProjectRepo).get(message.text)
|
||||
|
||||
for project in projects:
|
||||
await message.answer(
|
||||
f"<b>{project['name']}</b>\n\n {project['description']}",
|
||||
parse_mode="html"
|
||||
)
|
|
@ -3,3 +3,4 @@ from aiogram.dispatcher.filters.state import State, StatesGroup
|
|||
|
||||
class ProjectStates(StatesGroup):
|
||||
set_fields = State()
|
||||
search = State()
|
||||
|
|
Reference in a new issue