def test_find_with_filters(create_room, create_blocking): rooms = [create_room(), create_room()] blockings = [ create_blocking(start_date=date.today(), end_date=date.today(), room=rooms[0], state=BlockedRoom.State.accepted), create_blocking(start_date=date.today() + timedelta(days=1), end_date=date.today() + timedelta(days=1), room=rooms[1]), create_blocking(start_date=date.today() + timedelta(days=1), end_date=date.today() + timedelta(days=1), room=rooms[1]), ] assert set(BlockedRoom.find_with_filters({})) == set(b.blocked_rooms[0] for b in blockings) filters = { 'start_date': date.today(), 'end_date': date.today(), 'room_ids': {rooms[0].id}, 'state': BlockedRoom.State.accepted } assert set(BlockedRoom.find_with_filters(filters)) == { blockings[0].blocked_rooms[0] }
def _create_blocking(**params): room = params.pop('room', dummy_room) state = params.pop('state', BlockedRoom.State.pending) params.setdefault('start_date', date.today()) params.setdefault('end_date', date.today()) params.setdefault('reason', u'Blocked') params.setdefault('created_by_user', dummy_user) blocking = Blocking(**params) if room is not None: br = BlockedRoom(room=room, state=state, blocking=blocking) if state == BlockedRoom.State.accepted: br.approve(notify_blocker=False) db.session.add(blocking) db.session.flush() return blocking
def validate_blocked_rooms(self, field): try: field.data = map(int, field.data) except Exception as e: # In case someone sent crappy data raise ValidationError(str(e)) # Make sure all room ids are valid if len(field.data) != Room.find(Room.id.in_(field.data)).count(): raise ValidationError('Invalid rooms') if hasattr(self, '_blocking'): start_date = self._blocking.start_date end_date = self._blocking.end_date blocking_id = self._blocking.id else: start_date = self.start_date.data end_date = self.end_date.data blocking_id = None overlap = BlockedRoom.find_first( BlockedRoom.room_id.in_(field.data), BlockedRoom.state != BlockedRoom.State.rejected, Blocking.start_date <= end_date, Blocking.end_date >= start_date, Blocking.id != blocking_id, _join=Blocking) if overlap: msg = 'Your blocking for {} is overlapping with another blocking.'.format( overlap.room.full_name) raise ValidationError(msg)
def _save(self): self._blocking = blocking = Blocking() blocking.start_date = self._form.start_date.data blocking.end_date = self._form.end_date.data blocking.created_by_user = session.user blocking.reason = self._form.reason.data blocking.allowed = self._form.principals.data blocking.blocked_rooms = [BlockedRoom(room_id=room_id) for room_id in self._form.blocked_rooms.data] db.session.add(blocking) db.session.flush() # synchronizes relationships (e.g. BlockedRoom.room) flash(_(u'Blocking created'), 'success') self._process_blocked_rooms(blocking.blocked_rooms)
def test_find_with_filters_dates(create_blocking, start_date, end_date, matches): start_date = datetime.strptime(start_date, '%Y-%m-%d').date() end_date = datetime.strptime(end_date, '%Y-%m-%d').date() blockings = [ create_blocking(start_date=date(2014, 12, 5), end_date=date(2014, 12, 7)), create_blocking(start_date=date(2014, 12, 6), end_date=date(2014, 12, 8)) ] filters = {'start_date': start_date, 'end_date': end_date} assert set(BlockedRoom.find_with_filters(filters)) == set( blockings[i].blocked_rooms[0] for i in matches)
def _save(self): blocking = self._blocking blocking.reason = self._form.reason.data # Update the ACL. We don't use `=` here to prevent SQLAlchemy # from deleting and re-adding unchanged entries. blocking.allowed |= self._form.principals.data blocking.allowed &= self._form.principals.data # Add/remove blocked rooms if necessary old_blocked = {br.room_id for br in blocking.blocked_rooms} new_blocked = set(self._form.blocked_rooms.data) added_blocks = new_blocked - old_blocked removed_blocks = old_blocked - new_blocked for room_id in removed_blocks: blocked_room = next(br for br in blocking.blocked_rooms if br.room_id == room_id) blocking.blocked_rooms.remove(blocked_room) added_blocked_rooms = [] for room_id in added_blocks: blocked_room = BlockedRoom(room_id=room_id) blocking.blocked_rooms.append(blocked_room) added_blocked_rooms.append(blocked_room) db.session.flush() flash(_(u'Blocking updated'), 'success') self._process_blocked_rooms(added_blocked_rooms)
def _process_args(self): self.blocked_room = BlockedRoom.get(self._params['blocked_room_id'])
def test_find_with_filters_state(create_blocking, state): other_state = next(s for s in BlockedRoom.State if s != state) create_blocking(state=other_state) blocking = create_blocking(state=state) assert set(BlockedRoom.find_with_filters( {'state': state})) == {blocking.blocked_rooms[0]}
def test_state_name(state): assert BlockedRoom(state=state).state_name == state.title