From 25d0a78508178b1fcf25075f7efa2dc5a7fd6fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=B7=D0=BB=D0=BE=D0=B2=20=D0=9C=D0=B0=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=20=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D1=8C=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Fri, 6 Oct 2023 12:23:02 +0300 Subject: [PATCH 1/2] Add AbortWidgetClick exception --- src/aiogram_dialog/api/exceptions.py | 3 +++ src/aiogram_dialog/widgets/kbd/checkbox.py | 13 +++++++++---- src/aiogram_dialog/widgets/kbd/select.py | 14 +++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/aiogram_dialog/api/exceptions.py b/src/aiogram_dialog/api/exceptions.py index d878c564..18736c59 100644 --- a/src/aiogram_dialog/api/exceptions.py +++ b/src/aiogram_dialog/api/exceptions.py @@ -54,3 +54,6 @@ class InvalidWidget(DialogsError): class InvalidWidgetType(InvalidWidget): pass + +class AbortWidgetClick(DialogsError): + pass diff --git a/src/aiogram_dialog/widgets/kbd/checkbox.py b/src/aiogram_dialog/widgets/kbd/checkbox.py index 38f8a1cf..e7cbdb8f 100644 --- a/src/aiogram_dialog/widgets/kbd/checkbox.py +++ b/src/aiogram_dialog/widgets/kbd/checkbox.py @@ -4,6 +4,7 @@ from aiogram.types import CallbackQuery, InlineKeyboardButton from aiogram_dialog.api.entities import ChatEvent +from aiogram_dialog.api.exceptions import AbortWidgetClick from aiogram_dialog.api.protocols import DialogManager, DialogProtocol from aiogram_dialog.widgets.common import ManagedWidget, WhenCondition from aiogram_dialog.widgets.text import Case, Text @@ -62,10 +63,14 @@ async def _process_item_callback( ) -> bool: # remove prefix and cast "0" as False, "1" as True checked = data != "0" - await self.on_click.process_event( - callback, self.managed(manager), manager, - ) - await self.set_checked(callback, not checked, manager) + try: + await self.on_click.process_event( + callback, self.managed(manager), manager, + ) + await self.set_checked(callback, not checked, manager) + except AbortWidgetClick: + pass + return True def _is_text_checked( diff --git a/src/aiogram_dialog/widgets/kbd/select.py b/src/aiogram_dialog/widgets/kbd/select.py index c6f1053b..04c3ab24 100644 --- a/src/aiogram_dialog/widgets/kbd/select.py +++ b/src/aiogram_dialog/widgets/kbd/select.py @@ -14,6 +14,7 @@ from aiogram.types import CallbackQuery, InlineKeyboardButton from aiogram_dialog.api.entities import ChatEvent +from aiogram_dialog.api.exceptions import AbortWidgetClick from aiogram_dialog.api.protocols import DialogManager, DialogProtocol from aiogram_dialog.widgets.common import ManagedWidget, WhenCondition from aiogram_dialog.widgets.text import Case, Text @@ -165,11 +166,14 @@ async def _process_click( manager: DialogManager, item_id: str, ): - if self.on_item_click: - await self.on_item_click.process_event( - callback, select, manager, self.type_factory(item_id), - ) - await self._on_click(callback, select, manager, str(item_id)) + try: + if self.on_item_click: + await self.on_item_click.process_event( + callback, select, manager, self.type_factory(item_id), + ) + await self._on_click(callback, select, manager, str(item_id)) + except AbortWidgetClick: + pass @abstractmethod async def _on_click( From 4c69d37409acd1dc06d6ba82845ff08a0ad82ce9 Mon Sep 17 00:00:00 2001 From: Max Kozlov <8101543+MVKozlov@users.noreply.github.com> Date: Thu, 19 Oct 2023 10:28:42 +0300 Subject: [PATCH 2/2] Update exceptions.py Fix E302 expected 2 blank lines, found 1 --- src/aiogram_dialog/api/exceptions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/aiogram_dialog/api/exceptions.py b/src/aiogram_dialog/api/exceptions.py index 18736c59..5aaaa171 100644 --- a/src/aiogram_dialog/api/exceptions.py +++ b/src/aiogram_dialog/api/exceptions.py @@ -55,5 +55,6 @@ class InvalidWidget(DialogsError): class InvalidWidgetType(InvalidWidget): pass + class AbortWidgetClick(DialogsError): pass