示例#1
0
def test_find_found_in_id_map_found_in_DB(monkeypatch):
    # Test Data
    unexpected = Timeslot(11, 52, 'date2', 'block2', 'id2', 'id2')
    expected = Timeslot(1, 5, 'date', 'block', 'id', 'id')

    # Mock
    def id_find(_, __):
        return expected

    def tdg_find(_):
        return [[
            unexpected.getId(),
            unexpected.getStartTime(),
            unexpected.getEndTime(),
            unexpected.getDate(),
            unexpected.getBlock(),
            unexpected.getId()
        ]]

    monkeypatch.setattr(IdMap, 'find', id_find)
    monkeypatch.setattr(TimeslotTDG, 'find', tdg_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val.getStartTime() is expected.getStartTime())
    assert (val.getEndTime() is expected.getEndTime())
    assert (val.getDate() is expected.getDate())
    assert (val.getBlock() is expected.getBlock())
    assert (val.getId() is expected.getId())
示例#2
0
    def find_total_reserved_time_for_user_for_a_given_week(self, user_id, date):
        diff_between_monday_and_sunday = 6
        total_time = 0
        # filter date values
        date_split_list = date.split('/')
        year = int(date_split_list[0])
        month = int(date_split_list[1])
        day = int(date_split_list[2])

        # Create datetime object
        reservation_date = datetime(year, month, day)

        # find start of the week
        monday_date = reservation_date - timedelta(days=reservation_date.weekday())

        sunday_date = monday_date + timedelta(days=diff_between_monday_and_sunday)

        user_timeslot_list = TimeslotMapper.find_all_timeslots_for_user(user_id)
        for timeslot in user_timeslot_list:

            reservation_date = datetime(timeslot[3].year, timeslot[3].month, timeslot[3].day)

            # check if reservation_date lies between monday and sunday.
            if monday_date < reservation_date < sunday_date:
                total_time += timeslot[4]

        return total_time
示例#3
0
def test_find_not_found_in_id_map_found_in_DB(monkeypatch):
    # Test Data
    expected = Timeslot(1, 5, 'date', 'block', 'id', 'id')

    # Mock
    def no_find(_, __):
        return

    def yes_find(_):
        return [[
            expected.getId(),
            expected.getStartTime(),
            expected.getEndTime(),
            expected.getDate(),
            expected.getBlock(),
            expected.getId(),
            expected.getId()
        ]]

    monkeypatch.setattr(IdMap, 'find', no_find)
    monkeypatch.setattr(TimeslotTDG, 'find', yes_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val.getStartTime() is expected.getStartTime())
    assert (val.getEndTime() is expected.getEndTime())
    assert (val.getDate() is expected.getDate())
    assert (val.getBlock() is expected.getBlock())
    assert (val.getId() is expected.getId())
示例#4
0
def find(reservationId):
    result = ReservationTDG.find(reservationId)
    if not result:
        return None
    else:
        # must make a reference to timeslottable and create a timeslot object
        room = RoomMapper.find(result[0][1])
        holder = UserMapper.find(result[0][3])
        timeslot = TimeslotMapper.find(result[0][4])
        equipment = EquipmentMapper.find(result[0][5])
        return Reservation(room, holder, timeslot, result[0][2], equipment, result[0][0])
示例#5
0
    def delete_reservation(self, reservationId):
        reservation = ReservationMapper.find(reservationId)
        waiting = WaitingMapper.find(reservationId)
        if not reservation and not waiting:
            response = jsonify({'reservation error': 'that reservationId does not exist in any list'})
            response.status_code = STATUS_CODE['NOT_FOUND']
            return response

        if reservation:
            timeslotId = reservation.getTimeslot().getId()
            equipmentId = reservation.getEquipment().getId()
            ReservationMapper.delete(reservationId)
            ReservationMapper.done()
            data = {
                'success': 'reservation successfully deleted',
                'reservationId': reservationId
            }

        if waiting:
            timeslotId = waiting.getTimeslot().getId()
            equipmentId = waiting.getEquipment().getId()
            WaitingMapper.delete(reservationId)
            WaitingMapper.done()
            data = {
                'success': 'reservation on waiting list successfully deleted',
                'waitingId': reservationId
            }

        TimeslotMapper.delete(timeslotId)
        TimeslotMapper.done()
        EquipmentMapper.delete(equipmentId)
        EquipmentMapper.done()

        self.update_internal_lists()
        self.update_waiting_to_reserved()
        self.update_internal_lists()

        return jsonify(data)
示例#6
0
def findAll():
    result = ReservationTDG.findAll()
    allReservations = []
    if not result:
        return []
    else:
        for index, r in enumerate(result):
            room = RoomMapper.find(result[index][1])
            holder = UserMapper.find(result[index][3])
            timeslot = TimeslotMapper.find(result[index][4])
            equipment = EquipmentMapper.find(result[index][5])
            reservation = Reservation(room, holder, timeslot, result[index][2], equipment, result[index][0])
            allReservations.append(reservation)
        return allReservations
示例#7
0
def test_find_not_found_in_id_map_not_found_in_DB(monkeypatch):
    # Mock
    def no_find(_):
        return

    def no_find2(_, __):
        return

    monkeypatch.setattr(IdMap, 'find', no_find2)
    monkeypatch.setattr(TimeslotTDG, 'find', no_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val is None)
示例#8
0
    def make_new_reservation(self, data):
        startTime = int(data['timeslot']['startTime'])
        endTime = int(data['timeslot']['endTime'])
        date = str(data['timeslot']['date'])
        dateList = date.split('/')
        roomId = data['roomId']
        username = data['username']
        description = str(data['description'])
        laptop = int(data['equipment']['laptop'])
        board = int(data['equipment']['board'])
        projector = str(data['equipment']['projector'])

        if not UserMapper.find(username) or not RoomMapper.find(roomId):
            response = jsonify({'makeNewReservation error': 'Either the room or user does not exist.'})
            response.status_code = STATUS_CODE['NOT_FOUND']
            return response

        # no use for `block` parameter, for now, just passing empty strin
        timeslot = TimeslotMapper.makeNew(startTime, endTime, datetime(int(dateList[0]), int(dateList[1]), int(dateList[2])), '', username, str(uuid4()))
        room = RoomMapper.find(roomId)
        user = UserMapper.find(username)
        equipment = EquipmentMapper.makeNew(laptop, projector, board, str(uuid4()))


        duration = int(timeslot.getEndTime()) - int(timeslot.getStartTime())
        if self.find_total_reserved_time_for_user_for_a_given_week(user.getId(), timeslot.getDate().strftime('%Y/%m/%d')) + duration > 3:
            TimeslotMapper.delete(timeslot.getId())
            EquipmentMapper.delete(equipment.getId())
            TimeslotMapper.done()
            EquipmentMapper.done()
            response = jsonify({'error': 'You have already booked for your maximum amount of time this week. Aborting.'})
            response.status_code = STATUS_CODE['UNPROCESSABLE']
            return response

        TimeslotMapper.done()
        EquipmentMapper.done()

        if not self.isTimeslotAvailableforRoom(room, timeslot):
            return jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is a timeslot conflict: added to the waiting list'))

        if not self.isEquipmentAvailableForTimeSlot(timeslot, equipment):
            return jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is not enough equipment: added to the waiting list'))

        # Successfully adding a reservation
        return jsonify(self.commit_new_reservation(room, user, timeslot, description, equipment))
示例#9
0
    def make_new_repeated_reservation(self, data, repeats):
        startTime = int(data['timeslot']['startTime'])
        endTime = int(data['timeslot']['endTime'])
        date = str(data['timeslot']['date'])
        dateList = date.split('/')
        roomId = data['roomId']
        username = data['username']
        description = str(data['description'])
        laptop = int(data['equipment']['laptop'])
        board = int(data['equipment']['board'])
        projector = str(data['equipment']['projector'])

        if not UserMapper.find(username) or not RoomMapper.find(roomId):
            response = jsonify({'makeNewReservation error': 'Either the room or user does not exist.'})
            response.status_code = STATUS_CODE['NOT_FOUND']
            return response

        repeats = int(repeats)
        max_repetition = 2
        days_in_a_week = 7

        # no use for `block` parameter, for now, just passing empty strin
        room = RoomMapper.find(roomId)
        user = UserMapper.find(username)
        timeslot = TimeslotMapper.makeNewWithoutCommit(startTime, endTime, datetime(int(dateList[0]), int(dateList[1]), int(dateList[2])), '', username, str(uuid4()))

        # safe guard if repeat amount is greater than max repetition
        if int(repeats) > int(max_repetition):
            repeats = int(max_repetition)

        date_split_list = timeslot.getDate().strftime('%Y/%m/%d').split('/')
        year = int(date_split_list[0])
        month = int(date_split_list[1])
        day = int(date_split_list[2])
        reservation_date = datetime(year, month, day)

        reservations_created = []
        waitings_created = []

        # repeatAmount + 1 : because at least 1 reservation should be made
        for i in range(repeats + 1):
            new_date_list = reservation_date.strftime('%Y/%m/%d').split('/')
            new_date_list = [int(elem) for elem in new_date_list]
            timeslot = TimeslotMapper.makeNew(timeslot.getStartTime(), timeslot.getEndTime(), datetime(*new_date_list),
                                              timeslot.getBlock(), username, str(uuid4()))
            equipment = EquipmentMapper.makeNew(laptop, projector, board, str(uuid4()))

            duration = int(timeslot.getEndTime()) - int(timeslot.getStartTime())
            if self.find_total_reserved_time_for_user_for_a_given_week(user.getId(), timeslot.getDate().strftime('%Y/%m/%d')) + duration > 3:
                TimeslotMapper.delete(timeslot.getId())
                EquipmentMapper.delete(equipment.getId())
                TimeslotMapper.done()
                EquipmentMapper.done()
                response = jsonify({'error': 'You have already booked for your maximum amount of time this week. Aborting.'})
                response.status_code = STATUS_CODE['UNPROCESSABLE']
                return response

            TimeslotMapper.done()
            EquipmentMapper.done()

            if not self.isTimeslotAvailableforRoom(room, timeslot):
                result = self.commit_new_waiting(room, user, timeslot, description, equipment,
                                                 'There is a timeslot conflict: added to the waiting list', http_response=False)
                waitings_created.append(result.to_dict())

            elif not self.isEquipmentAvailableForTimeSlot(timeslot, equipment):
                result = self.commit_new_waiting(room, user, timeslot, description, equipment,
                                                 'There is not enough equipment: added to the waiting list', http_response=False)
                waitings_created.append(result.to_dict())

            else:
                # Successfully adding a reservation
                result = self.commit_new_reservation(room, user, timeslot, description, equipment, http_response=False)
                reservations_created.append(result.to_dict())

            # add a week to the current reservation date
            reservation_date += timedelta(days=days_in_a_week)

        response_data = {
            'success': 'You have successfully repeated your reservations.',
            'reservations': reservations_created,
            'waitings': waitings_created
        }
        return jsonify(response_data)
示例#10
0
    def modify_reservation(self, data, reservationId):
        startTime = int(data['timeslot']['startTime'])
        endTime = int(data['timeslot']['endTime'])
        date = str(data['timeslot']['date'])
        dateList = date.split('/')
        roomId = data['roomId']
        username = data['username']
        description = str(data['description'])
        laptop = int(data['equipment']['laptop'])
        board = int(data['equipment']['board'])
        projector = str(data['equipment']['projector'])

        old_booking = ReservationMapper.find(reservationId)
        if not old_booking:
            old_booking = WaitingMapper.find(reservationId)
            if not old_booking:
                response = jsonify({'makeNewReservation error': 'The specified reservation or waiting to modify does not exist.'})
                response.status_code = STATUS_CODE['NOT_FOUND']
                return response
            type = 'waiting'
        else:
            type = 'reservation'

        if not UserMapper.find(username) or not RoomMapper.find(roomId):
            response = jsonify({'makeNewReservation error': 'Either the room or user does not exist.'})
            response.status_code = STATUS_CODE['NOT_FOUND']
            return response

        # delete old but save information first
        old_timeslot = TimeslotMapper.find(old_booking.getTimeslot().getId())
        old_equipment = EquipmentMapper.find(old_booking.getEquipment().getId())
        if type == 'reservation':
            ReservationMapper.delete(old_booking.getId())
            ReservationMapper.done()
        if type == 'waiting':
            WaitingMapper.delete(old_booking.getId())
            WaitingMapper.done()
        TimeslotMapper.delete(old_booking.getTimeslot().getId())
        EquipmentMapper.delete(old_booking.getEquipment().getId())
        TimeslotMapper.done()
        EquipmentMapper.done()

        self.update_internal_lists()

        # no use for `block` parameter, for now, just passing empty strin
        timeslot = TimeslotMapper.makeNew(startTime, endTime, datetime(int(dateList[0]), int(dateList[1]), int(dateList[2])), '', username, str(uuid4()))
        room = RoomMapper.find(roomId)
        user = UserMapper.find(username)
        equipment = EquipmentMapper.makeNew(laptop, projector, board, str(uuid4()))


        duration = int(timeslot.getEndTime()) - int(timeslot.getStartTime())
        if self.find_total_reserved_time_for_user_for_a_given_week(user.getId(), timeslot.getDate().strftime('%Y/%m/%d')) + duration > 3:
            # delete newly create things
            TimeslotMapper.delete(timeslot.getId())
            EquipmentMapper.delete(equipment.getId())

            # add the old reservation back
            TimeslotMapper.makeNew(old_timeslot.getStartTime(), old_timeslot.getEndTime(), old_timeslot.getDate(),
                                   old_timeslot.getBlock(), old_timeslot.getUserId(), old_timeslot.getId())
            EquipmentMapper.makeNew(old_equipment.getNumLaptops(), old_equipment.getNumProjectors(),
                                    old_equipment.getNumWhiteboards(), old_equipment.getId())

            if type == 'reservation':
                ReservationMapper.makeNew(old_booking.getRoom(), old_booking.getUser(), old_booking.getTimeslot(),
                                          old_booking.getDescription(), old_booking.getEquipment(), old_booking.getId())
            if type == 'waiting':
                WaitingMapper.makeNew(old_booking.getRoom(), old_booking.getUser(), old_booking.getTimeslot(),
                                      old_booking.getDescription(), old_booking.getEquipment(), old_booking.getId())

            TimeslotMapper.done()
            EquipmentMapper.done()
            ReservationMapper.done()
            WaitingMapper.done()

            response = jsonify({'error': 'You have already booked for your maximum amount of time this week. Aborting new reservation, replacing old reservation.'})
            response.status_code = STATUS_CODE['UNPROCESSABLE']
            return response

        TimeslotMapper.done()
        EquipmentMapper.done()

        if not self.isTimeslotAvailableforRoom(room, timeslot):
            response = jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is a timeslot conflict: added to the waiting list'))
        elif not self.isEquipmentAvailableForTimeSlot(timeslot, equipment):
            response = jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is not enough equipment: added to the waiting list'))
        else:
            # Successfully adding a reservation
            response = jsonify(self.commit_new_reservation(room, user, timeslot, description, equipment))

        self.update_internal_lists()
        self.update_waiting_to_reserved()
        self.update_internal_lists()

        return response