示例#1
0
 def _process(self, start_date, end_date, room_ids, my_bookings, show_inactive):
     booked_for_user = session.user if my_bookings else None
     if end_date is None:
         end_date = start_date
     calendar = get_room_calendar(start_date, end_date, room_ids, booked_for_user=booked_for_user,
                                  include_inactive=show_inactive)
     return jsonify(serialize_availability(calendar).values())
示例#2
0
    def _process(self, args):
        new_booking_data = {
            'booking_reason': args['booking_reason'],
            'booked_for_user': args.get('booked_for_user', self.booking.booked_for_user),
            'start_dt': args['start_dt'],
            'end_dt': args['end_dt'],
            'repeat_frequency': args['repeat_frequency'],
            'repeat_interval': args['repeat_interval'],
        }

        additional_booking_attrs = {}
        if not should_split_booking(self.booking, new_booking_data):
            has_date_changed = not has_same_dates(self.booking, new_booking_data)
            room = self.booking.room
            self.booking.modify(new_booking_data, session.user)
            if (has_date_changed and not room.can_book(session.user, allow_admin=False) and
                    room.can_prebook(session.user, allow_admin=False) and self.booking.is_accepted):
                self.booking.reset_approval(session.user)
        else:
            new_booking = split_booking(self.booking, new_booking_data)
            additional_booking_attrs['new_booking_id'] = new_booking.id

        db.session.flush()
        today = date.today()
        calendar = get_room_calendar(args['start_dt'] or today, args['end_dt'] or today, [args['room_id']])
        return jsonify(booking=dict(serialize_booking_details(self.booking), **additional_booking_attrs),
                       room_calendar=serialize_availability(calendar).values())
示例#3
0
    def _process(self, room_ids, **kwargs):
        rooms = [self.room] if self.room else Room.query.filter(Room.id.in_(room_ids), ~Room.is_deleted).all()
        date_range, availability = get_rooms_availability(rooms, **kwargs)
        date_range = [dt.isoformat() for dt in date_range]

        for data in availability.viewvalues():
            # add additional helpful attributes
            data.update({
                'num_days_available': len(date_range) - len(data['conflicts']),
                'all_days_available': not data['conflicts'],
                'num_conflicts': len(data['conflicts'])
            })
        serialized = serialize_availability(availability)
        if self.room:
            availability = serialized[self.room.id]
        else:
            # keep order of original room id list
            availability = sorted(serialized.items(), key=lambda x: room_ids.index(x[0]))
        return jsonify(availability=availability, date_range=date_range)
示例#4
0
def get_booking_edit_calendar_data(booking, booking_changes):
    """Return calendar-related data for the booking edit modal."""
    room = booking.room
    booking_details = serialize_booking_details(booking)
    old_date_range = booking_details['date_range']
    booking_availability = dict(booking_details['occurrences'],
                                candidates={},
                                conflicts={},
                                conflicting_candidates={},
                                pre_bookings={},
                                pre_conflicts={},
                                pending_cancellations={},
                                num_days_available=None,
                                num_conflicts=None)
    response = {
        'will_be_split': False,
        'calendars': [{
            'date_range': old_date_range,
            'data': booking_availability
        }]
    }

    if should_split_booking(booking, booking_changes):
        future_occurrences = [
            occ
            for occ in sorted(booking.occurrences, key=attrgetter('start_dt'))
            if occ.start_dt >= datetime.now()
        ]
        new_booking_start_dt = datetime.combine(
            future_occurrences[0].start_dt.date(),
            booking_changes['start_dt'].time())
        availability_filters = dict(booking_changes,
                                    start_dt=new_booking_start_dt)
        new_date_range, data = get_rooms_availability(
            [room], skip_conflicts_with=[booking.id], **availability_filters)

        for occ in booking.occurrences:
            serialized = serialize_occurrences({occ.start_dt.date(): [occ]})
            if occ in future_occurrences:
                booking_availability['pending_cancellations'].update(
                    serialized)
            elif not occ.is_rejected and not occ.is_cancelled:
                booking_availability['bookings'].update(serialized)

        response['will_be_split'] = True
    elif not has_same_dates(booking, booking_changes):
        new_date_range, data = get_rooms_availability(
            [room], skip_conflicts_with=[booking.id], **booking_changes)
    else:
        return response

    room_availability = data[room.id]
    room_availability['cancellations'] = {}
    room_availability['rejections'] = {}
    other_bookings = {
        dt: filter(lambda x: x.reservation.id != booking.id, other)
        for dt, other in room_availability['bookings'].iteritems()
    }
    cancelled_dates = [
        occ.start_dt.date() for occ in booking.occurrences if occ.is_cancelled
    ]
    rejected_dates = [
        occ.start_dt.date() for occ in booking.occurrences if occ.is_rejected
    ]
    candidates = room_availability['candidates']

    for dt, dt_candidates in candidates.iteritems():
        if dt in cancelled_dates:
            candidates[dt] = []
            room_availability['cancellations'].update({dt: dt_candidates})
        elif dt in rejected_dates:
            candidates[dt] = []
            room_availability['rejections'].update({dt: dt_candidates})

    room_availability['num_days_available'] = len(new_date_range) - len(
        room_availability['conflicts'])
    room_availability['num_conflicts'] = len(room_availability['conflicts'])
    room_availability['bookings'] = {}
    room_availability['other'] = serialize_occurrences(other_bookings)
    room_availability['pending_cancellations'] = {}
    response['calendars'].append({
        'date_range': new_date_range,
        'data': serialize_availability(data)[room.id]
    })
    return response