add command params
This commit is contained in:
@@ -19,7 +19,7 @@ from ....utils.main import (
|
||||
get_entity_descriptor,
|
||||
)
|
||||
from ..context import ContextData, CallbackCommand, CommandContext
|
||||
from ..navigation import (
|
||||
from ....utils.navigation import (
|
||||
pop_navigation_context,
|
||||
save_navigation_context,
|
||||
)
|
||||
@@ -84,94 +84,89 @@ async def entity_item(
|
||||
callback_data.form_params or "default", entity_descriptor.default_form
|
||||
)
|
||||
|
||||
for edit_buttons_row in form.form_buttons:
|
||||
btn_row = []
|
||||
for button in edit_buttons_row:
|
||||
if form.form_buttons:
|
||||
for edit_buttons_row in form.form_buttons:
|
||||
btn_row = []
|
||||
for button in edit_buttons_row:
|
||||
if button.visibility and not button.visibility(entity_item):
|
||||
continue
|
||||
|
||||
if button.visibility and not button.visibility(entity_item):
|
||||
continue
|
||||
|
||||
if isinstance(button, FieldEditButton) and can_edit:
|
||||
|
||||
field_name = button.field_name
|
||||
btn_caption = button.caption
|
||||
if field_name in entity_descriptor.fields_descriptors:
|
||||
field_descriptor = entity_descriptor.fields_descriptors[
|
||||
field_name
|
||||
]
|
||||
field_value = getattr(entity_item, field_descriptor.field_name)
|
||||
if btn_caption:
|
||||
btn_text = get_callable_str(
|
||||
btn_caption, field_descriptor, entity_item, field_value
|
||||
)
|
||||
else:
|
||||
if field_descriptor.type_base is bool:
|
||||
btn_text = f"{'【✔︎】 ' if field_value else '【 】 '}{
|
||||
get_callable_str(
|
||||
field_descriptor.caption,
|
||||
field_descriptor,
|
||||
entity_item,
|
||||
field_value,
|
||||
)
|
||||
if field_descriptor.caption
|
||||
else field_name
|
||||
}"
|
||||
if isinstance(button, FieldEditButton) and can_edit:
|
||||
field_name = button.field_name
|
||||
btn_caption = button.caption
|
||||
if field_name in entity_descriptor.fields_descriptors:
|
||||
field_descriptor = entity_descriptor.fields_descriptors[
|
||||
field_name
|
||||
]
|
||||
field_value = getattr(entity_item, field_descriptor.field_name)
|
||||
if btn_caption:
|
||||
btn_text = get_callable_str(
|
||||
btn_caption, field_descriptor, entity_item, field_value
|
||||
)
|
||||
else:
|
||||
btn_text = (
|
||||
f"✏️ {
|
||||
if field_descriptor.type_base is bool:
|
||||
btn_text = f"{'【✔︎】 ' if field_value else '【 】 '}{
|
||||
get_callable_str(
|
||||
field_descriptor.caption,
|
||||
field_descriptor,
|
||||
entity_item,
|
||||
field_value,
|
||||
)
|
||||
if field_descriptor.caption
|
||||
else field_name
|
||||
}"
|
||||
if field_descriptor.caption
|
||||
else f"✏️ {field_name}"
|
||||
else:
|
||||
btn_text = (
|
||||
f"✏️ {
|
||||
get_callable_str(
|
||||
field_descriptor.caption,
|
||||
field_descriptor,
|
||||
entity_item,
|
||||
field_value,
|
||||
)
|
||||
}"
|
||||
if field_descriptor.caption
|
||||
else f"✏️ {field_name}"
|
||||
)
|
||||
btn_row.append(
|
||||
InlineKeyboardButton(
|
||||
text=btn_text,
|
||||
callback_data=ContextData(
|
||||
command=CallbackCommand.FIELD_EDITOR,
|
||||
context=CommandContext.ENTITY_FIELD_EDIT,
|
||||
entity_name=entity_descriptor.name,
|
||||
entity_id=str(entity_item.id),
|
||||
field_name=field_name,
|
||||
).pack(),
|
||||
)
|
||||
btn_row.append(
|
||||
InlineKeyboardButton(
|
||||
text=btn_text,
|
||||
callback_data=ContextData(
|
||||
command=CallbackCommand.FIELD_EDITOR,
|
||||
context=CommandContext.ENTITY_FIELD_EDIT,
|
||||
entity_name=entity_descriptor.name,
|
||||
entity_id=str(entity_item.id),
|
||||
field_name=field_name,
|
||||
).pack(),
|
||||
)
|
||||
)
|
||||
|
||||
elif isinstance(button, CommandButton):
|
||||
elif isinstance(button, CommandButton):
|
||||
btn_caption = button.caption
|
||||
|
||||
btn_caption = button.caption
|
||||
if btn_caption:
|
||||
btn_text = get_callable_str(
|
||||
btn_caption, entity_descriptor, entity_item
|
||||
)
|
||||
else:
|
||||
btn_text = button.command
|
||||
|
||||
if isinstance(button.context_data, ContextData):
|
||||
btn_cdata = button.context_data
|
||||
elif callable(button.context_data):
|
||||
btn_cdata = button.context_data(callback_data, entity_item)
|
||||
else:
|
||||
btn_cdata = ContextData(
|
||||
command=CallbackCommand.USER_COMMAND,
|
||||
user_command=button.command,
|
||||
data=str(entity_item.id),
|
||||
if isinstance(button.command, ContextData):
|
||||
btn_cdata = button.command
|
||||
elif callable(button.command):
|
||||
btn_cdata = button.command(callback_data, entity_item)
|
||||
elif isinstance(button.command, str):
|
||||
btn_cdata = ContextData(
|
||||
command=CallbackCommand.USER_COMMAND,
|
||||
user_command=button.command,
|
||||
)
|
||||
|
||||
btn_row.append(
|
||||
InlineKeyboardButton(
|
||||
text=btn_text,
|
||||
callback_data=btn_cdata.pack(),
|
||||
)
|
||||
)
|
||||
|
||||
btn_row.append(
|
||||
InlineKeyboardButton(
|
||||
text=btn_text,
|
||||
callback_data=btn_cdata.pack(),
|
||||
)
|
||||
)
|
||||
|
||||
if btn_row:
|
||||
keyboard_builder.row(*btn_row)
|
||||
if btn_row:
|
||||
keyboard_builder.row(*btn_row)
|
||||
|
||||
edit_delete_row = []
|
||||
if can_edit and form.show_edit_button:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from aiogram import Router, F
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery, InlineKeyboardButton
|
||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
@@ -10,7 +11,7 @@ from ....model.settings import Settings
|
||||
from ....model import EntityPermission
|
||||
from ....utils.main import (
|
||||
check_entity_permission,
|
||||
get_value_repr,
|
||||
get_entity_item_repr,
|
||||
get_entity_descriptor,
|
||||
)
|
||||
from ..common.routing import route_callback
|
||||
@@ -28,6 +29,9 @@ async def entity_delete_callback(query: CallbackQuery, **kwargs):
|
||||
user: UserBase = kwargs["user"]
|
||||
db_session: AsyncSession = kwargs["db_session"]
|
||||
app: "QBotApp" = kwargs["app"]
|
||||
state: FSMContext = kwargs["state"]
|
||||
state_data = await state.get_data()
|
||||
kwargs["state_data"] = state_data
|
||||
|
||||
entity_descriptor = get_entity_descriptor(app=app, callback_data=callback_data)
|
||||
|
||||
@@ -49,12 +53,7 @@ async def entity_delete_callback(query: CallbackQuery, **kwargs):
|
||||
|
||||
await route_callback(message=query, **kwargs)
|
||||
|
||||
elif callback_data.data == "no":
|
||||
await route_callback(message=query, back=False, **kwargs)
|
||||
|
||||
elif not callback_data.data:
|
||||
field_descriptor = entity_descriptor.fields_descriptors["name"]
|
||||
|
||||
entity = await entity_descriptor.type_.get(
|
||||
session=db_session, id=int(callback_data.entity_id)
|
||||
)
|
||||
@@ -62,13 +61,7 @@ async def entity_delete_callback(query: CallbackQuery, **kwargs):
|
||||
return await query.message.edit_text(
|
||||
text=(
|
||||
await Settings.get(Settings.APP_STRINGS_CONFIRM_DELETE_P_NAME)
|
||||
).format(
|
||||
name=get_value_repr(
|
||||
value=getattr(entity, field_descriptor.name),
|
||||
field_descriptor=field_descriptor,
|
||||
locale=user.lang,
|
||||
)
|
||||
),
|
||||
).format(name=get_entity_item_repr(entity=entity)),
|
||||
reply_markup=InlineKeyboardBuilder()
|
||||
.row(
|
||||
InlineKeyboardButton(
|
||||
@@ -88,7 +81,6 @@ async def entity_delete_callback(query: CallbackQuery, **kwargs):
|
||||
entity_name=callback_data.entity_name,
|
||||
entity_id=callback_data.entity_id,
|
||||
form_params=callback_data.form_params,
|
||||
data="no",
|
||||
).pack(),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ from ....utils.serialization import deserialize
|
||||
from ..context import ContextData, CallbackCommand, CommandContext
|
||||
from ..common.pagination import add_pagination_controls
|
||||
from ..common.filtering import add_filter_controls
|
||||
from ..navigation import pop_navigation_context, save_navigation_context
|
||||
from ....utils.navigation import pop_navigation_context, save_navigation_context
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ....main import QBotApp
|
||||
|
||||
Reference in New Issue
Block a user