def View(id): """ Gets information for given module. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return render_template('error/no_access.html') # Get module object module = ModuleModel.findById(id) if not module: return render_template('error/resource_not_found.html') # Get leader (teacher) object leader = TeacherModel.findById(module.getLeader()) if not module: return render_template('error/server_error.html') # Get teachers object teachers = TeacherModel.all() # Get enrolled students students = [] enrolments = StudentModuleModel.findBy('module', id) for enrolment in enrolments: students.append(StudentModel.findById(enrolment.getStudent())) # Get module sessions (+ teachers) sessions = ModuleSessionModel.findBy('module', id) sessions_list = [] for session2 in sessions: sessions_list.append({ 'session': session2, 'staff': TeacherModel.findById(session2.getStaff()), 'type': ModuleSessionTypeModel.findById(session2.getType()) }) # Get session types sessionTypes = ModuleSessionTypeModel.all() return render_template('module_view.html', data={ 'module': module, 'leader': leader, 'teachers': teachers, 'students': students, 'sessionTypes': sessionTypes, 'sessions': sessions_list })
def isTeacherAvailable(teacher_id, ts_from, ts_to): sessions = ModuleSessionModel.findBy('staff', teacher_id) for session in sessions: bookings = RoomBookingModel.findBy('module_session', session.getId()) for booking in bookings: if hasClash(ts_from, ts_to, booking.getTimeFrom(), booking.getTimeTo()): return False return True
def isAvailable(session, teacher, day_of_week, frequency, duration, sessionType, startHour, maxStartTime): # Check teacher availability teacher_sessions = ModuleSessionModel.findBy('staff', teacher.getId()) for session in teacher_sessions: session_bookings = RoomBookingModel.findBy('module_session', session.getId()) for booking in session_bookings: if hasClash(startHour, startHour + duration, booking.getTimeFrom(), booking.getTimeTo()): return False
def isStudentAvailable(student_id, ts_from, ts_to): enrolments = StudentModuleModel.findBy('student', student_id) modules = [] for enrolment in enrolments: modules.append(ModuleModel.findById(enrolment.getModule())) for module in modules: sessions = ModuleSessionModel.findBy('module', module.getId()) for session in sessions: bookings = RoomBookingModel.findBy('module_session', session.getId()) for booking in bookings: if hasClash(ts_from, ts_to, booking.getTimeFrom(), booking.getTimeTo()): return False return True
def Delete(id): """ Deletes teacher """ if not Authorization.canAccess(session.get('user'), ('admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator' }) teachers = TeacherModel.findById(id) if not teachers: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Teacher not found.' }) modules = ModuleModel.findBy('leader', id) if len(modules) != 0: return JsonResponse.badRequest({ 'message': 'bad_request', 'nice_message': 'Modules exist with this teacher as module leader.' }) sessions = ModuleSessionModel.findBy('staff', id) if len(sessions) != 0: return JsonResponse.badRequest({ 'message': 'bad_request', 'nice_message': 'Sessions exist for this teacher.' }) teachers.delete() return JsonResponse.ok()
def bookRoomOnDay(theStartHour, day, session, teacher, building, duration, sessionType): # Check teacher availability if not isTeacherAvailable( teacher, getTimestampGivenDayAndHour(day, theStartHour), getTimestampGivenDayAndHour(day, theStartHour + duration)): return None # Check student availability session = ModuleSessionModel.findById(session) module = ModuleModel.findById(session.getModule()) enrolments = StudentModuleModel.findBy('module', module.getId()) for enrolment in enrolments: if not isStudentAvailable( enrolment.getStudent(), getTimestampGivenDayAndHour(day, theStartHour), getTimestampGivenDayAndHour(day, theStartHour + duration)): return None # Check room availability room = getAvailableRoom( building, len(enrolments), session.getType(), getTimestampGivenDayAndHour(day, theStartHour), getTimestampGivenDayAndHour(day, theStartHour + duration)) if not room: return None booking = RoomBookingModel() booking.setRoom(room.getId()) \ .setTimeFrom(getTimestampGivenDayAndHour(day, theStartHour)) \ .setTimeTo(getTimestampGivenDayAndHour(day, theStartHour + duration)) \ .setModuleSession(session.getId()) \ .save() return booking
def scheduleRecurring(term_id, session_id, building_id, day_of_week, frequency, duration, sessionType): maxStartTime = dayEnd - duration # Sanity check if duration > maxSessionDuration: raise Error() roomBookings = [] bookingFailures = [] # get session session = ModuleSessionModel.findById(session_id) teacher_id = session.getStaff() # Get term and dates term = TermModel.findById(term_id) termStartDate = term.getStartDate() termEndDate = term.getEndDate() firstDay = getFirstDayOfWeekAfterDate(termStartDate, day_of_week) sessionDays = getDays(firstDay, termEndDate, frequency) # Every X weeks for day in sessionDays: for theStartHour in list(closed_range(dayStart, dayEnd - duration)): res = bookRoomOnDay(theStartHour, day, session_id, teacher_id, building_id, duration, sessionType) if res: break if res: roomBookings.append(res) else: bookingFailures.append(day) return {'bookings': roomBookings, 'failures': bookingFailures}
def View(module_id, session_id): """ Returns module session information. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return render_template('error/no_access.html') # Get module object module = ModuleModel.findById(module_id) if not module: return render_template('error/resource_not_found.html') # Get session object theSession = ModuleSessionModel.findById(session_id) if not theSession: return render_template('error/resource_not_found.html') # Get the campus objects campus_list = CampusModel.all() campuses = [] for campus in campus_list: buildings = CampusBuildingModel.findBy('campus', campus.getId()) if (len(buildings) > 0): campuses.append({'campus': campus, 'buildings': buildings}) theSession = { 'session': theSession, 'staff': TeacherModel.findById(theSession.getStaff()), 'type': ModuleSessionTypeModel.findById(theSession.getType()), 'campuses': CampusModel.all() } # Get list of teachers teachers = TeacherModel.all() # Get list of session types session_types = ModuleSessionTypeModel.all() # Get terms terms = TermModel.all() term_list = [] for term in terms: term_list.append({ 'term': term, 'startDate': datetime.utcfromtimestamp(term.getStartDate()).strftime("%B %Y"), 'endDate': datetime.utcfromtimestamp(term.getEndDate()).strftime("%B %Y") }) # Get list of room bookings room_bookings = RoomBookingModel.findBy('module_session', session_id) room_bookings2 = [] for booking in room_bookings: room = CampusBuildingRoomModel.findById(booking.getRoom()) building = CampusBuildingModel.findById(room.getBuilding()) campus = CampusModel.findById(building.getCampus()) timeFrom = datetime.utcfromtimestamp( booking.getTimeFrom()).strftime("%Y-%m-%d %H:%M") timeTo = datetime.utcfromtimestamp( booking.getTimeTo()).strftime("%Y-%m-%d %H:%M") room_bookings2.append({ 'booking': booking, 'room': room, 'building': building, 'campus': campus, 'timeFrom': timeFrom, 'timeTo': timeTo }) return render_template('session_view.html', data={ 'module': module, 'session': theSession, 'teachers': teachers, 'sessionTypes': session_types, 'roomBookings': room_bookings2, 'campuses': campuses, 'terms': term_list })
def scheduleOneOff(building_id, duration, capacity, day, hour, sessionType=None, session=None): # getTimestampGivenDayAndHour(day, theStartHour) # getTimestampGivenDayAndHour(day, theStartHour + duration) duration = int(duration) capacity = int(capacity) day = int(day) hour = int(hour) sessionType = int(sessionType) session = int(session) timedate = getTimestampGivenDayAndHour(day, hour) if duration > maxSessionDuration: raise Error() # Book Room for session if session: # Get session and module object session = ModuleSessionModel.findById(session) module = ModuleModel.findById(session.getModule()) # Check teacher availability teacher = TeacherModel.findById(session.getStaff()) if not isTeacherAvailable(teacher.getId(), timedate, timedate + hr2sec(duration)): return None # Check student availability enrolments = StudentModuleModel.findBy('module', module.getId()) for enrolment in enrolments: if not isStudentAvailable(enrolment.getStudent(), timedate, timedate + hr2sec(duration)): return None # Get room room = getAvailableRoom(building_id, len(enrolments), session.getType(), timedate, timedate + hr2sec(duration)) if not room: return None roomBooking = RoomBookingModel() roomBooking.setRoom(room.getId()) \ .setTimeFrom(timedate) \ .setTimeTo(timedate + hr2sec(duration)) \ .setModuleSession(session.getId()) \ .save() return roomBooking # Book Room for not session if not session: # Get free room that fits the requirements room = getAvailableRoom(building_id, capacity, sessionType, timedate, timedate + hr2sec(duration)) if not room: raise None roomBooking = RoomBookingModel() roomBooking.setRoom(room.getId()) \ .setTimeFrom(timedate) \ .setTimeTo(timedate + hr2sec(duration)) \ .save() return roomBooking return None # fail safe