Пример #1
0
def get_single_booking_suggestions(rooms, start_dt, end_dt, limit=None):
    data = []
    new_start_dt = start_dt - timedelta(minutes=BOOKING_TIME_DIFF)
    new_end_dt = end_dt + timedelta(minutes=BOOKING_TIME_DIFF)
    nonbookable_periods = get_rooms_nonbookable_periods(
        rooms, start_dt, end_dt)
    rooms = [room for room in rooms if room.id not in nonbookable_periods]

    if not rooms:
        return data

    unbookable_hours = get_rooms_unbookable_hours(rooms)
    rooms_occurrences = get_existing_rooms_occurrences(rooms,
                                                       new_start_dt,
                                                       new_end_dt,
                                                       RepeatFrequency.NEVER,
                                                       None,
                                                       allow_overlapping=True)
    for room in rooms:
        if limit and len(data) == limit:
            break

        suggestions = {}
        taken_periods = [(occ.start_dt, occ.end_dt)
                         for occ in rooms_occurrences.get(room.id, [])]
        if room.id in unbookable_hours:
            taken_periods.extend((datetime.combine(start_dt, uh.start_time),
                                  datetime.combine(end_dt, uh.end_time))
                                 for uh in unbookable_hours[room.id])

        taken_periods = sorted(taken_periods)
        suggested_time = get_start_time_suggestion(taken_periods, start_dt,
                                                   end_dt)
        if suggested_time:
            suggested_time_change = (suggested_time -
                                     start_dt).total_seconds() / 60
            if suggested_time_change and abs(
                    suggested_time_change) <= BOOKING_TIME_DIFF:
                suggestions['time'] = suggested_time_change

        duration_suggestion = get_duration_suggestion(taken_periods, start_dt,
                                                      end_dt)
        original_duration = (end_dt - start_dt).total_seconds() / 60
        if duration_suggestion and duration_suggestion <= DURATION_FACTOR * original_duration:
            suggestions['duration'] = duration_suggestion
        if suggestions:
            data.append({'room': room, 'suggestions': suggestions})
    return data
Пример #2
0
def get_single_booking_suggestions(rooms, start_dt, end_dt, limit=None):
    data = []
    new_start_dt = start_dt - timedelta(minutes=BOOKING_TIME_DIFF)
    new_end_dt = end_dt + timedelta(minutes=BOOKING_TIME_DIFF)
    nonbookable_periods = get_rooms_nonbookable_periods(rooms, start_dt, end_dt)
    rooms = [room for room in rooms if room.id not in nonbookable_periods]

    if not rooms:
        return data

    unbookable_hours = get_rooms_unbookable_hours(rooms)
    rooms_occurrences = get_existing_rooms_occurrences(rooms, new_start_dt, new_end_dt, RepeatFrequency.NEVER, None,
                                                       allow_overlapping=True)
    for room in rooms:
        if limit and len(data) == limit:
            break

        suggestions = {}
        taken_periods = [(occ.start_dt, occ.end_dt) for occ in rooms_occurrences.get(room.id, [])]
        if room.id in unbookable_hours:
            taken_periods.extend((datetime.combine(start_dt, uh.start_time), datetime.combine(end_dt, uh.end_time))
                                 for uh in unbookable_hours[room.id])

        taken_periods = sorted(taken_periods)
        suggested_time = get_start_time_suggestion(taken_periods, start_dt, end_dt)
        if suggested_time:
            suggested_time_change = (suggested_time - start_dt).total_seconds() / 60
            if suggested_time_change and abs(suggested_time_change) <= BOOKING_TIME_DIFF:
                suggestions['time'] = suggested_time_change

        duration_suggestion = get_duration_suggestion(taken_periods, start_dt, end_dt)
        original_duration = (end_dt - start_dt).total_seconds() / 60
        if duration_suggestion and duration_suggestion <= DURATION_FACTOR * original_duration:
            suggestions['duration'] = duration_suggestion
        if suggestions:
            data.append({'room': room, 'suggestions': suggestions})
    return data