Exemplo n.º 1
0
def list_of_adding_dates():
    hour = tm().hour + 3

    if 15 <= hour:
        return [cur_date(d) for d in [1, 2, 3, 4]]

    return [cur_date(d) for d in [0, 1, 2, 3, 4]]
Exemplo n.º 2
0
def get_schedule(src, user_id, text):
    logger.log("user_req", f"getting schedule in {text}")

    class_name = get_class_name_from_text(text.upper())
    dates = [cur_date(), cur_date(1)]

    return get_schedule_from_class(class_name,
                                   dates,
                                   add_room=True,
                                   add_teacher=True)
Exemplo n.º 3
0
    def get_schedule(self, class_name, date):
        if date is None:
            date = cur_date()

        class_id = self.get_class_id(class_name)
        columns = self.get_columns_names()
        lessons = {}

        with self.run_cursor() as cursor:
            for lesson_num in range(0, 10):  # максимально 10 уроков
                query = f"""
                    SELECT * FROM lessons WHERE class_id = {class_id} AND date = '{date}' AND number = {lesson_num}
                """
                cursor.execute(query)
                lesson_arr = cursor.fetchall()

                if not lesson_arr:
                    continue

                for it in range(len(lesson_arr)):
                    tmp = convert_arrays_to_dict(columns, lesson_arr[it])
                    if not tmp:
                        logger.log("lessons_db_manip", f"Длины разные!!!")
                        continue
                    lesson_arr[it] = tmp
                    del tmp

                lessons[lesson_num] = lesson_arr

        logger.log(
            "lessons_db_manip",
            f"returning schedule for class {class_name}, date {date}: len - {len(lessons)}"
        )
        return lessons
Exemplo n.º 4
0
def fast_schedule(src, user_id, text):
    info = user_db.get_user_info(user_id, src)
    ans_msg = ""
    user_subs = info['subs']

    if len(user_subs) == 0:
        return {
            "text": "Вы не подписаны ни на один из классов.",
            "buttons": menu_buttons
        }

    list_of_dates = [cur_date(), cur_date(1), cur_date(2)]

    for c, subs in user_subs.items():
        ans_msg += f"\nКласс {c}:\n"
        ans_msg += get_schedule_from_subs(c, subs, list_of_dates)

    return {"text": ans_msg, "buttons": menu_buttons}
Exemplo n.º 5
0
def get_schedule_from_subs(class_name,
                           user_subs,
                           list_of_dates,
                           a_room=False,
                           a_teacher=False,
                           a_homework=False):
    if list_of_dates is None:
        list_of_dates = [cur_date()]

    answer_string = ""
    for date in list_of_dates:
        temp = lesson_db.get_schedule_by_subs(class_name, date, user_subs)

        if not temp:
            lesson_db.add_schedule(class_name, date)

        day_schedule = lesson_db.get_schedule_by_subs(class_name, date,
                                                      user_subs)

        answer_string += f"{make_lined(get_word_by_date(date), symbol='̲')}:\n\n"

        if not day_schedule:
            answer_string += f"Уроков (в моей базе) нет.\n"
            continue

        for lesson_num, lesson in day_schedule.items():
            tmp = []
            for it in range(len(lesson)):
                tmp.append(f"{lesson[it]['name']}")
                if a_homework or a_room or a_teacher:
                    tmp[it] += "\n"

                if a_room and lesson[it]['room'] is not None:
                    tmp[it] += f"Кабинет: {lesson[it]['room']}\n"

                if a_teacher and lesson[it]['teacher'] is not None:
                    tmp[it] += f"Учитель: {lesson[it]['teacher']}\n"

                if a_homework and lesson[it]['homework'] is not None:
                    tmp[it] += f"Домашнее задание: \n{lesson[it]['homework']}\n"

                if lesson[it]['comment'] is not None:
                    tmp[it] += f"Комментарий: ({lesson[it]['comment']})\n"

            if a_homework or a_room or a_teacher:
                answer_string += "\n"

            answer_string += f"{lesson_num}. {'/'.join(tmp).rstrip()}.\n"
        answer_string += '\n'

    return answer_string
Exemplo n.º 6
0
    def get_schedule_by_subs(self, class_name, date, user_subs):
        logger.log(
            "lessons_db_manip",
            f"getting schedule by subs class: {class_name}, date: {date}, subs: {user_subs}"
        )

        if date is None:
            date = cur_date()

        class_id = self.get_class_id(class_name)
        columns = self.get_columns_names()
        lessons = {}

        with self.run_cursor() as cursor:
            for lesson_num in range(0, 10):  # максимально 10 уроков
                query = f"""
                    SELECT * FROM lessons WHERE class_id = {class_id} AND date = '{date}' AND number = {lesson_num}
                """
                cursor.execute(query)
                lesson_arr = cursor.fetchall()

                if not lesson_arr:
                    continue

                ans_arr = []
                for it in range(len(lesson_arr)):
                    if not lesson_arr[it]:
                        continue

                    tmp = convert_arrays_to_dict(columns, lesson_arr[it])

                    name = tmp['name']
                    if tmp.get('grp', None) is not None and user_subs.get(
                            name, []):
                        if tmp['grp'] not in user_subs[name]:
                            tmp = None

                    if not tmp:
                        continue

                    ans_arr.append(tmp)
                    del tmp

                lessons[lesson_num] = ans_arr
        return lessons
Exemplo n.º 7
0
def get_schedule_from_class(class_name,
                            list_of_dates=None,
                            add_room=False,
                            add_teacher=False):
    if list_of_dates is None:
        list_of_dates = [cur_date()]

    answer_string = ""
    for date in list_of_dates:

        temp = lesson_db.get_schedule(class_name, date)

        if not temp:
            lesson_db.add_schedule(class_name, date)

        day_schedule = lesson_db.get_schedule(class_name, date)

        answer_string += f"{get_word_by_date(date)}:\n"

        if not day_schedule:
            answer_string += f"Уроков (в моей базе) нет.\n"
            continue

        for lesson_num, lesson in day_schedule.items():
            tmp = []
            for it in range(len(lesson)):
                tmp.append(f"{lesson[it]['name']}")

                if add_room and lesson[it]['room'] is not None:
                    tmp[it] += f", кабинет: {lesson[it]['room']}"

                if add_teacher and lesson[it]['teacher'] is not None:
                    tmp[it] += f", учитель: {lesson[it]['teacher']}"

                if lesson[it]['comment'] is not None:
                    tmp[it] += f" ({lesson[it]['comment']})"

            answer_string += f"{lesson_num}. {'/'.join(tmp)}.\n"
    return answer_string
Exemplo n.º 8
0
 def list_of_dates(self):
     return [cur_date(a) for a in self.list_of_adds]
Exemplo n.º 9
0
 def get_beautified_lessons_for_desk(self, number=1, date=cur_date()):
     return self.add_field_class_name_to_list_of_lessons(
         self.get_lessons_by_date_and_lesson_number(date, number))
Exemplo n.º 10
0
    def add_lesson(self, lesson, homework, class_id, day_name, date):
        lesson_name = lesson["name"]
        lesson_num = lesson["num"]

        if lesson_name == "Cancel":
            lesson_name = "Урок отменен!"

        lesson_room = "NULL"
        lesson_teacher = "NULL"
        lesson_grp = None
        lesson_homework = None

        if lesson["room"]:
            lesson_room = f"'{lesson['room']}'"

        if lesson["teacher"]:
            lesson_teacher = f"'{lesson['teacher']}'"

        if "grp" in lesson:
            lesson_grp = f"{lesson['grp']}"
            self.add_class_groups(class_id, lesson_name, lesson["grp"])

        if homework:
            temp_homework = self.parse_homework(homework[date], lesson_name,
                                                lesson_grp)
            if temp_homework is not None:
                lesson_homework = f"{del_op(temp_homework).rstrip()}"

        grp_string = self.parse_string_in_query(lesson_grp)

        grp_add = ""
        if grp_string != "NULL":
            grp_add = f"AND grp = {grp_string}"

        old_lessons = self.get_lesson(class_id,
                                      date,
                                      lesson_num,
                                      grp=lesson_grp)

        if old_lessons and len(old_lessons) > 1:
            for old_les in old_lessons:
                if old_les['name'] == lesson_name:
                    old_lessons = [old_les]
                    break
            else:
                old_lessons = []

        if old_lessons and old_lessons[0]['homework']:
            old_lessons[0]['homework'] = old_lessons[0]['homework'].strip()

        homework_changed = old_lessons and lesson_homework and lesson_homework != old_lessons[
            0]['homework']

        if homework_changed and old_lessons[0]["is_updated"]:
            if date == cur_date(1):
                is_unsent = 1
            else:
                is_unsent = 0

            with self.run_cursor() as cursor:
                query = f"""
                    UPDATE lessons SET
                    homework = {self.parse_string_in_query(lesson_homework)},
                    unsent_homework = {is_unsent} WHERE 
                    class_id = '{class_id}'
                    AND date = '{date}' 
                    AND number = '{lesson_num}'
                    AND name = '{old_lessons[0]['name']}'   
                    {grp_add}
                """
                cursor.execute(query)

                logger.log(
                    "lessons_db_manip",
                    f"changed homework {class_id}, {date}, {lesson_num} "
                    f"from {old_lessons[0]['homework']} to {lesson_homework}")

        if old_lessons and lesson_name != old_lessons[0]['name'] and "Урок отменен!" not in old_lessons[0]['name'] \
                and old_lessons[0]["is_updated"]:
            new_lesson_name = f"{make_lined(old_lessons[0]['name'])} {lesson_name}"

            with self.run_cursor() as cursor:
                query = f"""
                    UPDATE lessons SET name = '{new_lesson_name}', unsent_change = 1 WHERE 
                    class_id = '{class_id}'
                    AND date = '{date}' 
                    AND number = '{lesson_num}'
                    AND name = '{old_lessons[0]['name']}'
                    {grp_add}
                """

                cursor.execute(query)

        if old_lessons and old_lessons[0]["is_updated"]:
            return

        lesson_homework = self.parse_string_in_query(lesson_homework)
        with self.run_cursor() as cursor:
            query = f"""
                INSERT INTO lessons 
                (name, number, class_id, day_of_week, room, teacher,
                 date, grp, comment, unsent_change, unsent_homework, is_updated, homework)
                VALUES (
                '{lesson_name}', {lesson_num}, {class_id}, '{day_name}', {lesson_room},
                {lesson_teacher}, {date}, {grp_string}, NULL, 0, 0, 0, {lesson_homework})
            """
            cursor.execute(query)