Exemple #1
0
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
                           })
Exemple #2
0
def View(id):
    """ Returns teachers information """
    if not Authorization.canAccess(session.get('user'), ('admin')):
        return render_template('error/no_access.html')

    teachers = TeacherModel.findById(id)
    if not teachers:
        return render_template('error/resource_not_found.html')

    return render_template('teacher_view.html', data={'teacher': teachers})
Exemple #3
0
def Update(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
    session = ModuleSessionModel.findById(session_id)

    if not session:
        return JsonResponse.notFound({
            'message': 'not_found',
            'nice_message': 'Session not found.'
        })

    # Save new values to database
    teacher = request.form.get('teacher')
    sessionType = request.form.get('type')

    if not teacher and not sessionType:
        return JsonResponse.badRequest({
            'message':
            'missing_parameters',
            'nice_message':
            'Please enter a teacher and session type.'
        })

    if teacher:
        if not TeacherModel.findById(teacher):
            return JsonResponse.badRequest({
                'message': 'not_found',
                'nice_message': 'Teacher not found.'
            })

        session.setStaff(teacher)

    if sessionType:
        if not ModuleSessionModel.findById(sessionType):
            return JsonResponse.badRequest({
                'message':
                'not_found',
                'nice_message':
                'Session type not found.'
            })

        session.setType(sessionType)

    session.save()

    return JsonResponse.ok()
Exemple #4
0
def Update(id):
    """ Updates teachers information """
    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.'
        })

    first_name = request.form.get('first_name')
    last_name = request.form.get('last_name')
    email = request.form.get('email')
    mobile_phone = request.form.get('mobile_phone')
    password = request.form.get('password')

    if not first_name and not last_name and not email and not mobile_phone and not password:
        return JsonResponse.badRequest({
            'message':
            'missing_parameters',
            'nice_message':
            'Please enter details for the teacher.'
        })

    teachers = TeacherModel.findById(id)

    if not teachers:
        return JsonResponse.notFound({
            'message': 'not_found',
            'nice_message': 'Teacher not found.'
        })

    if first_name:
        teachers.setFirstName(first_name)

    if last_name:
        teachers.setLastName(last_name)

    if email:
        teachers.setEmail(email)

    if mobile_phone:
        teachers.setMobile(mobile_phone)

    if password:
        salt = teachers.getSalt()
        hashedPassword = Security.hashPassword(password, salt)
        teachers.setPassword(hashedPassword)

    try:
        teachers.save()
    except:
        return JsonResponse.badRequest({'error': 'database_error'})

    return JsonResponse.ok()
Exemple #5
0
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()
Exemple #6
0
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()
Exemple #7
0
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()
Exemple #8
0
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()
Exemple #9
0
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
                           })
Exemple #10
0
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