Beispiel #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))
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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