functionality for editing teacher's info
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from telebot import types, TeleBot
|
||||
from database.models import Teachers
|
||||
from bot.utils.formatters import format_teacher_info
|
||||
from bot.handlers.start import startMarkup
|
||||
|
||||
|
||||
def register(bot: TeleBot):
|
||||
@@ -17,6 +18,8 @@ def register(bot: TeleBot):
|
||||
if teacher:
|
||||
details = format_teacher_info(teacher)
|
||||
markup = types.InlineKeyboardMarkup()
|
||||
markup.add(types.InlineKeyboardButton("Edit Teacher",
|
||||
callback_data=f"edit_teacher_{teacher_id}", switch_inline_query_current_chat="A default name for the teacher..."))
|
||||
bot.send_message(call.message.chat.id, details,
|
||||
reply_markup=markup, parse_mode="HTML")
|
||||
else:
|
||||
@@ -108,3 +111,71 @@ def register(bot: TeleBot):
|
||||
bot.clear_step_handler(call.message)
|
||||
bot.send_message(call.message.chat.id, "Action cancelled.")
|
||||
bot.answer_callback_query(call.id)
|
||||
|
||||
# Editing a Teacher Flow
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_teacher_'))
|
||||
def start_teacher_editing(call):
|
||||
teacher_id = call.data.split('_')[2]
|
||||
teacher = Teachers.getTeachertById(teacher_id)
|
||||
|
||||
editMarkup = types.ReplyKeyboardMarkup(
|
||||
resize_keyboard=True, one_time_keyboard=True)
|
||||
|
||||
for field in ['name', 'email', 'phone_number', 'password', 'username', 'birthday', 'about_me', 'job_title', 'Cancel']:
|
||||
editMarkup.add(types.KeyboardButton(field.capitalize()))
|
||||
|
||||
if teacher:
|
||||
msg = bot.send_message(call.message.chat.id,
|
||||
"Please enter the field you want to edit: ", reply_markup=editMarkup)
|
||||
|
||||
bot.register_next_step_handler(
|
||||
msg, process_field_select_step, teacher)
|
||||
else:
|
||||
bot.send_message(call.message.chat.id, "Teacher not found.")
|
||||
|
||||
bot.answer_callback_query(call.id)
|
||||
|
||||
def process_field_select_step(message, teacher: tuple):
|
||||
field = message.text.lower()
|
||||
if field == 'cancel':
|
||||
bot.send_message(message.chat.id, "Action cancelled.")
|
||||
return
|
||||
|
||||
fields = ['id', 'username', 'name', 'created_at', 'email', 'phone_number', 'last_seen', 'is_verfied',
|
||||
'birthday', 'about_me', 'job_title']
|
||||
fields_index = {field: i for i, field in enumerate(fields)}
|
||||
|
||||
if field in fields:
|
||||
previous_value = teacher[fields_index[field]]
|
||||
|
||||
markup = types.ReplyKeyboardMarkup(
|
||||
resize_keyboard=True, one_time_keyboard=True, row_width=1)
|
||||
markup.add(types.KeyboardButton(
|
||||
f"{previous_value} (current)"), types.KeyboardButton("Cancel"))
|
||||
|
||||
msg = bot.send_message(
|
||||
message.chat.id, f"Please enter new value for {field}:", reply_markup=markup)
|
||||
bot.register_next_step_handler(
|
||||
msg, process_value_edit_step, teacher[fields_index["id"]], field, previous_value)
|
||||
else:
|
||||
bot.send_message(
|
||||
message.chat.id, "Invalid field. Action cancelled.")
|
||||
|
||||
def process_value_edit_step(message, teacher_id, field, previous_value):
|
||||
new_value = message.text
|
||||
if (new_value.lower() == 'cancel'):
|
||||
bot.send_message(message.chat.id, "Action cancelled.",
|
||||
reply_markup=startMarkup())
|
||||
return
|
||||
|
||||
if (new_value == f"{previous_value} (current)"):
|
||||
bot.send_message(
|
||||
message.chat.id, f"No changes made to Teacher's {field}.", reply_markup=startMarkup())
|
||||
return
|
||||
|
||||
if (Teachers.updateTeacher(teacher_id, **{field: new_value})):
|
||||
bot.send_message(
|
||||
message.chat.id, f"✅ Teacher's {field} updated successfully.", reply_markup=startMarkup())
|
||||
else:
|
||||
bot.send_message(
|
||||
message.chat.id, f"❌ Failed to update Teacher's {field}.", reply_markup=startMarkup())
|
||||
|
||||
Reference in New Issue
Block a user