def test_is_active_at(create_blocking, check_date, expected): start_date = date(2014, 12, 5) end_date = date(2014, 12, 7) check_date = datetime.strptime(check_date, '%Y-%m-%d').date() blocking = create_blocking(start_date=start_date, end_date=end_date) assert blocking.is_active_at(check_date) == expected assert Blocking.find_first( Blocking.is_active_at(check_date)) == (blocking if expected else None)
def _merge_users(target, source, **kwargs): BlockingPrincipal.merge_users(target, source, 'blocking') Blocking.find(created_by_id=source.id).update( {Blocking.created_by_id: target.id}) Reservation.find(created_by_id=source.id).update( {Reservation.created_by_id: target.id}) Reservation.find(booked_for_id=source.id).update( {Reservation.booked_for_id: target.id}) Room.find(owner_id=source.id).update({Room.owner_id: target.id}) rb_settings.acls.merge_users(target, source)
def _process_args(self): self._blocking = Blocking.get(request.view_args['blocking_id']) if self._blocking is None: raise NotFound('A blocking with this ID does not exist.') defaults = FormDefaults(self._blocking, attrs={'reason'}, principals=self._blocking.allowed, blocked_rooms=[br.room_id for br in self._blocking.blocked_rooms]) self._form = BlockingForm(obj=defaults) self._form._blocking = self._blocking
def _process_args(self): blocking_id = self._params.get('blocking_id') self._room = Room.get(self._params['room_id']) self._blocking = Blocking.get(blocking_id) if blocking_id else None if 'start_dt' in self._params and 'end_dt' in self._params: start_dt = datetime.strptime(self._params['start_dt'], '%H:%M %Y-%m-%d') end_dt = datetime.strptime(self._params['end_dt'], '%H:%M %Y-%m-%d') self._nonbookable = bool(NonBookablePeriod.find_first(NonBookablePeriod.room_id == self._room.id, NonBookablePeriod.overlaps(start_dt, end_dt))) else: self._nonbookable = False
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 _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 _process_args(self): self._blocking = Blocking.get(request.view_args['blocking_id']) if not self._blocking: raise NotFound('A blocking with this ID does not exist.')
def get_blocked_rooms(self, *dates, **kwargs): states = kwargs.get('states', (BlockedRoom.State.accepted, )) return (self.blocked_rooms.join(BlockedRoom.blocking).options( contains_eager(BlockedRoom.blocking)).filter( or_(Blocking.is_active_at(d) for d in dates), BlockedRoom.state.in_(states)).all())