setup profile edition
This commit is contained in:
@@ -5,37 +5,55 @@ from aiogram_dialog.widgets.text import Const, Format
|
|||||||
|
|
||||||
from app.bot.dialogs.widgets.getters import user_getter
|
from app.bot.dialogs.widgets.getters import user_getter
|
||||||
|
|
||||||
|
from .getters import phone_getter
|
||||||
|
from .handlers import (
|
||||||
|
name_type_factory,
|
||||||
|
on_error,
|
||||||
|
phone_type_factory,
|
||||||
|
update_user_name,
|
||||||
|
update_user_phone,
|
||||||
|
)
|
||||||
from .states import ProfileSG
|
from .states import ProfileSG
|
||||||
|
|
||||||
profile_dialog = Dialog(
|
profile_dialog = Dialog(
|
||||||
Window(
|
Window(
|
||||||
Const("*Профиль*"),
|
Const("*Профиль*\n"),
|
||||||
Format("Имя: {user.fullname}"),
|
Format("*Имя*: {user.fullname}"),
|
||||||
Format("Телефон: {user.phone}"),
|
Format("*Телефон*: {phone}"),
|
||||||
SwitchTo(
|
SwitchTo(
|
||||||
Const("изменить имя"),
|
Const("✏️ изменить имя"),
|
||||||
id="change_name",
|
id="change_name",
|
||||||
state=ProfileSG.change_name,
|
state=ProfileSG.change_name,
|
||||||
),
|
),
|
||||||
SwitchTo(
|
SwitchTo(
|
||||||
Const("изменить телефон"),
|
Const("📞 изменить телефон"),
|
||||||
id="change_phone",
|
id="change_phone",
|
||||||
state=ProfileSG.change_phone,
|
state=ProfileSG.change_phone,
|
||||||
),
|
),
|
||||||
Cancel(Const("назад")),
|
Cancel(Const("◀️ назад")),
|
||||||
state=ProfileSG.profile,
|
state=ProfileSG.profile,
|
||||||
getter=user_getter,
|
getter=[user_getter, phone_getter],
|
||||||
),
|
),
|
||||||
Window(
|
Window(
|
||||||
Const("Введите имя"),
|
Const("Введите имя"),
|
||||||
SwitchTo(Const("отмена"), id="go_profile", state=ProfileSG.profile),
|
SwitchTo(Const("отмена"), id="go_profile", state=ProfileSG.profile),
|
||||||
TextInput(id="name_input"),
|
TextInput(
|
||||||
|
id="name_input",
|
||||||
|
type_factory=name_type_factory,
|
||||||
|
on_success=update_user_name,
|
||||||
|
on_error=on_error,
|
||||||
|
),
|
||||||
state=ProfileSG.change_name,
|
state=ProfileSG.change_name,
|
||||||
),
|
),
|
||||||
Window(
|
Window(
|
||||||
Const("Введите телефон"),
|
Const("Введите телефон"),
|
||||||
SwitchTo(Const("отмена"), id="go_profile", state=ProfileSG.profile),
|
SwitchTo(Const("отмена"), id="go_profile", state=ProfileSG.profile),
|
||||||
TextInput(id="phone_input"),
|
TextInput(
|
||||||
|
id="phone_input",
|
||||||
|
type_factory=phone_type_factory,
|
||||||
|
on_success=update_user_phone,
|
||||||
|
on_error=on_error,
|
||||||
|
),
|
||||||
state=ProfileSG.change_phone,
|
state=ProfileSG.change_phone,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
8
app/bot/dialogs/flows/profile/getters.py
Normal file
8
app/bot/dialogs/flows/profile/getters.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from aiogram.types import User
|
||||||
|
from aiogram_dialog import DialogManager
|
||||||
|
|
||||||
|
from app.infrastructure.database.models import User as UserModel
|
||||||
|
|
||||||
|
|
||||||
|
async def phone_getter(dialog_manager: DialogManager, event_from_user: User, user: UserModel, **kwargs):
|
||||||
|
return {"phone": f"\+{user.phone}" if user.phone else ""}
|
||||||
51
app/bot/dialogs/flows/profile/handlers.py
Normal file
51
app/bot/dialogs/flows/profile/handlers.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from aiogram.types import Message
|
||||||
|
from aiogram_dialog import DialogManager
|
||||||
|
from aiogram_dialog.widgets.input import ManagedTextInput
|
||||||
|
|
||||||
|
from app.infrastructure.database.crud import update_user
|
||||||
|
|
||||||
|
from .states import ProfileSG
|
||||||
|
|
||||||
|
|
||||||
|
def name_type_factory(text: str):
|
||||||
|
if not 2 < len(text) < 20:
|
||||||
|
raise ValueError("Имя должно быть от 2 до 20 символов")
|
||||||
|
|
||||||
|
def phone_type_factory(text: str):
|
||||||
|
if len(re.findall("\d", text)) < 11:
|
||||||
|
raise ValueError("Телефон должен быть в формате \+79991112233")
|
||||||
|
return text
|
||||||
|
|
||||||
|
async def on_error(
|
||||||
|
message: Message,
|
||||||
|
widget: ManagedTextInput,
|
||||||
|
dialog_manager: DialogManager,
|
||||||
|
error: ValueError,
|
||||||
|
):
|
||||||
|
await message.answer(error.args[0])
|
||||||
|
|
||||||
|
|
||||||
|
async def update_user_name(
|
||||||
|
message: Message,
|
||||||
|
widget: ManagedTextInput,
|
||||||
|
dialog_manager: DialogManager,
|
||||||
|
text: str,
|
||||||
|
):
|
||||||
|
session = dialog_manager.middleware_data["session"]
|
||||||
|
await update_user(session, message.from_user.id, fullname=text.strip())
|
||||||
|
await dialog_manager.switch_to(ProfileSG.profile)
|
||||||
|
|
||||||
|
|
||||||
|
async def update_user_phone(
|
||||||
|
message: Message,
|
||||||
|
widget: ManagedTextInput,
|
||||||
|
dialog_manager: DialogManager,
|
||||||
|
text: str,
|
||||||
|
):
|
||||||
|
session = dialog_manager.middleware_data["session"]
|
||||||
|
print(text)
|
||||||
|
text = ''.join(re.findall(r"\d", text))
|
||||||
|
await update_user(session, message.from_user.id, phone=text)
|
||||||
|
await dialog_manager.switch_to(ProfileSG.profile)
|
||||||
@@ -4,6 +4,4 @@ from aiogram.fsm.state import State, StatesGroup
|
|||||||
class ProfileSG(StatesGroup):
|
class ProfileSG(StatesGroup):
|
||||||
profile = State()
|
profile = State()
|
||||||
change_name = State()
|
change_name = State()
|
||||||
input_name = State()
|
|
||||||
change_phone = State()
|
|
||||||
change_phone = State()
|
change_phone = State()
|
||||||
@@ -11,9 +11,9 @@ from .states import StartSG
|
|||||||
start_dialog = Dialog(
|
start_dialog = Dialog(
|
||||||
Window(
|
Window(
|
||||||
Format("Привет, {user.fullname}"),
|
Format("Привет, {user.fullname}"),
|
||||||
Start(Const("события"), id="events", state=EventsSG.events_list),
|
Start(Const("📃 события"), id="events", state=EventsSG.events_list),
|
||||||
Button(Const("создать событие"), id="create_event", when="is_admin"),
|
Button(Const("✏️ создать событие"), id="create_event", when="is_admin"),
|
||||||
Start(Const("профиль"), id="profile", state=ProfileSG.profile),
|
Start(Const("👤 профиль"), id="profile", state=ProfileSG.profile),
|
||||||
getter=[user_getter, is_admin_getter],
|
getter=[user_getter, is_admin_getter],
|
||||||
state=StartSG.start,
|
state=StartSG.start,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -20,3 +20,15 @@ async def create_user(
|
|||||||
session.add(user)
|
session.add(user)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
async def update_user(
|
||||||
|
session: AsyncSession, tg_id: int, fullname: str = "", phone: str = ""
|
||||||
|
) -> None:
|
||||||
|
user = await get_user_by_tg_id(session, tg_id)
|
||||||
|
if fullname:
|
||||||
|
user.fullname = fullname
|
||||||
|
if phone:
|
||||||
|
user.phone = phone
|
||||||
|
await session.commit()
|
||||||
|
return user
|
||||||
|
|||||||
Reference in New Issue
Block a user