add command params
This commit is contained in:
@@ -8,10 +8,11 @@ import json
|
||||
|
||||
from ..context import ContextData, CallbackCommand, CommandContext
|
||||
from ...command_context_filter import CallbackCommandFilter
|
||||
from ..user_handlers.main import cammand_handler
|
||||
from ....model import EntityPermission
|
||||
from ....model.user import UserBase
|
||||
from ....model.settings import Settings
|
||||
from ....model.descriptors import EntityFieldDescriptor
|
||||
from ....model.descriptors import FieldDescriptor
|
||||
from ....model.language import LanguageBase
|
||||
from ....auth import authorize_command
|
||||
from ....utils.main import (
|
||||
@@ -60,7 +61,9 @@ async def field_editor_callback(message: Message | CallbackQuery, **kwargs):
|
||||
else:
|
||||
value = {}
|
||||
|
||||
value[list(LanguageBase.all_members.keys())[locale_index]] = message.text
|
||||
value[list(LanguageBase.all_members.values())[locale_index].value] = (
|
||||
message.text
|
||||
)
|
||||
value = json.dumps(value, ensure_ascii=False)
|
||||
|
||||
if locale_index < len(LanguageBase.all_members.values()) - 1:
|
||||
@@ -125,7 +128,7 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
callback_data: ContextData = kwargs.get("callback_data", None)
|
||||
state_data: dict = kwargs["state_data"]
|
||||
value = kwargs["value"]
|
||||
field_descriptor: EntityFieldDescriptor = kwargs["field_descriptor"]
|
||||
field_descriptor: FieldDescriptor = kwargs["field_descriptor"]
|
||||
|
||||
if callback_data.context == CommandContext.SETTING_EDIT:
|
||||
if callback_data.data != "cancel":
|
||||
@@ -145,29 +148,37 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
CommandContext.ENTITY_CREATE,
|
||||
CommandContext.ENTITY_EDIT,
|
||||
CommandContext.ENTITY_FIELD_EDIT,
|
||||
CommandContext.COMMAND_FORM,
|
||||
]:
|
||||
app: "QBotApp" = kwargs["app"]
|
||||
|
||||
entity_descriptor = get_entity_descriptor(app, callback_data)
|
||||
|
||||
form_name = (
|
||||
callback_data.form_params.split("&")[0]
|
||||
if callback_data.form_params
|
||||
else "default"
|
||||
)
|
||||
form = entity_descriptor.forms.get(form_name, entity_descriptor.default_form)
|
||||
if callback_data.context == CommandContext.COMMAND_FORM:
|
||||
field_sequence = list(field_descriptor.command.param_form.keys())
|
||||
current_index = field_sequence.index(callback_data.field_name)
|
||||
field_descriptors = field_descriptor.command.param_form
|
||||
else:
|
||||
form_name = (
|
||||
callback_data.form_params.split("&")[0]
|
||||
if callback_data.form_params
|
||||
else "default"
|
||||
)
|
||||
form = entity_descriptor.forms.get(
|
||||
form_name, entity_descriptor.default_form
|
||||
)
|
||||
|
||||
field_sequence = form.edit_field_sequence
|
||||
current_index = (
|
||||
field_sequence.index(callback_data.field_name)
|
||||
if callback_data.context
|
||||
in [CommandContext.ENTITY_CREATE, CommandContext.ENTITY_EDIT]
|
||||
else 0
|
||||
)
|
||||
field_sequence = form.edit_field_sequence
|
||||
current_index = (
|
||||
field_sequence.index(callback_data.field_name)
|
||||
if callback_data.context
|
||||
in [CommandContext.ENTITY_CREATE, CommandContext.ENTITY_EDIT]
|
||||
else 0
|
||||
)
|
||||
field_descriptors = entity_descriptor.fields_descriptors
|
||||
|
||||
entity_data = state_data.get("entity_data", {})
|
||||
|
||||
if callback_data.context == CommandContext.ENTITY_CREATE:
|
||||
if callback_data.context == CommandContext.ENTITY_CREATE and not entity_data:
|
||||
stack = state_data.get("navigation_stack", [])
|
||||
prev_callback_data = ContextData.unpack(stack[-1]) if stack else None
|
||||
if (
|
||||
@@ -199,16 +210,18 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
|
||||
if (
|
||||
callback_data.context
|
||||
in [CommandContext.ENTITY_CREATE, CommandContext.ENTITY_EDIT]
|
||||
in [
|
||||
CommandContext.ENTITY_CREATE,
|
||||
CommandContext.ENTITY_EDIT,
|
||||
CommandContext.COMMAND_FORM,
|
||||
]
|
||||
and current_index < len(field_sequence) - 1
|
||||
):
|
||||
entity_data[field_descriptor.field_name] = value
|
||||
state_data.update({"entity_data": entity_data})
|
||||
|
||||
next_field_name = field_sequence[current_index + 1]
|
||||
next_field_descriptor = entity_descriptor.fields_descriptors[
|
||||
next_field_name
|
||||
]
|
||||
next_field_descriptor = field_descriptors[next_field_name]
|
||||
kwargs.update({"field_descriptor": next_field_descriptor})
|
||||
callback_data.field_name = next_field_name
|
||||
|
||||
@@ -232,8 +245,6 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
)
|
||||
|
||||
else:
|
||||
entity_type = entity_descriptor.type_
|
||||
|
||||
entity_data[field_descriptor.field_name] = value
|
||||
|
||||
# What if user has several roles and each role has its own ownership field? Should we allow creation even
|
||||
@@ -246,13 +257,14 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
deser_entity_data = {
|
||||
key: await deserialize(
|
||||
session=db_session,
|
||||
type_=entity_descriptor.fields_descriptors[key].type_,
|
||||
type_=field_descriptors[key].type_,
|
||||
value=value,
|
||||
)
|
||||
for key, value in entity_data.items()
|
||||
}
|
||||
|
||||
if callback_data.context == CommandContext.ENTITY_CREATE:
|
||||
entity_type = entity_descriptor.type_
|
||||
user_permissions = get_user_permissions(user, entity_descriptor)
|
||||
if (
|
||||
EntityPermission.CREATE not in user_permissions
|
||||
@@ -290,6 +302,7 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
CommandContext.ENTITY_EDIT,
|
||||
CommandContext.ENTITY_FIELD_EDIT,
|
||||
]:
|
||||
entity_type = entity_descriptor.type_
|
||||
entity_id = int(callback_data.entity_id)
|
||||
entity = await entity_type.get(session=db_session, id=entity_id)
|
||||
if not entity:
|
||||
@@ -309,6 +322,23 @@ async def process_field_edit_callback(message: Message | CallbackQuery, **kwargs
|
||||
|
||||
await db_session.commit()
|
||||
|
||||
elif callback_data.context == CommandContext.COMMAND_FORM:
|
||||
clear_state(state_data=state_data)
|
||||
state_data["entity_data"] = entity_data
|
||||
|
||||
kwargs.update(
|
||||
{
|
||||
"callback_data": ContextData(
|
||||
command=CallbackCommand.USER_COMMAND,
|
||||
user_command=callback_data.user_command,
|
||||
data=callback_data.data,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
return await cammand_handler(message=message, **kwargs)
|
||||
|
||||
clear_state(state_data=state_data)
|
||||
|
||||
# TODO: Try back=False and check if it works to navigate to newly created entity
|
||||
await route_callback(message=message, back=True, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user