From d1b22b88d672810e7235e2d2fa7fe77be7f7bb4f Mon Sep 17 00:00:00 2001 From: MikiEremiki Date: Sun, 27 Oct 2024 21:59:25 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81=D1=82=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F=20state=20=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=20=D0=B2=20=D0=BD=D0=B5=D0=B3=D0=BE=20schedule=5Fevent?= =?UTF-8?q?=5Fids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/db_postgres.py | 7 ++- src/handlers/reserve_hl.py | 57 +++++++++++++++++----- src/handlers/studio_hl.py | 3 +- src/handlers/sub_hl.py | 41 +--------------- src/utilities/schemas/context_user_data.py | 15 ++---- src/utilities/utl_func.py | 3 +- 6 files changed, 58 insertions(+), 68 deletions(-) diff --git a/src/db/db_postgres.py b/src/db/db_postgres.py index 8d4df79..2a5db3f 100644 --- a/src/db/db_postgres.py +++ b/src/db/db_postgres.py @@ -623,15 +623,14 @@ async def get_schedule_events_by_theater_ids_actual( return result.scalars().all() -async def get_schedule_events_by_theater_and_month_and_types_actual( +async def get_schedule_events_by_ids_and_theater( session: AsyncSession, + schedule_event_ids: List[int], theater_event_ids: List[int], - type_event_ids: List[int] ): query = select(ScheduleEvent).where( + ScheduleEvent.id.in_(schedule_event_ids), ScheduleEvent.theater_event_id.in_(theater_event_ids), - ScheduleEvent.type_event_id.in_(type_event_ids), - ScheduleEvent.datetime_event >= datetime.now() ).order_by(ScheduleEvent.datetime_event) result = await session.execute(query) return result.scalars().all() diff --git a/src/handlers/reserve_hl.py b/src/handlers/reserve_hl.py index 74c53f9..b180371 100644 --- a/src/handlers/reserve_hl.py +++ b/src/handlers/reserve_hl.py @@ -2,8 +2,6 @@ import pprint from datetime import datetime -from telegram.error import BadRequest -from telegram.ext import ContextTypes, ConversationHandler, TypeHandler from telegram import ( Update, InlineKeyboardMarkup, @@ -20,7 +18,7 @@ from handlers.email_hl import check_email_and_update_user from handlers.sub_hl import ( request_phone_number, - send_breaf_message, send_filtered_schedule_events, + send_breaf_message, send_message_about_list_waiting, remove_button_from_last_message, create_and_send_payment, processing_successful_payment, @@ -42,13 +40,16 @@ get_formatted_date_and_time_of_event, create_event_names_text, get_events_for_time_hl, get_type_event_ids_by_command, get_emoji, clean_context, - add_clients_data_to_text, add_qty_visitors_to_text + add_clients_data_to_text, add_qty_visitors_to_text, + filter_schedule_event_by_active, get_unique_months, + clean_replay_kb_and_send_typing_action ) from utilities.utl_kbd import ( create_kbd_schedule_and_date, create_kbd_schedule, create_kbd_for_time_in_reserve, create_replay_markup, create_kbd_and_text_tickets_for_choice, create_kbd_for_time_in_studio, - create_kbd_for_date_in_reserve, remove_intent_id + create_kbd_for_date_in_reserve, remove_intent_id, create_kbd_with_months, + adjust_kbd ) from settings.settings import ( ADMIN_GROUP, COMMAND_DICT, SUPPORT_DATA, RESERVE_TIMEOUT @@ -86,11 +87,33 @@ async def choice_month(update: Update, context: ContextTypes.DEFAULT_TYPE): reserve_hl_logger.info(f'Пользователь начал выбор месяца: {user}') type_event_ids = await get_type_event_ids_by_command(command) - reply_markup, text = await send_filtered_schedule_events(update, - context, - type_event_ids) + schedule_events = await db_postgres.get_schedule_events_by_type_actual( + context.session, type_event_ids) + schedule_events = await filter_schedule_event_by_active(schedule_events) + months = get_unique_months(schedule_events) + message = await clean_replay_kb_and_send_typing_action(update) + text = 'Выберите месяц' + keyboard = await create_kbd_with_months(months) + keyboard = adjust_kbd(keyboard, 1) + keyboard.append(add_btn_back_and_cancel( + postfix_for_cancel=context.user_data['postfix_for_cancel'], + add_back_btn=False + )) + reply_markup = InlineKeyboardMarkup(keyboard) + await context.bot.delete_message( + chat_id=update.effective_chat.id, + message_id=message.message_id + ) + await update.effective_chat.send_message( + text=text, + reply_markup=reply_markup, + message_thread_id=update.effective_message.message_thread_id + ) state = 'MONTH' + schedule_event_ids = [item.id for item in schedule_events] + state_data = context.user_data['reserve_user_data'][state] = {} + state_data['schedule_event_ids'] = schedule_event_ids await set_back_context(context, state, text, reply_markup) context.user_data['STATE'] = state if context.user_data.get('command', False) and query: @@ -115,7 +138,8 @@ async def choice_show_or_date( reserve_hl_logger.info(f'Пользователь выбрал месяц: {number_of_month_str}') reserve_user_data = context.user_data['reserve_user_data'] - schedule_event_ids = reserve_user_data['schedule_event_ids'] + state = context.user_data['STATE'] + schedule_event_ids = reserve_user_data[state]['schedule_event_ids'] schedule_events = await db_postgres.get_schedule_events_by_ids( context.session, schedule_event_ids) @@ -172,6 +196,9 @@ async def choice_show_or_date( ) reserve_user_data['number_of_month_str'] = number_of_month_str + schedule_event_ids = [item.id for item in schedule_events_filter_by_month] + state_data = context.user_data['reserve_user_data'][state] = {} + state_data['schedule_event_ids'] = schedule_event_ids await set_back_context(context, state, text, reply_markup) context.user_data['STATE'] = state @@ -194,10 +221,13 @@ async def choice_date(update: Update, context: ContextTypes.DEFAULT_TYPE): theater_event_id = int(callback_data) reserve_user_data = context.user_data['reserve_user_data'] number_of_month_str = reserve_user_data['number_of_month_str'] + + state = context.user_data['STATE'] + schedule_event_ids = reserve_user_data[state]['schedule_event_ids'] theater_event = await db_postgres.get_theater_event( context.session, theater_event_id) - schedule_events = await db_postgres.get_schedule_events_by_theater_ids_actual( - context.session, [theater_event_id]) + schedule_events = await db_postgres.get_schedule_events_by_ids_and_theater( + context.session, schedule_event_ids, [theater_event_id]) keyboard = await create_kbd_for_date_in_reserve(schedule_events) reply_markup = await create_replay_markup( @@ -254,6 +284,11 @@ async def choice_date(update: Update, context: ContextTypes.DEFAULT_TYPE): state = 'LIST_WAIT' else: state = 'DATE' + + schedule_event_ids = [item.id for item in schedule_events] + state_data = context.user_data['reserve_user_data'][state] = {} + state_data['schedule_event_ids'] = schedule_event_ids + await set_back_context(context, state, text, reply_markup) context.user_data['STATE'] = state return state diff --git a/src/handlers/studio_hl.py b/src/handlers/studio_hl.py index 9ed73ed..73f841c 100644 --- a/src/handlers/studio_hl.py +++ b/src/handlers/studio_hl.py @@ -19,7 +19,8 @@ async def choice_show_and_date(update: Update, studio_hl_logger.info(f'Пользователь выбрал месяц: {number_of_month_str}') reserve_user_data = context.user_data['reserve_user_data'] - schedule_event_ids = reserve_user_data['schedule_event_ids'] + state = context.user_data['STATE'] + schedule_event_ids = reserve_user_data[state]['schedule_event_ids'] schedule_events = await db_postgres.get_schedule_events_by_ids( context.session, schedule_event_ids) diff --git a/src/handlers/sub_hl.py b/src/handlers/sub_hl.py index fdbae41..946f022 100644 --- a/src/handlers/sub_hl.py +++ b/src/handlers/sub_hl.py @@ -23,13 +23,12 @@ from utilities import add_btn_back_and_cancel from utilities.utl_func import ( get_unique_months, get_full_name_event, - filter_schedule_event_by_active, clean_replay_kb_and_send_typing_action, + filter_schedule_event_by_active, get_formatted_date_and_time_of_event, create_approve_and_reject_replay, set_back_context ) from utilities.utl_googlesheets import update_ticket_db_and_gspread -from utilities.utl_kbd import ( - adjust_kbd, create_kbd_with_months, create_email_confirm_btn) +from utilities.utl_kbd import create_email_confirm_btn sub_hl_logger = logging.getLogger('bot.sub_hl') @@ -223,15 +222,6 @@ async def get_theater_and_schedule_events_by_month(context, schedule_events, return tuple(enum_theater_events), schedule_events_filter_by_month -async def get_schedule_events_by_month(schedule_events, - number_of_month_str): - schedule_events_filter_by_month = [] - for event in schedule_events: - if event.datetime_event.month == int(number_of_month_str): - schedule_events_filter_by_month.append(event) - return schedule_events_filter_by_month - - async def remove_button_from_last_message(update, context): message_id = context.user_data['common_data']['message_id_buy_info'] try: @@ -688,33 +678,6 @@ async def send_agreement(update, context): return text, inline_markup -async def send_filtered_schedule_events(update, context, type_event_ids): - months, schedule_events = await get_schedule_events_and_month_by_type_event( - context, type_event_ids) - message = await clean_replay_kb_and_send_typing_action(update) - text = 'Выберите месяц' - keyboard = await create_kbd_with_months(months) - keyboard = adjust_kbd(keyboard, 1) - keyboard.append(add_btn_back_and_cancel( - postfix_for_cancel=context.user_data['postfix_for_cancel'], - add_back_btn=False - )) - reply_markup = InlineKeyboardMarkup(keyboard) - await context.bot.delete_message( - chat_id=update.effective_chat.id, - message_id=message.message_id - ) - await update.effective_chat.send_message( - text=text, - reply_markup=reply_markup, - message_thread_id=update.effective_message.message_thread_id - ) - schedule_event_ids = [item.id for item in schedule_events] - reserve_user_data = context.user_data['reserve_user_data'] - reserve_user_data['schedule_event_ids'] = schedule_event_ids - return reply_markup, text - - async def send_message_about_list_waiting(update: Update, context): reserve_user_data = context.user_data['reserve_user_data'] command = context.user_data.get('command', None) diff --git a/src/utilities/schemas/context_user_data.py b/src/utilities/schemas/context_user_data.py index a0e5837..5866bac 100644 --- a/src/utilities/schemas/context_user_data.py +++ b/src/utilities/schemas/context_user_data.py @@ -38,6 +38,9 @@ 'custom_made_event_id': CustomMadeEvent.id, }, 'reserve_user_data': { + str | int: { + 'schedule_event_ids': List[ScheduleEvent.id], + }, 'back': { str | int: { 'text': str, # текст для возврата назад в State str @@ -46,17 +49,6 @@ }, }, 'number_of_month_str': str, - 'choose_event_info': { - 'theater_event_id': int, - 'name_show': str, - 'date_show': str, - 'time_show': str, - 'event_id': int, - 'option': str, - 'text_emoji': str, - 'flag_indiv_cost': bool, - }, - # Инфо о выбранном показе пользователя 'text_select_event': str, 'client_data': { 'name_adult': str, @@ -70,7 +62,6 @@ 'choose_theater_event_id': TheaterEvent.id, 'chose_base_ticket_id': BaseTicket.base_ticket_id, 'type_ticket_price': str, - 'key_option_for_reserve': int, 'original_child_text': str, 'date_for_price': datetime, 'ticket_ids': List[int], diff --git a/src/utilities/utl_func.py b/src/utilities/utl_func.py index 658ba25..78002cf 100644 --- a/src/utilities/utl_func.py +++ b/src/utilities/utl_func.py @@ -938,7 +938,8 @@ async def get_events_for_time_hl(theater_event_id, selected_date, context): utilites_logger.info(f'Пользователь выбрал дату: {selected_date}') reserve_user_data = context.user_data['reserve_user_data'] - schedule_event_ids = reserve_user_data['schedule_event_ids'] + state = context.user_data['STATE'] + schedule_event_ids = reserve_user_data[state]['schedule_event_ids'] schedule_events = await db_postgres.get_schedule_events_by_ids( context.session, schedule_event_ids)