functionality for editing teacher's info

This commit is contained in:
Hosein
2025-12-31 21:24:52 +03:30
parent af88cbce8f
commit e844392479
3 changed files with 125 additions and 0 deletions
+71
View File
@@ -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())
+12
View File
@@ -27,3 +27,15 @@ def register(bot):
def handle_test_options(message):
option = message.text
bot.send_message(message.chat.id, f"You selected: {option}")
def startMarkup():
markup = types.ReplyKeyboardMarkup(
row_width=2, one_time_keyboard=True, resize_keyboard=True)
btn1 = types.KeyboardButton("Show Students")
btn2 = types.KeyboardButton("Show Teachers")
btn3 = types.KeyboardButton("Show Courses")
btn4 = types.KeyboardButton("Show Tag")
btn5 = types.KeyboardButton("Show Categories")
markup.add(btn1, btn2, btn3, btn4, btn5)
return markup
+42
View File
@@ -112,3 +112,45 @@ class Teachers:
finally:
if conn:
connection.release_db_connection(conn)
def updateTeacher(teacher_id, **fields):
if not fields:
return False # nothing to update
conn = None
try:
conn = connection.get_db_connection()
if not conn:
return False
cursor = conn.cursor()
# Build dynamic SET clause
columns = []
values = []
for key, value in fields.items():
columns.append(f"{key} = %s")
values.append(value)
values.append(teacher_id)
query = f"""
UPDATE teachers
SET {', '.join(columns)}
WHERE id = %s
"""
cursor.execute(query, tuple(values))
conn.commit()
cursor.close()
return True
except Exception as e:
logger.error(f"Database query error in updateTeacher: {e}")
return False
finally:
if conn:
connection.release_db_connection(conn)