def edit_lesson(lesson_id): """ Edit a lesson. """ # 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 ] # Find the lesson with the given ID. lesson = Lesson.query.filter(Lesson.lesson_id == lesson_id).first() # 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 ] 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( 'staff/edit_lesson.html', edit_lesson_form=edit_lesson_form, 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 = Lesson.query.filter(Lesson.lesson_id == lesson_id).first() # 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('staff.record_attendance', lesson_id=lesson_id)) # Render the view lesson template and pass in the association and the lesson object. return render_template( 'staff/record_attendance.html', lesson=lesson, record_single_attendance_form=record_single_attendance_form )
def add_lesson(): """ Add a new lesson. """ # Create empty error object. error = None # Create form. add_lesson_form = AddLessonForm() # Add the rooms. add_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')) # Update the form choices. add_lesson_form.users.choices = [ (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in all_users ] if request.method == 'POST' and add_lesson_form.validate_on_submit(): # Create a new lesson object. new_lesson = Lesson() # Create the datetime object. lesson_datetime = datetime.combine( add_lesson_form.lesson_date.data, time( add_lesson_form.lesson_hour.data, add_lesson_form.lesson_minute.data ) ) # Update the lesson details. new_lesson.update_lesson_details( lesson_datetime=lesson_datetime, lesson_duration=add_lesson_form.lesson_duration.data*60, lesson_notes=add_lesson_form.lesson_notes.data, room_id=add_lesson_form.lesson_room_id.data ) # Iterate through the users. for user_id in add_lesson_form.users.data: # Select the user object. user_object = select_user(user_id) # Append it to the lessons users. new_lesson.users.append(user_object) # Send an update. if app.config['UPDATE_ON_NEW_LESSON']: # Send an email update. html = 'A new lesson has been created on: ' + new_lesson.get_lesson_date() # Send a lesson update. send_lesson_update( user_object, html, url_for('student.lessons', _external=True) ) # Flash a success message. flash("Successfully added new lesson.") # Add the lesson to the db. db.session.add(new_lesson) # Commit changes. db.session.commit() return redirect(url_for('staff.add_lesson')) return render_template( 'staff/add_lesson.html', add_lesson_form=add_lesson_form, error=error )