functionality for editing teacher's info
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from telebot import types, TeleBot
|
from telebot import types, TeleBot
|
||||||
from database.models import Teachers
|
from database.models import Teachers
|
||||||
from bot.utils.formatters import format_teacher_info
|
from bot.utils.formatters import format_teacher_info
|
||||||
|
from bot.handlers.start import startMarkup
|
||||||
|
|
||||||
|
|
||||||
def register(bot: TeleBot):
|
def register(bot: TeleBot):
|
||||||
@@ -17,6 +18,8 @@ def register(bot: TeleBot):
|
|||||||
if teacher:
|
if teacher:
|
||||||
details = format_teacher_info(teacher)
|
details = format_teacher_info(teacher)
|
||||||
markup = types.InlineKeyboardMarkup()
|
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,
|
bot.send_message(call.message.chat.id, details,
|
||||||
reply_markup=markup, parse_mode="HTML")
|
reply_markup=markup, parse_mode="HTML")
|
||||||
else:
|
else:
|
||||||
@@ -108,3 +111,71 @@ def register(bot: TeleBot):
|
|||||||
bot.clear_step_handler(call.message)
|
bot.clear_step_handler(call.message)
|
||||||
bot.send_message(call.message.chat.id, "Action cancelled.")
|
bot.send_message(call.message.chat.id, "Action cancelled.")
|
||||||
bot.answer_callback_query(call.id)
|
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())
|
||||||
|
|||||||
@@ -27,3 +27,15 @@ def register(bot):
|
|||||||
def handle_test_options(message):
|
def handle_test_options(message):
|
||||||
option = message.text
|
option = message.text
|
||||||
bot.send_message(message.chat.id, f"You selected: {option}")
|
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
|
||||||
|
|||||||
@@ -112,3 +112,45 @@ class Teachers:
|
|||||||
finally:
|
finally:
|
||||||
if conn:
|
if conn:
|
||||||
connection.release_db_connection(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)
|
||||||
|
|||||||
Reference in New Issue
Block a user