Skip to content

Commit a7818bd

Browse files
authored
Merge pull request #525 from Tishka17/feature/global_getter
Add global `getter` to `setup_dialogs`
2 parents bb05af1 + 4ec8bef commit a7818bd

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

docs/widgets/passing_data/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ You have access to the following dynamic data:
1111
* ``start_data`` - data that was passed when the current dialog was started. These data is stored in the aiogram FSM storage.
1212
* ``dialog_data`` - you can use this dict to store dialog-related data. It will be accessible at all windows of current dialog. These data is stored in the aiogram FSM storage.
1313

14-
In addition you can specify getter-functions for dialog and window.
14+
In addition you can specify getter-functions for ``Dialog``, ``Window`` or globally in ``setup_dialogs``.
1515
Getter-function has to return a dictionary.
1616

1717
Library collects all the data above into one dictionary object and passes this object to widgets.
@@ -28,7 +28,7 @@ Result:
2828

2929
.. image:: /resources/passing_data_example.png
3030

31-
In event handlers you don't have access to that dictionary, but you can acess ``event``, ``middleware_data``, ``start_data``, ``dialog_data`` via dialog_manager:
31+
In event handlers you don't have access to that dictionary, but you can access ``event``, ``middleware_data``, ``start_data``, ``dialog_data`` via dialog_manager:
3232

3333
.. literalinclude:: ./data_in_handlers.py
3434

src/aiogram_dialog/manager/manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
EVENT_SIMULATED,
4545
STACK_KEY,
4646
STORAGE_KEY,
47+
DataGetter,
4748
FakeChat,
4849
FakeUser,
4950
)
@@ -77,6 +78,7 @@ def __init__(
7778
registry: DialogRegistryProtocol,
7879
router: Router,
7980
data: dict,
81+
getter: DataGetter | None,
8082
):
8183
self.disabled = False
8284
self.message_manager = message_manager
@@ -86,6 +88,7 @@ def __init__(
8688
self._show_mode: ShowMode = ShowMode.AUTO
8789
self._registry = registry
8890
self._router = router
91+
self._getter = getter
8992

9093
@property
9194
def show_mode(self) -> ShowMode:
@@ -126,11 +129,16 @@ def check_disabled(self):
126129

127130
async def load_data(self) -> dict:
128131
context = self.current_context()
132+
if self._getter:
133+
data = await self._getter(**self.middleware_data)
134+
else:
135+
data = {}
129136
return {
130137
"dialog_data": context.dialog_data,
131138
"start_data": context.start_data,
132139
"middleware_data": self._data,
133140
"event": self.event,
141+
**data,
134142
}
135143

136144
def is_preview(self) -> bool:

src/aiogram_dialog/manager/manager_factory.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from aiogram import Router
22

33
from aiogram_dialog.api.entities import ChatEvent
4-
from aiogram_dialog.api.internal import DialogManagerFactory
4+
from aiogram_dialog.api.internal import DataGetter, DialogManagerFactory
55
from aiogram_dialog.api.protocols import (
66
DialogManager,
77
DialogRegistryProtocol,
@@ -16,9 +16,11 @@ def __init__(
1616
self,
1717
message_manager: MessageManagerProtocol,
1818
media_id_storage: MediaIdStorageProtocol,
19+
getter: DataGetter | None,
1920
) -> None:
2021
self.message_manager = message_manager
2122
self.media_id_storage = media_id_storage
23+
self.getter = getter
2224

2325
def __call__(
2426
self, event: ChatEvent, data: dict,
@@ -32,4 +34,5 @@ def __call__(
3234
media_id_storage=self.media_id_storage,
3335
registry=registry,
3436
router=router,
37+
getter=self.getter,
3538
)

src/aiogram_dialog/setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from aiogram_dialog.api.entities import DIALOG_EVENT_NAME
1010
from aiogram_dialog.api.exceptions import UnregisteredDialogError
11-
from aiogram_dialog.api.internal import DialogManagerFactory
11+
from aiogram_dialog.api.internal import DataGetter, DialogManagerFactory
1212
from aiogram_dialog.api.protocols import (
1313
BgManagerFactory,
1414
DialogProtocol,
@@ -174,6 +174,7 @@ def _prepare_dialog_manager_factory(
174174
dialog_manager_factory: DialogManagerFactory | None,
175175
message_manager: MessageManagerProtocol | None,
176176
media_id_storage: MediaIdStorageProtocol | None,
177+
getter: DataGetter | None,
177178
) -> DialogManagerFactory:
178179
if dialog_manager_factory is not None:
179180
return dialog_manager_factory
@@ -184,6 +185,7 @@ def _prepare_dialog_manager_factory(
184185
return DefaultManagerFactory(
185186
message_manager=message_manager,
186187
media_id_storage=media_id_storage,
188+
getter=getter,
187189
)
188190

189191

@@ -224,6 +226,7 @@ def setup_dialogs(
224226
media_id_storage: MediaIdStorageProtocol | None = None,
225227
stack_access_validator: StackAccessValidator | None = None,
226228
events_isolation: BaseEventIsolation | None = None,
229+
getter: DataGetter | None = None,
227230
) -> BgManagerFactory:
228231
_setup_event_observer(router)
229232
_register_event_handler(router, handle_update)
@@ -233,6 +236,7 @@ def setup_dialogs(
233236
dialog_manager_factory=dialog_manager_factory,
234237
message_manager=message_manager,
235238
media_id_storage=media_id_storage,
239+
getter=getter,
236240
)
237241
stack_access_validator = _prepare_stack_access_validator(
238242
stack_access_validator,

0 commit comments

Comments
 (0)