def _process(self):
        form = self._form
        if self._is_submitted() and form.validate():
            if form.data.get('is_only_my_rooms'):
                form.room_ids.data = [
                    room.id for room in Room.find_all()
                    if room.is_owned_by(session.user)
                ]

            occurrences = ReservationOccurrence.find_with_filters(
                form.data, session.user).all()
            rooms = self._filter_displayed_rooms(
                [r for r in self._rooms if r.id in set(form.room_ids.data)],
                occurrences)
            return WPRoomBookingSearchBookingsResults(
                self,
                rooms=rooms,
                occurrences=occurrences,
                show_blockings=self.show_blockings,
                start_dt=form.start_dt.data,
                end_dt=form.end_dt.data,
                form=form,
                form_data=self._form_data,
                menu_item=self.menu_item).display()

        my_rooms = [r.id for r in Room.get_owned_by(session.user)]
        return WPRoomBookingSearchBookings(self,
                                           errors=form.error_list,
                                           rooms=self._rooms,
                                           my_rooms=my_rooms,
                                           form=form).display()
Esempio n. 2
0
 def _process(self):
     state = BlockedRoom.State.get(self.state)
     my_blocks = defaultdict(list)
     for room in Room.get_owned_by(session.user):
         roomBlocks = room.blocked_rooms.filter(True if state is None else BlockedRoom.state == state).all()
         if roomBlocks:
             my_blocks[room] += roomBlocks
     return WPRoomBookingBlockingsForMyRooms(self, room_blocks=my_blocks).display()
    def _process_select_room(self):
        # Step 1: Room(s), dates, repetition selection
        form = self._make_select_room_form()
        if form.validate_on_submit():
            flexible_days = form.flexible_dates_range.data
            day_start_dt = datetime.combine(form.start_dt.data.date(), time())
            day_end_dt = datetime.combine(form.end_dt.data.date(),
                                          time(23, 59))
            selected_rooms = [
                r for r in self._rooms if r.id in form.room_ids.data
            ]
            selected_period_days = (day_end_dt - day_start_dt).days
            for room in selected_rooms:
                booking_limit_days = room.booking_limit_days or rb_settings.get(
                    'booking_limit')
                if selected_period_days > booking_limit_days:
                    flash(
                        _(u'Bookings for the room "{}" may not be longer than {} days'
                          ).format(room.name, booking_limit_days), 'error')
                    return redirect(url_for('rooms.book'))
            occurrences, candidates = self._get_all_occurrences(
                form.room_ids.data, form, flexible_days)
            period_form_defaults = FormDefaults(
                repeat_interval=form.repeat_interval.data,
                repeat_frequency=form.repeat_frequency.data)
            period_form = self._make_select_period_form(period_form_defaults)

            # Show step 2 page
            return self._get_view('select_period',
                                  rooms=selected_rooms,
                                  occurrences=occurrences,
                                  candidates=candidates,
                                  start_dt=day_start_dt,
                                  end_dt=day_end_dt,
                                  period_form=period_form,
                                  form=form,
                                  repeat_frequency=form.repeat_frequency.data,
                                  repeat_interval=form.repeat_interval.data,
                                  flexible_days=flexible_days).display()

        # GET or form errors => show step 1 page
        return self._get_view(
            'select_room',
            errors=form.error_list,
            rooms=self._rooms,
            form=form,
            my_rooms=[r.id for r in Room.get_owned_by(session.user)],
            max_room_capacity=Room.max_capacity,
            can_override=rb_is_admin(session.user),
            date_changed=not form.is_submitted() and self.date_changed,
        ).display()
def test_ownership_functions(dummy_room, create_user, create_room_attribute,
                             create_group, is_owner, has_group, in_group,
                             expected):
    other_user = create_user(123)
    create_room_attribute(u'manager-group')
    if is_owner:
        dummy_room.owner = other_user
    if has_group:
        dummy_room.set_attribute_value(u'manager-group', u'123')
    if in_group:
        other_user.local_groups.add(create_group(123).group)
    assert dummy_room.is_owned_by(other_user) == expected
    assert Room.user_owns_rooms(other_user) == expected
    assert set(
        Room.get_owned_by(other_user)) == ({dummy_room} if expected else set())