Ejemplo n.º 1
0
def get_rooms_availability(rooms, start_dt, end_dt, repeat_frequency, repeat_interval):
    availability = OrderedDict()
    candidates = ReservationOccurrence.create_series(start_dt, end_dt, (repeat_frequency, repeat_interval))
    date_range = sorted(set(cand.start_dt.date() for cand in candidates))
    occurrences = get_existing_rooms_occurrences(rooms, start_dt.replace(hour=0, minute=0),
                                                 end_dt.replace(hour=23, minute=59), repeat_frequency, repeat_interval)
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(rooms, start_dt, end_dt)
    conflicts, pre_conflicts = get_rooms_conflicts(rooms, start_dt.replace(tzinfo=None), end_dt.replace(tzinfo=None),
                                                   repeat_frequency, repeat_interval, blocked_rooms,
                                                   nonbookable_periods, unbookable_hours)
    dates = list(candidate.start_dt.date() for candidate in candidates)
    for room in rooms:
        room_occurrences = occurrences.get(room.id, [])
        room_conflicts = conflicts.get(room.id, [])
        pre_room_conflicts = pre_conflicts.get(room.id, [])
        pre_bookings = [occ for occ in room_occurrences if not occ.reservation.is_accepted]
        existing_bookings = [occ for occ in room_occurrences if occ.reservation.is_accepted]
        room_blocked_rooms = blocked_rooms.get(room.id, [])
        room_nonbookable_periods = nonbookable_periods.get(room.id, [])
        room_unbookable_hours = unbookable_hours.get(room.id, [])
        availability[room.id] = {'room_id': room.id,
                                 'candidates': group_by_occurrence_date(candidates),
                                 'pre_bookings': group_by_occurrence_date(pre_bookings),
                                 'bookings': group_by_occurrence_date(existing_bookings),
                                 'conflicts': group_by_occurrence_date(room_conflicts),
                                 'pre_conflicts': group_by_occurrence_date(pre_room_conflicts),
                                 'blockings': group_blockings(room_blocked_rooms, dates),
                                 'nonbookable_periods': group_nonbookable_periods(room_nonbookable_periods, dates),
                                 'unbookable_hours': room_unbookable_hours}
    return date_range, availability
Ejemplo n.º 2
0
def get_rooms_availability(rooms, start_dt, end_dt, repeat_frequency,
                           repeat_interval, flexibility):
    period_days = (end_dt - start_dt).days
    availability = OrderedDict()
    candidates = ReservationOccurrence.create_series(
        start_dt, end_dt, (repeat_frequency, repeat_interval))
    date_range = sorted(set(cand.start_dt.date() for cand in candidates))
    occurrences = get_existing_rooms_occurrences(
        rooms, start_dt.replace(hour=0, minute=0),
        end_dt.replace(hour=23, minute=59), repeat_frequency, repeat_interval)
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(
        rooms, start_dt, end_dt)
    conflicts, pre_conflicts = get_rooms_conflicts(
        rooms, start_dt.replace(tzinfo=None), end_dt.replace(tzinfo=None),
        repeat_frequency, repeat_interval, blocked_rooms, nonbookable_periods,
        unbookable_hours)
    dates = list(candidate.start_dt.date() for candidate in candidates)
    for room in rooms:
        booking_limit_days = room.booking_limit_days or rb_settings.get(
            'booking_limit')
        if period_days > booking_limit_days:
            continue

        start_dt = start_dt + timedelta(days=flexibility)
        end_dt = end_dt + timedelta(days=flexibility)
        room_occurrences = occurrences.get(room.id, [])
        room_conflicts = conflicts.get(room.id, [])
        pre_room_conflicts = pre_conflicts.get(room.id, [])
        pre_bookings = [
            occ for occ in room_occurrences if not occ.reservation.is_accepted
        ]
        existing_bookings = [
            occ for occ in room_occurrences if occ.reservation.is_accepted
        ]
        room_blocked_rooms = blocked_rooms.get(room.id, [])
        room_nonbookable_periods = nonbookable_periods.get(room.id, [])
        room_unbookable_hours = unbookable_hours.get(room.id, [])
        availability[room.id] = {
            'room':
            room,
            'candidates':
            group_by_occurrence_date(candidates),
            'pre_bookings':
            group_by_occurrence_date(pre_bookings),
            'bookings':
            group_by_occurrence_date(existing_bookings),
            'conflicts':
            group_by_occurrence_date(room_conflicts),
            'pre_conflicts':
            group_by_occurrence_date(pre_room_conflicts),
            'blockings':
            group_blockings(room_blocked_rooms, dates),
            'nonbookable_periods':
            group_nonbookable_periods(room_nonbookable_periods, dates),
            'unbookable_hours':
            room_unbookable_hours
        }
    return date_range, availability
Ejemplo n.º 3
0
def get_recurring_booking_suggestions(rooms, start_dt, end_dt, repeat_frequency, repeat_interval, limit=None):
    data = []
    booking_days = end_dt - start_dt
    booking_length = booking_days.days + 1
    candidates = ReservationOccurrence.create_series(start_dt, end_dt, (repeat_frequency, repeat_interval))
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(rooms, start_dt, end_dt)
    conflicts = get_rooms_conflicts(rooms, start_dt, end_dt, repeat_frequency, repeat_interval, blocked_rooms,
                                    nonbookable_periods, unbookable_hours)[0]
    for room in rooms:
        if limit and len(data) == limit:
            break

        suggestions = {}
        booking_limit = room.booking_limit_days or rb_settings.get('booking_limit')
        limit_exceeded = booking_limit is not None and booking_limit < booking_length
        if limit_exceeded:
            excess_days = booking_length - booking_limit
            suggestions['shorten'] = excess_days

        if not limit_exceeded:
            number_of_conflicting_days = len(group_by_occurrence_date(conflicts.get(room.id, [])))
            if number_of_conflicting_days and number_of_conflicting_days < len(candidates):
                suggestions['skip'] = number_of_conflicting_days
        if suggestions:
            data.append({'room': room, 'suggestions': suggestions})
    return data
Ejemplo n.º 4
0
def get_number_of_skipped_days_for_rooms(rooms,
                                         start_dt,
                                         end_dt,
                                         repeat_frequency,
                                         repeat_interval,
                                         limit=None):
    data = []
    candidates = ReservationOccurrence.create_series(
        start_dt, end_dt, (repeat_frequency, repeat_interval))
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(
        rooms, start_dt, end_dt)
    conflicts, _ = get_rooms_conflicts(rooms, start_dt, end_dt,
                                       repeat_frequency, repeat_interval,
                                       blocked_rooms, nonbookable_periods,
                                       unbookable_hours)
    for room in rooms:
        if limit and len(data) == limit:
            break

        number_of_conflicting_days = len(
            group_by_occurrence_date(conflicts.get(room.id, [])))
        if number_of_conflicting_days and number_of_conflicting_days < len(
                candidates):
            data.append({
                'room': room,
                'suggestions': {
                    'skip': number_of_conflicting_days
                }
            })
    return sorted(data, key=lambda item: item['suggestions']['skip'])
Ejemplo n.º 5
0
def get_number_of_skipped_days_for_rooms(rooms, start_dt, end_dt, repeat_frequency, repeat_interval, limit=None):
    data = []
    candidates = ReservationOccurrence.create_series(start_dt, end_dt, (repeat_frequency, repeat_interval))
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(rooms, start_dt, end_dt)
    conflicts, _ = get_rooms_conflicts(rooms, start_dt, end_dt, repeat_frequency, repeat_interval, blocked_rooms,
                                       nonbookable_periods, unbookable_hours)
    for room in rooms:
        if limit and len(data) == limit:
            break

        number_of_conflicting_days = len(group_by_occurrence_date(conflicts.get(room.id, [])))
        if number_of_conflicting_days and number_of_conflicting_days < len(candidates):
            data.append({'room': room, 'suggestions': {'skip': number_of_conflicting_days}})
    return sorted(data, key=lambda item: item['suggestions']['skip'])
Ejemplo n.º 6
0
def get_rooms_availability(rooms,
                           start_dt,
                           end_dt,
                           repeat_frequency,
                           repeat_interval,
                           skip_conflicts_with=None,
                           admin_override_enabled=False):
    availability = OrderedDict()
    candidates = ReservationOccurrence.create_series(
        start_dt, end_dt, (repeat_frequency, repeat_interval))
    date_range = sorted(set(cand.start_dt.date() for cand in candidates))
    occurrences = get_existing_rooms_occurrences(
        rooms, start_dt.replace(hour=0, minute=0),
        end_dt.replace(hour=23, minute=59), repeat_frequency, repeat_interval)
    blocked_rooms = get_rooms_blockings(rooms, start_dt.date(), end_dt.date())
    nonoverridable_blocked_rooms = group_blocked_rooms(
        filter_blocked_rooms(blocked_rooms,
                             nonoverridable_only=True,
                             explicit=True))
    overridable_blocked_rooms = group_blocked_rooms(
        filter_blocked_rooms(blocked_rooms,
                             overridable_only=True,
                             explicit=True))
    unbookable_hours = get_rooms_unbookable_hours(rooms)
    nonbookable_periods = get_rooms_nonbookable_periods(
        rooms, start_dt, end_dt)
    conflicts, pre_conflicts, conflicting_candidates = get_rooms_conflicts(
        rooms,
        start_dt.replace(tzinfo=None),
        end_dt.replace(tzinfo=None),
        repeat_frequency,
        repeat_interval,
        nonoverridable_blocked_rooms,
        nonbookable_periods,
        unbookable_hours,
        skip_conflicts_with,
        allow_admin=admin_override_enabled)
    dates = list(candidate.start_dt.date() for candidate in candidates)
    for room in rooms:
        room_occurrences = occurrences.get(room.id, [])
        room_conflicting_candidates = conflicting_candidates.get(room.id, [])
        room_conflicts = conflicts.get(room.id, [])
        pre_room_conflicts = pre_conflicts.get(room.id, [])
        pre_bookings = [
            occ for occ in room_occurrences if not occ.reservation.is_accepted
        ]
        existing_bookings = [
            occ for occ in room_occurrences if occ.reservation.is_accepted
        ]
        room_nonoverridable_blocked_rooms = nonoverridable_blocked_rooms.get(
            room.id, [])
        room_overridable_blocked_rooms = overridable_blocked_rooms.get(
            room.id, [])
        room_nonbookable_periods = nonbookable_periods.get(room.id, [])
        room_unbookable_hours = unbookable_hours.get(room.id, [])

        room_candidates = get_room_candidates(candidates, room_conflicts,
                                              pre_room_conflicts)
        availability[room.id] = {
            'room_id':
            room.id,
            'candidates':
            group_by_occurrence_date(room_candidates),
            'conflicting_candidates':
            group_by_occurrence_date(room_conflicting_candidates),
            'pre_bookings':
            group_by_occurrence_date(pre_bookings),
            'bookings':
            group_by_occurrence_date(existing_bookings),
            'conflicts':
            group_by_occurrence_date(room_conflicts),
            'pre_conflicts':
            group_by_occurrence_date(pre_room_conflicts),
            'blockings':
            group_blockings(room_nonoverridable_blocked_rooms, dates),
            'overridable_blockings':
            group_blockings(room_overridable_blocked_rooms, dates),
            'nonbookable_periods':
            group_nonbookable_periods(room_nonbookable_periods, dates),
            'unbookable_hours':
            room_unbookable_hours
        }
    return date_range, availability