diff --git a/__init__.py b/__init__.py index 86d27a1..090ddb7 100644 --- a/__init__.py +++ b/__init__.py @@ -2,6 +2,11 @@ from .main import QBotApp as QBotApp, Config as Config from .router import Router as Router from .model.bot_entity import BotEntity as BotEntity from .model.bot_enum import BotEnum as BotEnum, EnumMember as EnumMember +from .bot.handlers.context import ( + ContextData as ContextData, + CallbackCommand as CallbackCommand, + CommandContext as CommandContext, +) from .model.descriptors import ( Entity as Entity, EntityField as EntityField, @@ -9,4 +14,6 @@ from .model.descriptors import ( EntityList as EntityList, EntityPermission as EntityPermission, CommandCallbackContext as CommandCallbackContext, + CommandButton as CommandButton, + FieldEditButton as FieldEditButton, ) diff --git a/bot/handlers/common/filtering.py b/bot/handlers/common/filtering.py index f19dc5b..49b7121 100644 --- a/bot/handlers/common/filtering.py +++ b/bot/handlers/common/filtering.py @@ -10,13 +10,20 @@ def add_filter_controls( keyboard_builder: InlineKeyboardBuilder, entity_descriptor: EntityDescriptor, filter: str = None, + filtering_fields: list[str] = None, page: int = 1, ): - field_name_descriptor = entity_descriptor.fields_descriptors["name"] - if field_name_descriptor.caption: - caption = get_callable_str(field_name_descriptor.caption, field_name_descriptor) - else: - caption = field_name_descriptor.name + caption = ", ".join( + [ + get_callable_str( + entity_descriptor.fields_descriptors[field_name].caption, + entity_descriptor, + ) + if entity_descriptor.fields_descriptors[field_name].caption + else field_name + for field_name in filtering_fields + ] + ) keyboard_builder.row( InlineKeyboardButton( diff --git a/bot/handlers/editors/entity.py b/bot/handlers/editors/entity.py index dde1931..0134e3f 100644 --- a/bot/handlers/editors/entity.py +++ b/bot/handlers/editors/entity.py @@ -263,6 +263,7 @@ async def render_entity_picker( keyboard_builder=keyboard_builder, entity_descriptor=type_.bot_entity_descriptor, filter=entity_filter, + filtering_fields=form_list.filtering_fields, ) if is_list: diff --git a/bot/handlers/editors/main_callbacks.py b/bot/handlers/editors/main_callbacks.py index e55faf4..8fe7e80 100644 --- a/bot/handlers/editors/main_callbacks.py +++ b/bot/handlers/editors/main_callbacks.py @@ -200,13 +200,14 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs prev_form_name or "default", entity_descriptor.default_list ) - for filt in prev_form_list.static_filters: - if filt.value_type == "const": - entity_data[filt.field_name] = filt.value - elif len(prev_form_params) > filt.param_index: - entity_data[filt.field_name] = prev_form_params[ - filt.param_index - ] + if prev_form_list.static_filters: + for filt in prev_form_list.static_filters: + if filt.value_type == "const": + entity_data[filt.field_name] = filt.value + elif len(prev_form_params) > filt.param_index: + entity_data[filt.field_name] = prev_form_params[ + filt.param_index + ] if ( callback_data.context @@ -298,6 +299,9 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs state_data.update(state_data) + clear_state(state_data=state_data) + return await route_callback(message=message, back=False, **kwargs) + elif callback_data.context in [ CommandContext.ENTITY_EDIT, CommandContext.ENTITY_FIELD_EDIT, diff --git a/bot/handlers/forms/entity_list.py b/bot/handlers/forms/entity_list.py index 5150484..9d11beb 100644 --- a/bot/handlers/forms/entity_list.py +++ b/bot/handlers/forms/entity_list.py @@ -232,6 +232,7 @@ async def entity_list( keyboard_builder=keyboard_builder, entity_descriptor=entity_descriptor, filter=entity_filter, + filtering_fields=form_list.filtering_fields, ) context = pop_navigation_context(navigation_stack) diff --git a/model/bot_enum.py b/model/bot_enum.py index 94beb58..e0585ca 100644 --- a/model/bot_enum.py +++ b/model/bot_enum.py @@ -1,3 +1,4 @@ +from aiogram.utils.i18n import I18n from pydantic_core.core_schema import str_schema from sqlalchemy.types import TypeDecorator, String from typing import Any, Self, overload @@ -133,7 +134,11 @@ class EnumMember(object): if lang and lang in self.loc_obj.keys(): return self.loc_obj[lang] else: - return self.loc_obj[list(self.loc_obj.keys())[0]] + i18n = I18n.get_current() + if i18n: + return self.loc_obj[i18n.current_locale] + else: + return self.loc_obj[list(self.loc_obj.keys())[0]] return self.value diff --git a/model/descriptors.py b/model/descriptors.py index d728c86..fd7fa00 100644 --- a/model/descriptors.py +++ b/model/descriptors.py @@ -64,7 +64,7 @@ class EntityList: show_add_new_button: bool = True item_form: str | None = None pagination: bool = True - static_filters: list[Filter] | Any = None + static_filters: list[Filter] = None filtering: bool = False filtering_fields: list[str] = None order_by: str | Any | None = None diff --git a/utils/navigation.py b/utils/navigation.py index a0d547b..4011d24 100644 --- a/utils/navigation.py +++ b/utils/navigation.py @@ -18,6 +18,8 @@ def save_navigation_context( stack and navigation_context and navigation_context.command == callback_data.command + and navigation_context.entity_name == callback_data.entity_name + and navigation_context.entity_id == callback_data.entity_id and navigation_context.command != CallbackCommand.USER_COMMAND ): navigation_context = callback_data