Ejemplo n.º 1
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))
Ejemplo n.º 2
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])
Ejemplo n.º 3
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
Ejemplo n.º 4
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(RoomTDG, 'find', no_find)

    # Execute
    val = RoomMapper.find(1)

    # Verify
    assert (val is None)
Ejemplo n.º 5
0
def test_find_found_in_id_map_not_found_in_DB(monkeypatch):
    # Test Data
    expected = Room(110)

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

    def no_find(_):
        return

    monkeypatch.setattr(IdMap, 'find', id_find)
    monkeypatch.setattr(RoomTDG, 'find', no_find)

    # Execute
    val = RoomMapper.find(1)

    # Verify
    assert (val.getId() is expected.getId())
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
 def get_all_rooms(self):
     room_models = RoomMapper.findAll()
     if room_models:
         return sorted([room.getId() for room in room_models])
     return []
Ejemplo n.º 8
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