def makeReservation(self, car_id, start, end):
        """Make a car reservation given a car_id, start and end dates only if
        given car is available or if given range does not overlap with existing resevations
        for that car.
        """
        # check that there is car availability for the given range
        # or the given range does not overlap with other reservations
        requested_car = Car.objects.get(pk=car_id)
        print(requested_car.model)
        if requested_car is None:
            raise ValueError('the %i id is not valid' % car_id)
        reservation = CarReservation()
        # check if dates overlap with existing reservations when there is no avail
        other_reservations_overlapping = CarReservation.objects.filter(car_id=car_id). \
            filter(Q(start_date__range=[start, end]) | Q(end_date__range=[start, end]))
        if other_reservations_overlapping and requested_car.avail == 0:
            return None

        # %z is supported in Python 3.2+ but not in 2.7, so hardcoding +00:00
        reservation.start_date = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S+00:00")
        reservation.end_date = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S+00:00")
        reservation.car = requested_car
        reservation.save()

        # TIM NOTE: The field below is being used incorrectly.
        # Please see carrental/models/Car.py for the explanation.
        requested_car.avail -= 1
        requested_car.save()
        return reservation
示例#2
0
 def makeReservation(self, car_id, start, end):
     # check that there is car availability for the given range
     # or the given range does not overlap with other reservations
     requested_car = Car.objects.get(pk=car_id)
     print(requested_car.model)
     if requested_car is None:
         raise ValueError("the %i id is not valid" % car_id)
     reservation = CarReservation()
     # check if dates overlap with existing reservations when there is no avail
     other_reservations_overlapping = CarReservation.objects.filter(car=car_id).filter(
         Q(start_date__range=[start, end]) | Q(end_date__range=[start, end])
     )
     if other_reservations_overlapping and requested_car.avail == 0:
         return None
     #%z is supported in Python 3.2+ but not in 2.7, so hardcoding +00:00
     reservation.start_date = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S+00:00")
     reservation.end_date = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S+00:00")
     reservation.car = requested_car
     reservation.save()
     requested_car.avail -= 1
     requested_car.save()
     return reservation