def send_message_or_file(message, subject_id): if message.text == '/cancel': bot.send_message(chat_id=message.from_user.id, text=f'Дія була скасована {emojize(":white_check_mark:", use_aliases=True)}') bot.clear_step_handler_by_chat_id(chat_id=message.from_user.id) elif message.content_type not in ['text', 'photo', 'document']: bot.send_message(chat_id=message.from_user.id, text=f'Файл/повідомлення не відправлено {emojize(":x:", use_aliases=True)}\n' 'Некорректний формат\n\n' 'Відправте файл/повідомлення боту і він його передасть всім студентам цього предмету\n\n' 'Щоб скасувати дію можна скористатися командою /cancel') bot.register_next_step_handler(message, send_message_or_file, subject_id) else: subject = Subject.get_subject_fullname_by_id(subject_id) students = [] for group in Group.get_groups(): for student in Student.get_students_by_group(group.id): students.append(student) caption = '' if message.caption is None else message.caption if message.content_type == 'text': for student in students: try: bot.send_message(chat_id=student.id, text=f'Повідомлення по предмету {subject}:\n\n{message.text}') except ApiException: continue bot.send_message(chat_id=message.from_user.id, text=f'Повідомлення було відправлено ' f'{emojize(":white_check_mark:", use_aliases=True)}') elif message.content_type == 'document': for student in students: try: bot.send_document(chat_id=student.id, data=message.document.file_id, caption=f'Методичний матеріал з предмету {subject} ' f'\n\n{caption}') except ApiException: continue bot.send_message(chat_id=message.from_user.id, text=f'Файл було відправлено {emojize(":white_check_mark:", use_aliases=True)}') save_file_to_local(message.document.file_id, subject) elif message.content_type == 'photo': for student in students: try: bot.send_photo(chat_id=student.id, photo=message.photo[-1].file_id, caption=f'Методичний матеріал з предмету {subject} ' f'\n\n{caption}') except ApiException: continue bot.send_message(chat_id=message.from_user.id, text=f'Фото було відправлено {emojize(":white_check_mark:", use_aliases=True)}') save_file_to_local(message.photo[-1].file_id, subject)
def add_profcomdebtor(call): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='debtorgroup_') bot.send_message(chat_id=call.from_user.id, text='Вибери групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(call.from_user.id, get_group_for_profcomdebt)
def get_debtors_by_group(call): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='grdebtor_') bot.send_message(chat_id=call.from_user.id, text='Вибери групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(call.from_user.id, show_debtors_by_group_callback)
def headman_assignment(call): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='headmangroup_') bot.send_message(chat_id=call.from_user.id, text='Вибери групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(call.from_user.id, get_group_headman_assign)
def headman_changing(call): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='chheadgroup_') bot.send_message(chat_id=call.from_user.id, text='Змінити старосту групи:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(call.from_user.id, get_group_headman_change)
def add_extragrade(message): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='extragradegroup_') bot.send_message(chat_id=message.from_user.id, text='Вибери групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(message.from_user.id, get_group_for_extragrade)
def register(message): group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='group_') bot.delete_message(chat_id=message.from_user.id, message_id=message.message.message_id) bot.send_message(chat_id=message.from_user.id, text='Вибери свою групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(message.from_user.id, get_group)
def get_grade_type(call): subject_id = call.data.split('_')[1] grade_type_id = call.data.split('_')[2] group_keyboard = make_keyboard( 'student', Group.get_groups()[:4], f'studentsgroup_{subject_id}_{grade_type_id}_') bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id, text='Виберіть групу:', reply_markup=group_keyboard)
def register_for_admins(message): if Student.get_student_by_id(message.from_user.id) is None: group_keyboard = make_keyboard(keyboard_type='group', elem_list=Group.get_groups(), marker='group_') bot.send_message(chat_id=message.from_user.id, text='Вибери свою групу:', reply_markup=group_keyboard) bot.register_next_step_handler_by_chat_id(message.from_user.id, get_group) else: bot.send_message(chat_id=message.from_user.id, text='Ти вже зареєстрований') bot.clear_step_handler_by_chat_id(chat_id=message.from_user.id)
def add_grades(): if len(session.query(Grade).all()) > 0: return else: random.seed(5) for group in Group.get_groups()[:4]: for student in Student.get_students_by_group(group.id): grade = random.randint(75, 100) session.add(Grade(grade=grade, ects=Grade.convert_to_ects(grade), date=date.today().strftime("%m/%d/%Y"), gradetype_id=5, student_id=student.id, subject_id=1)) grade = random.randint(75, 100) session.add(Grade(grade=grade, ects=Grade.convert_to_ects(grade), date=date.today().strftime("%m/%d/%Y"), gradetype_id=4, student_id=student.id, subject_id=2)) grade = random.randint(75, 100) session.add(Grade(grade=grade, ects=Grade.convert_to_ects(grade), date=date.today().strftime("%m/%d/%Y"), gradetype_id=3, student_id=student.id, subject_id=3)) grade = random.randint(75, 100) session.add(Grade(grade=grade, ects=Grade.convert_to_ects(grade), date=date.today().strftime("%m/%d/%Y"), gradetype_id=5, student_id=student.id, subject_id=4)) grade = random.randint(75, 100) session.add(Grade(grade=grade, ects=Grade.convert_to_ects(grade), date=date.today().strftime("%m/%d/%Y"), gradetype_id=5, student_id=student.id, subject_id=5)) session.commit() print("grades added")
def list_headman(call): headmans_list = Headman.get_all_headmans() message_text = f'<b>Старости {len(headmans_list)}/52:</b>\n\n' for group in Group.get_groups(): headman = Headman.get_headman_by_group(group_id=group.id) if headman: student = Student.get_student_by_id(headman.student_id) link = f'<a href="t.me/{student.username}">{get_fio(student.name)}</a> ({headman.rating})\n' else: link = '-\n' message_text += ''.join(f'КНТ-{group.name}: {link}') bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id, text=message_text, parse_mode='html', disable_web_page_preview=True)
def prepare_student_events_table(): group_dict = {} for group in Group.get_groups(): students = EventVisitor.get_visitor_students(group.id) if not students: continue else: stud_dict = {} for student in students: try: s_name = get_fio(Student.get_student_by_id(student[1].student_id).name) s_event = Event.get_event(student[1].event_id).name stud_dict.setdefault(s_name, []).append(s_event) except AttributeError: continue group_dict[group.name] = stud_dict return group_dict