def get_group_headman_assign(message): group = message.text group_id = Group.get_id_by_group(group) if group_id is False: bot.clear_step_handler_by_chat_id(message.from_user.id) bot.send_message( chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons)) else: if not Headman.get_headman_by_group(group_id): student_keyboard = make_keyboard( keyboard_type='student', elem_list=Student.get_students_by_group(group_id), marker=f'headman_{Group.get_group_by_id(group_id)}_') bot.send_message(chat_id=message.from_user.id, text='Вибери старосту:', reply_markup=student_keyboard) else: keyboard = InlineKeyboardMarkup() keyboard.add( InlineKeyboardButton( text= f'Змінити старосту {emojize(":repeat:", use_aliases=True)}', callback_data='change_headman')) bot.send_message( chat_id=message.from_user.id, text='Цій групі вже призначено старосту.\n' 'Якщо потрібно його змінити, скористайся командою ' f'{emojize(":point_down:", use_aliases=True)}', reply_markup=keyboard)
def get_debtors_by_subject_group(subject_id, group_id): debtors = session.query(SubjectDebtor, Student).filter(SubjectDebtor.subject_id == subject_id)\ .filter(Student.group_id == group_id).filter(SubjectDebtor.student_id == Student.id).all() return [ Student.get_student_by_id(debtor[0].student_id) for debtor in debtors ]
def get_group_for_grade(call): subject_id = call.data.split('_')[1] grade_type_id = call.data.split('_')[2] group_id = call.data.split('_')[3] students = (student for student in Student.get_students_by_group(group_id)) ask_grade(call, students, grade_type_id, subject_id, group_id)
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 get_debtors_by_group(group_id): debtors = session.query( ProfcomDebtor, Student).filter(Student.group_id == group_id).filter( ProfcomDebtor.student_id == Student.id).all() return [ Student.get_student_by_id(debtor[0].student_id) for debtor in debtors ]
def save_profcomdebtor(message, debtor_id, group): debt = message.text ProfcomDebtor.add_debtor(debtor_id, debt) username = Student.get_student_by_id(debtor_id).username name = Student.get_student_by_id(debtor_id).name bot.send_message(chat_id=message.from_user.id, text=f'Студент <a href="t.me/{username}">{name}</a> ' f'групи {group} занесений до боржників ' f'{emojize(":heavy_exclamation_mark:", use_aliases=True)}', parse_mode='html', disable_web_page_preview=True) bot.send_message(chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons))
def change_headman(call): group = call.data.split('_')[1] new_headman_id = call.data.split('_')[2] Headman.change_headman(new_headman_id) username = Student.get_student_by_id(new_headman_id).username name = Student.get_student_by_id(new_headman_id).name bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id, text=f'<a href="t.me/{username}">{name}</a> ' f'призначений старостою групи {group} ' f'{emojize(":white_check_mark:", use_aliases=True)}', parse_mode='html') bot.send_message(chat_id=call.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons))
def get_group(message): group_id = Group.get_id_by_group(message.text) if group_id is False: bot.clear_step_handler_by_chat_id(message.from_user.id) bot.send_message(chat_id=message.from_user.id, text='Вибери свою групу:') bot.register_next_step_handler_by_chat_id(message.from_user.id, get_group) else: student = Student() student.id = message.from_user.id student.username = message.from_user.username student.group_id = group_id message = bot.send_message(chat_id=message.from_user.id, text='Введи ПІБ українською мовою') bot.register_next_step_handler(message, get_name, student)
def show_headman_info(message): group = message.text group_id = Group.get_id_by_group(group) if group_id is False: bot.clear_step_handler_by_chat_id(message.from_user.id) bot.send_message( chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons)) else: headman = Headman.get_headman_by_group(group_id) if not headman: keyboard = InlineKeyboardMarkup() keyboard.add( InlineKeyboardButton( text=f'Призначити старосту ' f'{emojize(":white_check_mark:", use_aliases=True)}', callback_data='assign_headman')) bot.send_message(chat_id=message.from_user.id, text=f'Групі {group} непризначено старосту.\n' 'Для призначення старости скористайся командою ' f'{emojize(":point_down:", use_aliases=True)}', reply_markup=keyboard) else: username = Student.get_student_by_id(headman.student_id).username name = Student.get_student_by_id(headman.student_id).name phone = Student.get_student_by_id(headman.student_id).phone bot.send_message( chat_id=message.from_user.id, text= f'Староста групи {group}: <a href="t.me/{username}">{name}</a>\n' f'Номер телефону: {phone}', parse_mode='html', disable_web_page_preview=True) bot.send_message( chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons))
def get_profcomdebtor_for_del(call): debtor_id = call.data.split('_')[1] group = Group.get_group_by_id(Student.get_student_by_id(debtor_id).group_id) username = Student.get_student_by_id(debtor_id).username name = Student.get_student_by_id(debtor_id).name bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id, text=f'Студент <a href="t.me/{username}">{name}</a> ' f'групи {group} видалений з боржників ' f'{emojize(":heavy_exclamation_mark:", use_aliases=True)}', parse_mode='html', disable_web_page_preview=True) ProfcomDebtor.delete_debtor(debtor_id) bot.send_message(chat_id=call.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons))
def cancel_reg_on_event(call): event_id = call.data.split('_')[1] EventVisitor.delete_visitor(event_id, call.from_user.id) bot.edit_message_text( chat_id=call.from_user.id, message_id=call.message.message_id, text= f'Реєстрація скасована {emojize(":white_check_mark:", use_aliases=True)}' ) user = Student.get_student_by_id(call.from_user.id)
def save_extragrade(message, student_id): ExtraGrade.add_extragrade(extra_grade=int(message.text), student_id=student_id) student = Student.get_student_by_id(student_id) bot.send_message(chat_id=message.from_user.id, text=f'Додатковий бал студенту ' f'<a href="t.me/{student.username}">{student.name}</a>' f' було поставлено {emojize(":white_check_mark:", use_aliases=True)}', parse_mode='html') bot.send_message(chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons))
def register_on_event(call): event_id = call.data.split('_')[1] chat_id = call.from_user.id if Group.get_group_by_id( Student.get_student_by_id(chat_id).group_id) == 'other': message = bot.send_message( chat_id=chat_id, text="Введіть прізвище ім'я факультет групу\n" "<b>Наприклад:</b> <i>Петров Петро ФЕУ 123</i>", parse_mode='html') bot.register_next_step_handler(message, reg_on_event_other, event_id) else: EventVisitor.add_visitor(event_id, chat_id) bot.edit_message_text( chat_id=chat_id, message_id=call.message.message_id, text= f'Реєстрація пройшла успішно {emojize(":white_check_mark:", use_aliases=True)}' ) user = Student.get_student_by_id(chat_id)
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 get_group_for_profcomdebt(message): group = message.text group_id = Group.get_id_by_group(group) if group_id is False: bot.clear_step_handler_by_chat_id(message.from_user.id) bot.send_message(chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons)) else: student_keyboard = make_keyboard(keyboard_type='student', elem_list=[student for student in Student.get_students_by_group(group_id)], marker=f'debtor_{Group.get_group_by_id(group_id)}_') bot.send_message(chat_id=message.from_user.id, text='Вибери студента:', reply_markup=student_keyboard)
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 prepare_student_grades_table(): stud_dict = {} for student in Student.get_all_students(): try: name_group = f'{student.name}_{Group.get_group_by_id(student.group_id)}' score = calculate_score(student.id) if score is False: continue else: stud_dict[name_group] = [score[0], score[1], score[2]] except AttributeError: continue return stud_dict
def get_group_headman_change(message): group = message.text group_id = Group.get_id_by_group(group) if group_id is False: bot.clear_step_handler_by_chat_id(message.from_user.id) bot.send_message( chat_id=message.from_user.id, text='Вибери пункт меню:', reply_markup=make_role_replykeyboard(studdekan_buttons)) else: student_keyboard = make_keyboard( keyboard_type='student', elem_list=Student.get_students_by_group(group_id), marker=f'chheadman_{Group.get_group_by_id(group_id)}_') bot.send_message(chat_id=message.from_user.id, text='Вибери нового старосту:', reply_markup=student_keyboard)
def prepare_event_visitors_table(event_id): visitor_ids = EventVisitor.get_visitors(event_id) stud_dict = {} otherfac_list = [] for visitor_id in visitor_ids: student = Student.get_student_by_id(visitor_id) if Group.get_group_by_id(student.group_id) == 'other': otherfac_list.append(EventVisitor.get_visitor_by_id(visitor_id).note) else: try: s_name = get_fio(student.name) s_group = f'КНТ-{Group.get_group_by_id(student.group_id)}' stud_dict.setdefault(s_group, []).append(s_name) except AttributeError: continue return stud_dict, otherfac_list
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
def get_all_debtors(): return [ Student.get_student_by_id(debtor.student_id) for debtor in session.query(ProfcomDebtor).all() ]
def add_another_fac(message): Student.add_student( Student(id=message.from_user.id, username=message.from_user.username, group_id=Group.get_id_by_group('other')))
def change_headman(new_headman_id): new_headman = Student.get_student_by_id(new_headman_id) old_headman = Headman.get_headman_by_group(new_headman.group_id) old_headman.student_id = new_headman.id session.commit()