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 get_sorted_schedule_timeslots(provider, request_timeslot): ''' Get a provider's timeslots ordered by proximity to the requested time This method is limited to the day of the request for now. Eventually expand to multi-day search ''' # returns day of week (0=monday, 1=tuesday, etc.) request_day = request_timeslot.start.weekday() # map the number to the actual day request_day_key = get_days_of_the_week()[request_day][0] request_date = request_timeslot.start.date() scheduleQuery = Schedule.query(Schedule.provider == provider.key, Schedule.day == request_day_key) logging.debug('schedules count for %s is %s' % (request_day_key, scheduleQuery.count())) timeslots = [] for s in scheduleQuery: ts = create_one_hour_timeslots_over_range(request_date, s.start_time, s.end_time) timeslots = timeslots + ts # sort sorted_timeslots = sorted( timeslots, key=lambda t: timeslot_distance(t, request_timeslot)) return sorted_timeslots
def get_sorted_schedule_timeslots(provider, request_timeslot): ''' Get a provider's timeslots ordered by proximity to the requested time This method is limited to the day of the request for now. Eventually expand to multi-day search ''' # returns day of week (0=monday, 1=tuesday, etc.) request_day = request_timeslot.start.weekday() # map the number to the actual day request_day_key = get_days_of_the_week()[request_day][0] request_date = request_timeslot.start.date() scheduleQuery = Schedule.query(Schedule.provider==provider.key, Schedule.day==request_day_key) logging.debug('schedules count for %s is %s' % (request_day_key, scheduleQuery.count())) timeslots = [] for s in scheduleQuery: ts = create_one_hour_timeslots_over_range(request_date, s.start_time, s.end_time) timeslots = timeslots + ts # sort sorted_timeslots = sorted(timeslots, key=lambda t: timeslot_distance(t, request_timeslot)) return sorted_timeslots
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