def _moduleInfoListCallback(self, context, moduleInfo, eol, userData): if moduleInfo: module = Module(moduleInfo.contents) self.modules.insert(module.index, module) for ev in self.events['moduleInfoList']: ev(userData)
def List(module_id): """ Lists all sessions 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(module_id) if not module: return render_template('error/resource_not_found.html') # Get list of module sessions for given module sessions = ModuleSession.findBy('module', module_id) # Get a list of all teachers teachers = TeacherModel.all() # Get a list of all module session types session_types = ModuleSessionTypeModel.all() return render_template('session_list.html', data={ 'module': module, 'sessions': sessions, 'teachers': teachers, 'session_types': session_types })
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 List(student_id): """Returns all modules for given student.""" # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return render_template('error/no_access.html') student = StudentModel.findById(student_id) if not student: return render_template('error/resource_not_found.html') module_enrolments = StudentModuleModel.findBy('student', student_id) modules = [] module_id = [] for enrolment in module_enrolments: Module = ModuleModel.findById(enrolment.getModule()) modules.append(Module) module_id.append(Module.getId()) AllModules = ModuleModel.all() Available_Modules = [] for module in AllModules: if module.getId() not in module_id: Available_Modules.append(module) # get student timetable timetable = Timetable.getStudentTimetable(student_id, Timetable.getStartOfMonth(), Timetable.getEndOfMonth()) return render_template('enrolment_list.html', data={ "enroled_modules": modules, "available_modules": Available_Modules, "student": student, "timetable": timetable })
def Create(): """ Creates a new module. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this page. Contact system administrator.' }) # Parse and validate request body name = request.form.get('module_name') leader = request.form.get('leader') if not name or not leader: return JsonResponse.badRequest({ 'message': 'missing_parameters', 'nice_message': 'Please make sure you enter a name and leader.' }) # Make sure leader is a valid staff member if not TeacherModel.findById(leader): return JsonResponse.badRequest({ 'message': 'not_found', 'nice_message': 'Teacher not found' }) # Save new data to database module = ModuleModel() module.setName(name) \ .setLeader(leader) \ .save() return JsonResponse.ok()
def Create(module_id): """ Creates a new module session. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this page. Contact system administrator.' }) # Get module object module = ModuleModel.findById(module_id) if not module: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Module not found.' }) # Parse and validate request body teacher = request.form.get('teacher') sessionType = request.form.get('type') if not TeacherModel.findById(teacher): return JsonResponse.badRequest({ 'message': 'not_found', 'nice_message': 'Teacher not found.' }) if not ModuleSessionTypeModel.findById(sessionType): return JsonResponse.badRequest({ 'message': 'not_found', 'nice_message': 'Module session not found.' }) # Save new data to database moduleSession = ModuleSessionModel() moduleSession.setModule(module.getId()) \ .setStaff(teacher) \ .setType(sessionType) \ .save() return JsonResponse.ok()
def Update(id): """ Updates a given module. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this page. Contact system administrator.' }) # Get module object module = ModuleModel.findById(id) if not module: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Module not found.' }) # Save new values to database name = request.form.get('module_name') leader = request.form.get('leader') if not name and not leader: return JsonResponse.badRequest({ 'message': 'missing_parameters', 'nice_message': 'Please enter a new name or module leader.' }) if name: module.setName(name) if leader: if not TeacherModel.findById(leader): return JsonResponse.badRequest({ 'message': 'not_found', 'nice_message': 'Teacher not found.' }) module.setLeader(leader) module.save() return JsonResponse.ok()
def List(): """ Lists all modules. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return render_template('error/no_access.html') # Get list of all teachers teachers = TeacherModel.all() # Get list of modules modules = ModuleModel.all() return render_template('module_list.html', data={ 'modules': modules, 'teachers': teachers })
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 Delete(id): """ Deletes a given module. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this page. Contact system administrator.' }) # Get module object module = ModuleModel.findById(id) if not module: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Module not found.' }) # Delete from database module.delete() return JsonResponse.ok()
def CreateRecurringBooking(module_id, session_id): """ Updates a module session. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator' }) # Get session object theSession = ModuleSessionModel.findById(session_id) if not theSession: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Session not found.' }) module = ModuleModel.findById(theSession.getModule()) if not module: return JsonResponse.internalServerError({ 'message': 'not_found', 'nice_message': 'Module not found.' }) enrolments = StudentModuleModel.findBy('module', module.getId()) # Get post values building_id = request.form.get('building') duration = request.form.get('duration') day = request.form.get('day') frequency = request.form.get('frequency') term_id = request.form.get('term') if not building_id or not duration or not day or not frequency or not term_id: return JsonResponse.badRequest({ 'message': 'missing_parameters', 'nice_message': 'Please enter a building, duration, day, frequency and term' }) if day not in ('1', '2', '3', '4', '5'): return JsonResponse.badRequest({ 'message': 'bad_request', 'nice_message': 'Please choose a day Monday to Friday.' }) day = int(day) if frequency not in ('1', '2', '3', '4', '5', '6', '7', '8'): return JsonResponse.badRequest({ 'message': 'bad_request', 'nice_message': 'Please select a frequency.' }) frequency = int(frequency) duration = int(duration) if duration <= 0 or duration > 3: return JsonResponse.badRequest({ 'message': 'bad_requst', 'nice_message': 'Please select a duration betweene 1 and 3 hours.' }) building = CampusBuildingModel.findById(building_id) if not building: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Building not found.' }) term = TermModel.findById(term_id) if not term: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Term not found.' }) #def scheduleRecurring(term_id, session_id, building_id, day_of_week, frequency, duration, sessionType): res = Scheduler.scheduleRecurring(term.getId(), theSession.getId(), building.getId(), day, frequency, duration, theSession.getType()) #if not res: # return JsonResponse.badRequest() return JsonResponse.ok()
def CreateBooking(module_id, session_id): """ Creates a new room booking. """ # Verify user access if not Authorization.canAccess(session.get('user'), ('scheduling_admin')): return JsonResponse.unauthorized({ 'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact sytem administrator.' }) # Get session object theSession = ModuleSessionModel.findById(session_id) if not theSession: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Session not found.' }) module = ModuleModel.findById(theSession.getModule()) if not module: return JsonResponse.internalServerError({ 'message': 'not_found', 'nice_message': 'Module not found.' }) enrolments = StudentModuleModel.findBy('module', module.getId()) # Get post values building_id = request.form.get('building') duration = request.form.get('duration') day = request.form.get('day') hour = request.form.get('hour') duration = int(duration) if duration <= 0 or duration > 3: return JsonResponse.badRequest({ 'message': 'bad_request', 'nice_message': 'Please enter a duration between 1 and 3.' }) if not building_id or not duration or not day or not hour: return JsonResponse.badRequest({ 'message': 'missing_parameters', 'nice_message': 'Please ensure you enter a buildng, duration and time.' }) day = day + " 03:00" # Daylight savings time fix ts = int( datetime.strptime(day + " UTC", "%Y-%m-%d %H:%M %Z").strftime("%s")) building = CampusBuildingModel.findById(building_id) if not building: return JsonResponse.notFound({ 'message': 'not_found', 'nice_message': 'Building not found.' }) #def scheduleOneOff(building_id, duration, capacity, day, hour, sessionType = None, session = None): res = Scheduler.scheduleOneOff(building_id, duration, len(enrolments), ts, hour, theSession.getType(), theSession.getId()) if not res: return JsonResponse.badRequest({ 'message': 'booking_failed', 'nice_message': 'Booking not made.' }) return JsonResponse.ok()
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