def filter_and_sort_providers_based_on_schedule(booking, providerQuery): ''' Loops over the providers and find the best available timeslot for each of them Sorts the list by ascending proximity to the requested time ''' booking_responses = [] request_timeslot = create_one_hour_timeslot(booking.request_datetime) #logging.info('request timeslot: %s %s' % request_timeslot) for p in providerQuery: # list all available timeslots in order of proximity to requested datetime sorted_available_timeslots = get_sorted_schedule_timeslots( p, request_timeslot) logging.debug('provider:%s schedules: %s' % (p.vanity_url, sorted_available_timeslots)) # remove existing bookings sorted_available_timeslots_without_conflicts = filter_out_timeslots_with_existing_bookings( sorted_available_timeslots, p, request_timeslot) if sorted_available_timeslots_without_conflicts: # not empty best_ts = sorted_available_timeslots_without_conflicts[0] br = BookingResponse(p, best_ts) booking_responses.append(br) # sort all responses by distance to the request sorted_responses = sorted( booking_responses, key=lambda br: timeslot_distance(br.timeslot, request_timeslot)) # limit to 3 results returned_responses = sorted_responses[0:3] # ..and return return returned_responses
def filter_and_sort_providers_based_on_schedule(booking, providerQuery): ''' Loops over the providers and find the best available timeslot for each of them Sorts the list by ascending proximity to the requested time ''' booking_responses = [] request_timeslot = create_one_hour_timeslot(booking.request_datetime) #logging.info('request timeslot: %s %s' % request_timeslot) for p in providerQuery: # list all available timeslots in order of proximity to requested datetime sorted_available_timeslots = get_sorted_schedule_timeslots(p, request_timeslot) logging.debug('provider:%s schedules: %s' % (p.vanity_url, sorted_available_timeslots)) # remove existing bookings sorted_available_timeslots_without_conflicts = filter_out_timeslots_with_existing_bookings(sorted_available_timeslots, p, request_timeslot) if sorted_available_timeslots_without_conflicts: # not empty best_ts = sorted_available_timeslots_without_conflicts[0] br = BookingResponse(p, best_ts) booking_responses.append(br) # sort all responses by distance to the request sorted_responses = sorted(booking_responses, key=lambda br: timeslot_distance(br.timeslot, request_timeslot)) # limit to 3 results returned_responses = sorted_responses[0:3] # ..and return return returned_responses
def test_timeslot_equality(self): dt1 = datetime(2012, 04, 17, 4, 34) dt2 = datetime(2012, 04, 17, 4, 34) ts1 = create_one_hour_timeslot(dt1) ts2 = create_one_hour_timeslot(dt2) self.assertEqual(ts1, ts2)