示例#1
0
    def _process(self, args):
        room = Room.get_one(args.pop('room_id'))
        user_id = args.pop('user_id', None)
        booked_for = User.get_one(user_id) if user_id else session.user
        is_prebooking = args.pop('is_prebooking')

        # Check that the booking is not longer than allowed
        booking_limit_days = 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(room.name, booking_limit_days))
            raise ExpectedError(msg)

        try:
            resv = Reservation.create_from_data(room, dict(args, booked_for_user=booked_for), session.user,
                                                prebook=is_prebooking)
            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 is_prebooking:
            data = {'pre_bookings': serialized_occurrences}
        else:
            data = {'bookings': serialized_occurrences}
        return jsonify(room_id=room.id, booking=reservation_details_schema.dump(resv).data, calendar_data=data)
示例#2
0
def _serialize_booking_details(booking):
    attributes = reservation_details_schema.dump(booking)
    date_range, occurrences = get_booking_occurrences(booking)
    date_range = [dt.isoformat() for dt in date_range]
    booking_details = dict(attributes)
    occurrences_by_type = dict(bookings={},
                               cancellations={},
                               rejections={},
                               other_bookings={})
    booking_details['occurrences'] = occurrences_by_type
    booking_details['date_range'] = 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)
    occurrences_by_type['other_bookings'] = get_room_bookings(
        booking.room, start_dt, end_dt, skip_booking_id=booking.id)
    return booking_details
示例#3
0
    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)
示例#4
0
    def _process(self):
        args = self.args
        user_id = args.pop('user_id', None)
        booked_for = User.get_one(user_id, is_deleted=False) if user_id else session.user

        # 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, dict(args, booked_for_user=booked_for), 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).data, calendar_data=data)
示例#5
0
 def _process(self):
     if self.action == 'approve':
         self.booking.accept(session.user)
     elif self.action == 'reject':
         self.reject()
     elif self.action == 'cancel':
         self.booking.cancel(session.user)
     return jsonify(booking=reservation_details_schema.dump(self.booking))
示例#6
0
 def _process(self):
     attributes = reservation_details_schema.dump(self.booking).data
     date_range, occurrences = get_booking_occurrences(self.booking)
     date_range = [dt.isoformat() for dt in date_range]
     occurrences = {dt.isoformat(): reservation_details_occurrences_schema.dump(data).data
                    for dt, data in occurrences.iteritems()}
     booking_details = dict(attributes)
     booking_details['occurrences'] = occurrences
     booking_details['date_range'] = date_range
     return jsonify(booking_details)
示例#7
0
def _serialize_booking_details(booking):
    attributes = reservation_details_schema.dump(booking).data
    date_range, occurrences = get_booking_occurrences(booking)
    date_range = [dt.isoformat() for dt in date_range]
    occurrences = {dt.isoformat(): reservation_details_occurrences_schema.dump(data).data
                   for dt, data in occurrences.iteritems()}
    booking_details = dict(attributes)
    booking_details['occurrences'] = occurrences
    booking_details['date_range'] = date_range
    return booking_details
示例#8
0
 def _process(self):
     if self.action == 'approve':
         self.booking.accept(session.user)
         state = 'approved'
     elif self.action == 'reject':
         self.reject()
         state = 'rejected'
     elif self.action == 'cancel':
         self.booking.cancel(session.user)
         state = 'cancelled'
     return jsonify(booking=reservation_details_schema.dump(self.booking).data, booking_state=state)
示例#9
0
 def _process(self):
     if self.action == 'approve':
         self.booking.accept(session.user)
         state = 'approved'
     elif self.action == 'reject':
         self.reject()
         state = 'rejected'
     elif self.action == 'cancel':
         self.booking.cancel(session.user)
         state = 'cancelled'
     return jsonify(booking=reservation_details_schema.dump(self.booking).data, booking_state=state)
示例#10
0
def _serialize_booking_details(booking):
    attributes = reservation_details_schema.dump(booking).data
    date_range, occurrences = get_booking_occurrences(booking)
    date_range = [dt.isoformat() for dt in date_range]
    booking_details = dict(attributes)
    occurrences_by_type = dict(bookings={}, cancelations={}, rejections={}, other_bookings={})
    booking_details['occurrences'] = occurrences_by_type
    booking_details['date_range'] = date_range
    for dt, [occ] in occurrences.iteritems():
        serialized_occ = reservation_details_occurrences_schema.dump([occ]).data
        if occ.is_cancelled:
            occurrences_by_type['cancelations'][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)
    occurrences_by_type['other_bookings'] = get_room_bookings(booking.room, start_dt, end_dt,
                                                              skip_booking_id=booking.id)
    return booking_details
示例#11
0
def serialize_booking_details(booking):
    from indico.modules.rb_new.operations.bookings import (get_booking_occurrences, group_blockings,
                                                           group_nonbookable_periods, get_room_bookings)

    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, [])
    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'] = get_room_bookings(booking.room, start_dt, end_dt, skip_booking_id=booking.id)
    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