def approve_lesson(lesson_id): lesson = current_user.teacher.lessons.filter_by(id=lesson_id).first() if not lesson: raise RouteError("Lesson does not exist", 404) # check if there isn't another lesson at the same time same_time_lesson = Appointment.query.filter( Appointment.approved_lessons_filter( Appointment.date == lesson.date, Appointment.id != lesson.id ) ).first() if same_time_lesson: raise RouteError("There is another lesson at the same time.") lesson.update(is_approved=True) if lesson.student.user.firebase_token: logger.debug(f"sending fcm for lesson approval") try: FCM.notify( token=lesson.student.user.firebase_token, title=gettext("Lesson Approved"), body=gettext( "Lesson at %(date)s has been approved!", date=format_datetime( lesson.date, locale=LOCALE, format="short", tzinfo=timezone(TIMEZONE), ), ), ) except NotificationError: pass return {"message": "Lesson approved."}
def total_lessons_price(cls): q = (select([coalesce(func.sum(Appointment.price), 0)]).where( Appointment.approved_lessons_filter( Appointment.date < datetime.utcnow(), Appointment.student_id == cls.id, )).label("total_lessons_price")) return q + cls.number_of_old_lessons * cls.price
def lessons_done(cls): q = select([ cast(func.sum(Appointment.duration), db.Float) / (func.count(Appointment.student_id) * Teacher.lesson_duration) ]).where( Appointment.approved_lessons_filter( Appointment.date < datetime.utcnow(), Appointment.student_id == cls.id)) j = Student.__table__.join(Teacher.__table__) q = q.select_from(j).label("lessons_done") return q + cls.number_of_old_lessons
def lessons_done(self) -> int: """return the number of a new lesson: num of latest lesson+1""" latest_lesson = (self.lessons.filter( Appointment.approved_lessons_filter( Appointment.date < datetime.utcnow())).order_by( Appointment.date.desc()).limit(1).one_or_none()) starting_count = self.number_of_old_lessons if not latest_lesson: return starting_count return latest_lesson.lesson_number
def total_lessons_price(self): return (sum(lesson.price for lesson in self.lessons.filter( Appointment.approved_lessons_filter( Appointment.date < datetime.utcnow())).all()) + self.price * self.number_of_old_lessons)