def attendance(): """ Display all lessons attendance can be recorded for. """ # Create new form objects for minimum and maximum dates. select_date_form = SelectMinMaxDateForm() if request.method == 'POST' and select_date_form.validate_on_submit(): # Form was submitted and is valid, filter by dates. no_attendance_recorded = select_lessons( current_user, attendance_recorded=False, min_date=select_date_form.min_date.data, max_date=select_date_form.max_date.data, order_by=Lesson.lesson_datetime.asc() ) else: # Select all lessons with recorded attendance. no_attendance_recorded = select_lessons( current_user, attendance_recorded=False, order_by=Lesson.lesson_datetime.asc() ) # Render the attendance template. return render_template( 'tutor/attendance.html', no_attendance_recorded=no_attendance_recorded, select_date_form=select_date_form )
def dashboard(): """ Tutor dashboard. """ # Select all of todays lessons. todays_lessons = select_lessons( current_user, min_date=datetime.now().date(), max_date=datetime.now().date()+timedelta(days=1), order_by=Lesson.lesson_datetime.asc() ) return render_template( 'tutor/dashboard.html', todays_lessons=todays_lessons )
def view_attendance(lesson_id): """ View attendance for a lesson. """ # Get the UserLessonAssociation for the current and # the given lesson id. (So we can also display attendance etc.) lesson = select_lessons(current_user, lesson_id=lesson_id, single=True) # Ensure the lesson id/association object is found. if not lesson: abort(404) # Render the view lesson template and pass in the association and the lesson object. return render_template( 'tutor/view_attendance.html', lesson=lesson, )
def record_attendance(lesson_id): """ Record attendance for a lesson. """ # Get the UserLessonAssociation for the current and # the given lesson id. (So we can also display attendance etc.) lesson = select_lessons(current_user, lesson_id=lesson_id, single=True) # Ensure the lesson id/association object is found. if not lesson: abort(404) record_single_attendance_form = RecordSingleAttendanceForm() if request.method == 'POST' and record_single_attendance_form.validate_on_submit(): assoc = UserLessonAssociation.query.filter( UserLessonAssociation.lesson_id == lesson_id ).filter( UserLessonAssociation.user_id == int(record_single_attendance_form.user_id.data) ).first() if assoc: assoc.attendance_code = record_single_attendance_form.attendance_code.data flash("Successfully updated lesson attendance.") else: abort(500) # We only want to send updates if they we're late or not there. if assoc.attendance_code == 'L' or assoc.attendance_code == 'N': # Send an email update. html = 'Attendance for your lesson on: ' + assoc.lesson.get_lesson_date() \ + ' has been updated. Your attendance is now recorded as: ' + \ assoc.get_lesson_attendance_str() # Send a lesson update. send_lesson_update( assoc.user, html, url_for( 'student.view_lesson', lesson_id=lesson_id, _external=True ), parent=True ) if check_attendance_complete(lesson): # The attendance is complete. lesson.update_lesson_details(attendance_recorded=True) else: lesson.update_lesson_details(attendance_recorded=False) # Save Changes db.session.commit() # Refresh return redirect(url_for('tutor.record_attendance', lesson_id=lesson_id)) # Render the view lesson template and pass in the association and the lesson object. return render_template( 'tutor/record_attendance.html', lesson=lesson, record_single_attendance_form=record_single_attendance_form )
def edit_lesson(lesson_id): """ Edit a lesson. """ # Find the lesson with the given ID. lesson = select_lessons(current_user, lesson_id=lesson_id, single=True) # If the lesson is not found abort. if not lesson: # HTTP not found. abort(404) # Create a new edit lesson form. edit_lesson_form = EditLessonForm() # Add the rooms. edit_lesson_form.lesson_room_id.choices = [ (room.room_id, room.get_location()) for room in Room.query.all() ] # Select all users. all_users = select_users_by_roles(('STU', 'TUT', 'STA')) # Set the choices for the users that can be selected for the new users. edit_lesson_form.add_users.choices = [ (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in all_users ] # Remove the current user. edit_lesson_form.add_users.choices.remove( (current_user.user_id, current_user.get_full_name() + " (" + current_user.get_role(pretty=True) + ")") ) # All the users that can be removed are the users of the lesson. edit_lesson_form.remove_users.choices = [ (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in lesson.users ] # Remove the current user from these choices (as they must still be in the lesson). edit_lesson_form.remove_users.choices.remove( (current_user.user_id, current_user.get_full_name() + " (" + current_user.get_role(pretty=True) + ")") ) if request.method == 'POST' and edit_lesson_form.validate_on_submit(): # Create the datetime object. lesson_datetime = datetime.combine( edit_lesson_form.lesson_date.data, time( edit_lesson_form.lesson_hour.data, edit_lesson_form.lesson_minute.data ) ) # Update the lesson. lesson.update_lesson_details( lesson_datetime=lesson_datetime, lesson_duration=edit_lesson_form.lesson_duration.data*60, lesson_notes=edit_lesson_form.lesson_notes.data, room_id=edit_lesson_form.lesson_room_id.data ) if app.config['UPDATE_ON_EDIT_LESSON']: # Iterate through the users and send updates. for user in lesson.users: if user.get_role() == 'STU': # Send an email update. html = 'Your lesson on: ' + lesson.get_lesson_date() + \ ' has been updated.' # Send a lesson update. send_lesson_update( user, html, url_for( 'student.view_lesson', lesson_id=lesson.lesson_id, _external=True ) ) # Iterate through the users to add. for user_id in edit_lesson_form.add_users.data: # Select the user object. user_object = select_user(user_id) # If the user is not already going to the lesson. if user_object not in lesson.users: # Append it to the lessons users. lesson.users.append(user_object) # Send an email update. html = 'You have been added to a lesson on: ' + lesson.get_lesson_date() # Send a lesson update. send_lesson_update( user_object, html, url_for( 'student.view_lesson', lesson_id=lesson.lesson_id, _external=True ) ) # Iterate through the users to remove. for user_id in edit_lesson_form.remove_users.data: # Delete the user lesson association for this user/lesson. db.session.delete( UserLessonAssociation.query.filter( UserLessonAssociation.lesson_id == lesson_id ).filter( UserLessonAssociation.user_id == user_id ).first() ) # Send an email update. html = 'You have been removed from the lesson on: ' + lesson.get_lesson_date() \ + ' this means your attendance is no longer required.' # Send a lesson update. send_lesson_update( User.query.filter(User.user_id == user_id).first(), html, url_for('student.lessons', _external=True) ) # Commit Changes. db.session.commit() # Flash a success message. flash("Successfully updated lesson.") # Set the defaults. edit_lesson_form.lesson_date.default = lesson.lesson_datetime.date() edit_lesson_form.lesson_notes.default = lesson.get_lesson_notes() edit_lesson_form.lesson_room_id.default = lesson.room.room_id # Process the form. edit_lesson_form.process() return render_template( 'tutor/edit_lesson.html', edit_lesson_form=edit_lesson_form, lesson=lesson )