def _process(self): args = self.args args.setdefault('booked_for_user', session.user) if not is_booking_start_within_grace_period(args['start_dt'], session.user, args['admin_override_enabled']): raise ExpectedError(_('You cannot create a booking which starts in the past')) # Check that the booking is not longer than allowed booking_limit_days = self.room.booking_limit_days or rb_settings.get('booking_limit') if not self._validate_room_booking_limit(args['start_dt'], args['end_dt'], booking_limit_days): msg = (_('Bookings for the room "{}" may not be longer than {} days') .format(self.room.name, booking_limit_days)) raise ExpectedError(msg) try: resv = Reservation.create_from_data(self.room, args, session.user, prebook=self.prebook) if args.get('link_type') is not None and args.get('link_id') is not None: self._link_booking(resv, args['link_type'], args['link_id'], args['link_back']) db.session.flush() except NoReportError as e: db.session.rollback() raise ExpectedError(unicode(e)) serialized_occurrences = serialize_occurrences(group_by_occurrence_date(resv.occurrences.all())) if self.prebook: data = {'pre_bookings': serialized_occurrences} else: data = {'bookings': serialized_occurrences} return jsonify(room_id=self.room.id, booking=reservation_details_schema.dump(resv), calendar_data=data)
def _process(self): args = self.args args.setdefault('booked_for_user', session.user) if not is_booking_start_within_grace_period(args['start_dt'], session.user, args['admin_override_enabled']): raise ExpectedError(_('You cannot create a booking which starts in the past')) # Check that the booking is not longer than allowed booking_limit_days = self.room.booking_limit_days or rb_settings.get('booking_limit') if not self._validate_room_booking_limit(args['start_dt'], args['end_dt'], booking_limit_days): msg = (_('Bookings for the room "{}" may not be longer than {} days') .format(self.room.name, booking_limit_days)) raise ExpectedError(msg) try: resv = Reservation.create_from_data(self.room, args, session.user, prebook=self.prebook) if args.get('link_type') is not None and args.get('link_id') is not None: self._link_booking(resv, args['link_type'], args['link_id'], args['link_back']) db.session.flush() except NoReportError as e: db.session.rollback() raise ExpectedError(unicode(e)) serialized_occurrences = serialize_occurrences(group_by_occurrence_date(resv.occurrences.all())) if self.prebook: data = {'pre_bookings': serialized_occurrences} else: data = {'bookings': serialized_occurrences} return jsonify(room_id=self.room.id, booking=reservation_details_schema.dump(resv), calendar_data=data)
def serialize_booking_details(booking): from indico.modules.rb.operations.blockings import filter_blocked_rooms, get_rooms_blockings, group_blocked_rooms from indico.modules.rb.operations.bookings import (get_booking_occurrences, get_existing_room_occurrences, group_blockings, group_nonbookable_periods) from indico.modules.rb.operations.misc import get_rooms_nonbookable_periods, get_rooms_unbookable_hours from indico.modules.rb.schemas import reservation_details_schema, reservation_occurrences_schema_with_permissions attributes = reservation_details_schema.dump(booking) date_range, occurrences = get_booking_occurrences(booking) booking_details = dict(attributes) occurrences_by_type = dict(bookings={}, cancellations={}, rejections={}, other={}, blockings={}, unbookable_hours={}, nonbookable_periods={}, overridable_blockings={}) booking_details['occurrences'] = occurrences_by_type booking_details['date_range'] = [dt.isoformat() for dt in date_range] for dt, [occ] in occurrences.iteritems(): serialized_occ = reservation_occurrences_schema_with_permissions.dump([occ]) if occ.is_cancelled: occurrences_by_type['cancellations'][dt.isoformat()] = serialized_occ elif occ.is_rejected: occurrences_by_type['rejections'][dt.isoformat()] = serialized_occ occurrences_by_type['bookings'][dt.isoformat()] = serialized_occ if occ.is_valid else [] start_dt = datetime.combine(booking.start_dt, time.min) end_dt = datetime.combine(booking.end_dt, time.max) unbookable_hours = get_rooms_unbookable_hours([booking.room]).get(booking.room.id, []) other_bookings = get_existing_room_occurrences(booking.room, start_dt, end_dt, booking.repeat_frequency, booking.repeat_interval, only_accepted=True, skip_booking_id=booking.id) blocked_rooms = get_rooms_blockings([booking.room], start_dt.date(), end_dt.date()) overridable_blockings = group_blocked_rooms(filter_blocked_rooms(blocked_rooms, overridable_only=True, explicit=True)).get(booking.room.id, []) nonoverridable_blockings = group_blocked_rooms(filter_blocked_rooms(blocked_rooms, nonoverridable_only=True, explicit=True)).get(booking.room.id, []) nonbookable_periods = get_rooms_nonbookable_periods([booking.room], start_dt, end_dt).get(booking.room.id, []) nonbookable_periods_grouped = group_nonbookable_periods(nonbookable_periods, date_range) occurrences_by_type['other'] = serialize_occurrences(group_by_occurrence_date(other_bookings)) occurrences_by_type['blockings'] = serialize_blockings(group_blockings(nonoverridable_blockings, date_range)) occurrences_by_type['overridable_blockings'] = serialize_blockings(group_blockings(overridable_blockings, date_range)) occurrences_by_type['unbookable_hours'] = serialize_unbookable_hours(unbookable_hours) occurrences_by_type['nonbookable_periods'] = serialize_nonbookable_periods(nonbookable_periods_grouped) return booking_details
def serialize_booking_details(booking): from indico.modules.rb.operations.blockings import filter_blocked_rooms, get_rooms_blockings, group_blocked_rooms from indico.modules.rb.operations.bookings import (get_booking_occurrences, get_existing_room_occurrences, group_blockings, group_nonbookable_periods) from indico.modules.rb.operations.misc import get_rooms_nonbookable_periods, get_rooms_unbookable_hours from indico.modules.rb.schemas import reservation_details_schema, reservation_occurrences_schema_with_permissions attributes = reservation_details_schema.dump(booking) date_range, occurrences = get_booking_occurrences(booking) booking_details = dict(attributes) occurrences_by_type = dict(bookings={}, cancellations={}, rejections={}, other={}, blockings={}, unbookable_hours={}, nonbookable_periods={}, overridable_blockings={}) booking_details['occurrences'] = occurrences_by_type booking_details['date_range'] = [dt.isoformat() for dt in date_range] for dt, [occ] in occurrences.items(): serialized_occ = reservation_occurrences_schema_with_permissions.dump([occ]) if occ.is_cancelled: occurrences_by_type['cancellations'][dt.isoformat()] = serialized_occ elif occ.is_rejected: occurrences_by_type['rejections'][dt.isoformat()] = serialized_occ occurrences_by_type['bookings'][dt.isoformat()] = serialized_occ if occ.is_valid else [] start_dt = datetime.combine(booking.start_dt, time.min) end_dt = datetime.combine(booking.end_dt, time.max) unbookable_hours = get_rooms_unbookable_hours([booking.room]).get(booking.room.id, []) other_bookings = get_existing_room_occurrences(booking.room, start_dt, end_dt, booking.repeat_frequency, booking.repeat_interval, skip_booking_id=booking.id) blocked_rooms = get_rooms_blockings([booking.room], start_dt.date(), end_dt.date()) overridable_blockings = group_blocked_rooms(filter_blocked_rooms(blocked_rooms, overridable_only=True, explicit=True)).get(booking.room.id, []) nonoverridable_blockings = group_blocked_rooms(filter_blocked_rooms(blocked_rooms, nonoverridable_only=True, explicit=True)).get(booking.room.id, []) nonbookable_periods = get_rooms_nonbookable_periods([booking.room], start_dt, end_dt).get(booking.room.id, []) nonbookable_periods_grouped = group_nonbookable_periods(nonbookable_periods, date_range) occurrences_by_type['other'] = serialize_occurrences(group_by_occurrence_date(other_bookings)) occurrences_by_type['blockings'] = serialize_blockings(group_blockings(nonoverridable_blockings, date_range)) occurrences_by_type['overridable_blockings'] = serialize_blockings(group_blockings(overridable_blockings, date_range)) occurrences_by_type['unbookable_hours'] = serialize_unbookable_hours(unbookable_hours) occurrences_by_type['nonbookable_periods'] = serialize_nonbookable_periods(nonbookable_periods_grouped) return booking_details