Добавил комментарии к домашкам

This commit is contained in:
faridik 2021-09-01 16:39:19 +03:00
parent aa474abeaa
commit 93b313690e
5 changed files with 70 additions and 27 deletions

View File

@ -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
)
)

View File

@ -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

View File

@ -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(

View File

@ -25,13 +25,26 @@ class Messages:
START = "👀 Посмотрим (~3с) ..."
SELECT_COURSE = "👇 Выбери дисциплину"
SELECT_ASSNT = "👇 Выбери работу"
END = "😉 Всего наилучшего"
TIMEOUT = "🕛 Время запроса вышло"
@staticmethod
def get(grade, score, n_of_assignments):
summary = (
f"Ранг: <b>{score:.2f}</b> {n_of_assignments} зад.\n"
+ f"Оценка: <b>{grade:.2f}</b>"
)
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'<b>{name}</b>:\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

View File

@ -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: