Add None type to project fields that don't exist

This commit is contained in:
LoRiot 2022-10-22 15:22:44 +03:00
parent ad3f5f5e43
commit 5e6ea7be27

View file

@ -3,7 +3,6 @@ import re
from aiogram import types from aiogram import types
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from asyncpg import BitString
from states.project import ProjectStates from states.project import ProjectStates
from services.repositories import Repos, ProjectRepo from services.repositories import Repos, ProjectRepo
@ -33,17 +32,17 @@ async def create_project(
message: types.Message, repo: Repos, state: FSMContext message: types.Message, repo: Repos, state: FSMContext
): ):
await state.reset_state() await state.reset_state()
field_patterns = { field_types = {
"Название проекта": str, "Описание": str, "Контакты": str, "Название проекта": str, "Описание": str, "Контакты": str,
"Кол-во участников": int, "Статус": BitString, "Теги": str, "Кол-во участников": int, "Статус": int, "Теги": str,
"Категория": str "Категория": str
} }
field_values = [] field_values = {name: None for name in field_types}
for pattern, field_type in field_patterns.items(): for name, data_type in field_types.items():
try: try:
field_values.append( field_values[name] = data_type(
field_type(re.findall(rf"(?<={pattern}: ).*", message.text)[0]) re.findall(rf"(?<={name}: ).*", message.text)[0]
) )
except IndexError: except IndexError:
pass pass
@ -51,5 +50,5 @@ async def create_project(
logger.debug(field_values) logger.debug(field_values)
await repo.get_repo(ProjectRepo).add( await repo.get_repo(ProjectRepo).add(
*field_values, user_id=message.from_user.id *field_values.values(), user_id=message.from_user.id
) )