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] 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 _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] 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 _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 _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()
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()
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 self._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 get_managed_room_ids(user): if _can_get_all_groups(user): return { id_ for id_, in _query_managed_rooms(user).with_entities(Room.id) } else: return {r.id for r in Room.get_owned_by(user)}
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(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 search_for_rooms(filters, only_available=False): query = (Room.query .outerjoin(favorite_room_table, db.and_(favorite_room_table.c.user_id == session.user.id, favorite_room_table.c.room_id == Room.id)) .reset_joinpoint() # otherwise filter_by() would apply to the favorite table .options(raiseload('owner')) .filter(Room.is_active) .order_by(favorite_room_table.c.user_id.is_(None), db.func.indico.natsort(Room.full_name))) criteria = {} if 'capacity' in filters: query = query.filter(db.or_(Room.capacity >= (filters['capacity'] * 0.8), Room.capacity.is_(None))) if 'building' in filters: criteria['building'] = filters['building'] if 'floor' in filters: criteria['floor'] = filters['floor'] query = query.filter_by(**criteria) if 'text' in filters: query = query.filter(_make_room_text_filter(filters['text'])) if filters.get('equipment'): subquery = (db.session.query(RoomEquipmentAssociation) .with_entities(db.func.count(RoomEquipmentAssociation.c.room_id)) .filter( RoomEquipmentAssociation.c.room_id == Room.id, EquipmentType.name.in_(filters['equipment']) ) .join(EquipmentType, RoomEquipmentAssociation.c.equipment_id == EquipmentType.id) .correlate(Room) .as_scalar()) query = query.filter(subquery == len(filters['equipment'])) if filters.get('favorite'): query = query.filter(favorite_room_table.c.user_id.isnot(None)) if filters.get('mine'): ids = get_managed_room_ids(session.user) if ids: query = query.filter(Room.id.in_(ids)) query = _filter_coordinates(query, filters) if not only_available: return query start_dt, end_dt = filters['start_dt'], filters['end_dt'] repeatability = (filters['repeat_frequency'], filters['repeat_interval']) query = query.filter(Room.filter_available(start_dt, end_dt, repeatability, include_pre_bookings=True, include_pending_blockings=True)) if not rb_is_admin(session.user): selected_period_days = (filters['end_dt'] - filters['start_dt']).days booking_limit_days = db.func.coalesce(Room.booking_limit_days, rb_settings.get('booking_limit')) own_rooms = [r.id for r in Room.get_owned_by(session.user)] query = query.filter(db.or_(Room.id.in_(own_rooms) if own_rooms else False, db.and_(Room.filter_bookable_hours(start_dt.time(), end_dt.time()), db.or_(booking_limit_days.is_(None), selected_period_days <= booking_limit_days)))) return query
def test_ownership_functions(dummy_room, create_user, create_room_attribute, is_owner, has_group, in_group, expected): user = create_user(u'user') create_room_attribute(u'manager-group') if is_owner: dummy_room.owner = user if has_group: dummy_room.set_attribute_value(u'manager-group', u'managers') if in_group: user.groups.add(u'managers') assert dummy_room.is_owned_by(user) == expected assert Room.user_owns_rooms(user) == expected assert set(Room.get_owned_by(user)) == ({dummy_room} if expected else set())
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())
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())
def test_ownership_functions(dummy_room, create_user, create_room_attribute, is_owner, has_group, in_group, expected): user = create_user(u'user') create_room_attribute(u'manager-group') if is_owner: dummy_room.owner = user if has_group: dummy_room.set_attribute_value(u'manager-group', u'managers') if in_group: user.groups.add(u'managers') assert dummy_room.is_owned_by(user) == expected assert Room.user_owns_rooms(user) == expected assert set( Room.get_owned_by(user)) == ({dummy_room} if expected else set())
def get_rooms(self): """Returns the rooms this user is responsible for""" from indico.modules.rb.models.rooms import Room # avoid circular import return Room.get_owned_by(self)
def get_rooms(self): """Returns the rooms this user is responsible for""" from indico.modules.rb.models.rooms import Room # avoid circular import return Room.get_owned_by(self)
def get_managed_room_ids(user): if _can_get_all_groups(user): return {id_ for id_, in _query_managed_rooms(user).with_entities(Room.id)} else: return {r.id for r in Room.get_owned_by(user)}
def search_for_rooms(filters, availability=None): """Search for a room, using the provided filters. :param filters: The filters, provided as a dictionary :param availability: A boolean specifying whether (un)available rooms should be provided, or `None` in case all rooms should be returned. """ query = (Room.query .outerjoin(favorite_room_table, db.and_(favorite_room_table.c.user_id == session.user.id, favorite_room_table.c.room_id == Room.id)) .reset_joinpoint() # otherwise filter_by() would apply to the favorite table .options(raiseload('owner')) .filter(Room.is_active) .order_by(favorite_room_table.c.user_id.is_(None), db.func.indico.natsort(Room.full_name))) criteria = {} if 'capacity' in filters: query = query.filter(Room.capacity >= filters['capacity']) if 'building' in filters: criteria['building'] = filters['building'] if 'division' in filters: criteria['division'] = filters['division'] query = query.filter_by(**criteria) if 'text' in filters: query = query.filter(_make_room_text_filter(filters['text'])) if filters.get('equipment'): subquery = (db.session.query(RoomEquipmentAssociation) .with_entities(db.func.count(RoomEquipmentAssociation.c.room_id)) .filter( RoomEquipmentAssociation.c.room_id == Room.id, EquipmentType.name.in_(filters['equipment']) ) .join(EquipmentType, RoomEquipmentAssociation.c.equipment_id == EquipmentType.id) .correlate(Room) .as_scalar()) query = query.filter(subquery == len(filters['equipment'])) if filters.get('features'): for feature in filters['features']: query = query.filter(Room.available_equipment.any(EquipmentType.features.any(RoomFeature.name == feature))) if filters.get('favorite'): query = query.filter(favorite_room_table.c.user_id.isnot(None)) if filters.get('mine'): ids = get_managed_room_ids(session.user) query = query.filter(Room.id.in_(ids)) query = _filter_coordinates(query, filters) if availability is None: return query start_dt, end_dt = filters['start_dt'], filters['end_dt'] repeatability = (filters['repeat_frequency'], filters['repeat_interval']) availability_query = Room.filter_available(start_dt, end_dt, repeatability, include_pre_bookings=True, include_pending_blockings=True) if availability is False: availability_query = ~availability_query query = query.filter(availability_query) if not rb_is_admin(session.user): selected_period_days = (filters['end_dt'] - filters['start_dt']).days booking_limit_days = db.func.coalesce(Room.booking_limit_days, rb_settings.get('booking_limit')) own_rooms = [r.id for r in Room.get_owned_by(session.user)] query = query.filter(db.or_(Room.id.in_(own_rooms) if own_rooms else False, db.and_(Room.filter_bookable_hours(start_dt.time(), end_dt.time()), Room.filter_nonbookable_periods(start_dt, end_dt), db.or_(booking_limit_days.is_(None), selected_period_days <= booking_limit_days)))) return query
def search_for_rooms(filters, availability=None): """Search for a room, using the provided filters. :param filters: The filters, provided as a dictionary :param availability: A boolean specifying whether (un)available rooms should be provided, or `None` in case all rooms should be returned. """ query = ( Room.query.outerjoin( favorite_room_table, db.and_( favorite_room_table.c.user_id == session.user.id, favorite_room_table.c.room_id == Room.id)).reset_joinpoint( ) # otherwise filter_by() would apply to the favorite table .options(raiseload('owner')).filter(Room.is_active).order_by( favorite_room_table.c.user_id.is_(None), db.func.indico.natsort(Room.full_name))) criteria = {} if 'capacity' in filters: query = query.filter(Room.capacity >= filters['capacity']) if 'building' in filters: criteria['building'] = filters['building'] if 'division' in filters: criteria['division'] = filters['division'] query = query.filter_by(**criteria) if 'text' in filters: query = query.filter(_make_room_text_filter(filters['text'])) if filters.get('equipment'): subquery = (db.session.query(RoomEquipmentAssociation).with_entities( db.func.count(RoomEquipmentAssociation.c.room_id)).filter( RoomEquipmentAssociation.c.room_id == Room.id, EquipmentType.name.in_(filters['equipment'])).join( EquipmentType, RoomEquipmentAssociation.c.equipment_id == EquipmentType.id).correlate(Room).as_scalar()) query = query.filter(subquery == len(filters['equipment'])) if filters.get('favorite'): query = query.filter(favorite_room_table.c.user_id.isnot(None)) if filters.get('mine'): ids = get_managed_room_ids(session.user) if ids: query = query.filter(Room.id.in_(ids)) query = _filter_coordinates(query, filters) if availability is None: return query start_dt, end_dt = filters['start_dt'], filters['end_dt'] repeatability = (filters['repeat_frequency'], filters['repeat_interval']) availability_query = Room.filter_available(start_dt, end_dt, repeatability, include_pre_bookings=True, include_pending_blockings=True) if availability is False: availability_query = ~availability_query query = query.filter(availability_query) if not rb_is_admin(session.user): selected_period_days = (filters['end_dt'] - filters['start_dt']).days booking_limit_days = db.func.coalesce(Room.booking_limit_days, rb_settings.get('booking_limit')) own_rooms = [r.id for r in Room.get_owned_by(session.user)] query = query.filter( db.or_( Room.id.in_(own_rooms) if own_rooms else False, db.and_( Room.filter_bookable_hours(start_dt.time(), end_dt.time()), Room.filter_nonbookable_periods(start_dt, end_dt), db.or_(booking_limit_days.is_(None), selected_period_days <= booking_limit_days)))) return query