From 93b313690eaf23d2ac98b49f418881ad85abf500 Mon Sep 17 00:00:00 2001 From: faridik Date: Wed, 1 Sep 2021 16:39:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BA=20=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpreadsheetsService/manager/manager.py | 22 +++++++++++---- SpreadsheetsService/models/assignment.py | 14 ++++++++- TelegramBot/bot.py | 36 +++++++++++++----------- TelegramBot/messages/messages.py | 23 +++++++++++---- TelegramBot/utils/inline_keyboard.py | 2 ++ 5 files changed, 70 insertions(+), 27 deletions(-) diff --git a/SpreadsheetsService/manager/manager.py b/SpreadsheetsService/manager/manager.py index 48e48ab..33dca40 100644 --- a/SpreadsheetsService/manager/manager.py +++ b/SpreadsheetsService/manager/manager.py @@ -26,6 +26,8 @@ ASSIGNMENT_SUBJECT = 3 ASSIGNMENT_WEIGHTS = 4 ASSIGNMENT_RANGES = 5 ASSIGNMENT_ALLOWS = 6 +ASSIGNMENT_HOW_TO_DISPLAY = 7 +ASSIGNMENT_NOTES_RANGES = 8 TOKEN_FILE = pathlib.Path() / ".secrets" / "token.json" CLIENT_SECRET_FILE = pathlib.Path() / ".secrets" / "client_secret.json" @@ -211,14 +213,24 @@ class Manager: f = lambda row: student.group_id == row[GROUP_IDS] # TODO add subject for row in filter(f, values): - assignment_value = self.get_values(row[ASSIGNMENT_RANGES]) + assignment_values = self.get_values(row[ASSIGNMENT_RANGES]) toFloat = lambda x: float(x.replace(",", ".")) - ass_values = list(map(toFloat, assignment_value[student.number])) + assignment_value = list(map(toFloat, assignment_values[student.number-1])) + notes_range, n_row = row[ASSIGNMENT_NOTES_RANGES].split(',') + note = self.get_values(notes_range)[student.number-1][int(n_row)] + if note == '-': + note = "Замечаний по работе нет." + else: + note = f"Замечания:\n {note}" student.add_assignment( Assignment( - row[ASSIGNMENT_NAMES], ass_values, - toFloat(row[ASSIGNMENT_WEIGHTS]), - row[ASSIGNMENT_SUBJECT] + name=row[ASSIGNMENT_NAMES], + points=assignment_value, + weight=toFloat(row[ASSIGNMENT_WEIGHTS]), + subject=row[ASSIGNMENT_SUBJECT], + allow_to_display=bool(int(row[ASSIGNMENT_ALLOWS])), + how_to_display=row[ASSIGNMENT_HOW_TO_DISPLAY], + notes=note ) ) diff --git a/SpreadsheetsService/models/assignment.py b/SpreadsheetsService/models/assignment.py index 78aad64..4c686b3 100644 --- a/SpreadsheetsService/models/assignment.py +++ b/SpreadsheetsService/models/assignment.py @@ -1,11 +1,20 @@ +from logging import addLevelName + + class Assignment(dict): """Задание.""" - def __init__(self, name: str, points: list, weight: float = 1, subject: str = None): + def __init__(self, name: str, points: list, weight: float = 1, + subject: str = None, allow_to_display: bool = False, + how_to_display: str = 'z,1,1', + notes: str = 'Замечаний по работе нет.'): self.name = name self.subject = subject self.weight = weight self.points = points + self.allow_to_display = allow_to_display + self.how_to_display = how_to_display + self.notes = notes dict.__init__( self, name=self.name, @@ -13,6 +22,9 @@ class Assignment(dict): weight=self.weight, points=self.points, total=self.total, + allow_to_display=self.allow_to_display, + how_to_display=self.how_to_display, + notes=self.notes, ) @property diff --git a/TelegramBot/bot.py b/TelegramBot/bot.py index 1cfe400..16be060 100644 --- a/TelegramBot/bot.py +++ b/TelegramBot/bot.py @@ -95,15 +95,17 @@ def grades(update: Update, context: CallbackContext): context.user_data["subjects"] = subjects context.user_data["all_assignments"] = assignments reply_markup = build_menu_of_subjects(subjects) - update.message.reply_text( + msg = update.message.reply_text( text=MESSAGES.Assignments.SELECT_COURSE, reply_markup=reply_markup ) else: context.user_data["assignments"] = assignments reply_markup = build_menu_of_assignments(assignments) - update.message.reply_text( + msg = update.message.reply_text( text=MESSAGES.Assignments.SELECT_ASSNT, reply_markup=reply_markup ) + + context.user_data["msg"] = msg return_value = GRADES_CALLBACK except: @@ -125,9 +127,10 @@ def callback(update: Update, context: CallbackContext): if "$back$" in call: subjects = context.user_data["subjects"] reply_markup = build_menu_of_subjects(subjects) - update.callback_query.message.edit_text( + msg = update.callback_query.message.edit_text( text=MESSAGES.Assignments.SELECT_COURSE, reply_markup=reply_markup ) + context.user_data["msg"] = msg return GRADES_CALLBACK assignments = context.user_data["assignments"] @@ -139,13 +142,15 @@ def callback(update: Update, context: CallbackContext): f = lambda ass: ass["name"] == name and ass["subject"] == subject assignment = next(filter(f, assignments)) - update.callback_query.message.edit_text( + msg = update.callback_query.message.edit_text( text=MESSAGES.Assignments.get(assignment['name'], assignment['points'], - assignment['how_to_display']), + assignment['how_to_display'], + assignment['notes']), parse_mode=ParseMode.HTML, reply_markup=reply_markup ) + context.user_data["msg"] = msg # Нажатие кнопок при выборе дисциплины if call.startswith("subject#"): @@ -158,9 +163,10 @@ def callback(update: Update, context: CallbackContext): context.user_data["has_back_button"] = True reply_markup = build_menu_of_assignments(assignments, has_back_button=True) - update.callback_query.message.edit_text( + msg = update.callback_query.message.edit_text( MESSAGES.Assignments.SELECT_ASSNT, reply_markup=reply_markup ) + context.user_data["msg"] = msg if call.startswith("cancel#"): update.callback_query.message.edit_text( @@ -171,21 +177,19 @@ def callback(update: Update, context: CallbackContext): return GRADES_CALLBACK def timeout(update: Update, context: CallbackContext): - update.callback_query.message.edit_text( + msg = context.user_data["msg"] + msg.edit_text( MESSAGES.Assignments.TIMEOUT, ) return ConversationHandler.END def grades_end(update: Update, context: CallbackContext): - update.message.edit_text( - MESSAGES.Assignments.TIMEOUT, - ) - update.message.reply_text( + msg = context.user_data["msg"] + msg.edit_text( MESSAGES.Assignments.END ) return ConversationHandler.END - def broadcast(update: Update, context: CallbackContext): """Команда: разослать студентам.""" access_message = update.message.reply_text("🔐 Проверка доступа...") @@ -270,12 +274,12 @@ def main() -> None: ConversationHandler.TIMEOUT: [ MessageHandler(Filters.text | Filters.command, timeout) ], - # ConversationHandler.END: [ - # MessageHandler(Filters.text | Filters.command, grades_end) - # ] + ConversationHandler.END: [ + MessageHandler(Filters.text | Filters.command, grades_end) + ] }, fallbacks=[CommandHandler("cancel", cancel)], - conversation_timeout=10, + #conversation_timeout=10, ) broadcast_handler = ConversationHandler( diff --git a/TelegramBot/messages/messages.py b/TelegramBot/messages/messages.py index 4b77ab2..daaa16f 100644 --- a/TelegramBot/messages/messages.py +++ b/TelegramBot/messages/messages.py @@ -25,13 +25,26 @@ class Messages: START = "👀 Посмотрим (~3с) ..." SELECT_COURSE = "👇 Выбери дисциплину" SELECT_ASSNT = "👇 Выбери работу" + END = "😉 Всего наилучшего" + TIMEOUT = "🕛 Время запроса вышло" @staticmethod - def get(grade, score, n_of_assignments): - summary = ( - f"Ранг: {score:.2f} {n_of_assignments} зад.\n" - + f"Оценка: {grade:.2f}" - ) + def get(name: str, assignment: list, how_to_display: str, notes: str): + is_float, n_cols, n_rows = how_to_display.split(',') + n_cols, n_rows = int(n_cols), int(n_rows) + summary = f'{name}:\n' + if is_float == 'z': + d = {0 : '❎', 1 : '✅'} + for item in range(0, len(assignment), n_cols): + summary += ''.join(list(map(d.get, + assignment[item : item + n_cols]))) + '\n' + elif is_float == 'r': + to_str = lambda x: f'{x:.2f}' + for item in range(0, len(assignment), n_cols): + summary += ' '.join(list(map(to_str, + assignment[item: item + n_cols]))) + '\n' + summary += f'Σ: {to_str(sum(assignment))}\n' + summary += f"\n{notes}" return summary @staticmethod diff --git a/TelegramBot/utils/inline_keyboard.py b/TelegramBot/utils/inline_keyboard.py index ebe9e31..b443c0c 100644 --- a/TelegramBot/utils/inline_keyboard.py +++ b/TelegramBot/utils/inline_keyboard.py @@ -20,6 +20,8 @@ def build_menu_of_assignments( ) -> InlineKeyboardMarkup: button_list = [] for assignment in assignments: + if not assignment['allow_to_display']: + continue if pressed_name and assignment["name"] == pressed_name: title = f'💁‍♀️ {assignment["name"]}' else: