aboutsummaryrefslogtreecommitdiff
path: root/libs/fsm.py
diff options
context:
space:
mode:
authorTolmachev Igor <me@igorek.dev>2026-03-23 18:07:30 +0300
committerTolmachev Igor <me@igorek.dev>2026-03-23 18:07:30 +0300
commitf7b7e87cffc9dcb2817b070d7a003ac234c96ec3 (patch)
tree47f8af52edcac24f16ae58d0501bd2a4a42c2278 /libs/fsm.py
parent8e034766bb7e2d23f88c5ff1a254126f11a5f412 (diff)
downloadvpn_manager_bot-f7b7e87cffc9dcb2817b070d7a003ac234c96ec3.tar.gz
vpn_manager_bot-f7b7e87cffc9dcb2817b070d7a003ac234c96ec3.zip
Add new_announcement command
Diffstat (limited to 'libs/fsm.py')
-rw-r--r--libs/fsm.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/libs/fsm.py b/libs/fsm.py
new file mode 100644
index 0000000..00ccacb
--- /dev/null
+++ b/libs/fsm.py
@@ -0,0 +1,23 @@
1from contextlib import asynccontextmanager
2
3from aiogram.fsm.context import FSMContext
4from pydantic.main import BaseModel
5from typing_extensions import AsyncGenerator
6
7
8async def set_data(state: FSMContext, model: BaseModel) -> None:
9 await state.set_data(model.model_dump())
10
11
12async def get_data[T: BaseModel](state: FSMContext, model_type: type[T]) -> T:
13 return model_type.model_validate(await state.get_data())
14
15
16@asynccontextmanager
17async def edit_data[T: BaseModel](
18 state: FSMContext,
19 model_type: type[T],
20) -> AsyncGenerator[T]:
21 model = await get_data(state, model_type)
22 yield model
23 await set_data(state, model)