def unbook_item(user: models.User, item: models.BookingItem, force: bool = False) -> None: """ Remove booking item. If the parameter `force` is set to `True`, removing of past bookings or other user's bookings is allowed (if user have right permissions). If user `user` do not have permissions to request this action, it will not be performed and `BotNoAccess` will be raised. If time has already passed and `force` parameter is set to `False`, action will not be performed and `BotTimePassed` will be raised. If such booking is not found, action will not be performed and `BotBookingNotFound` will be raised. """ if not user.get_is_in_whitelist(): raise BotNoAccess() if force: if not user.get_is_admin(): raise BotNoAccess() if not force: if item.start_datetime <= datetime.now(): raise BotTimePassed() if item.user != user: raise BotNoAccess() item.delete_instance()
def book(user: models.User, time_data: datetime, duration: timedelta, description: str) -> None: """ Create booking item. Create booking item for user `user``, with time `time_data`, duration `duration` and description `description`. If user do not have permissions to request this action, it will not be performed and `BotNoAccess` will be raised. If time span is already occupied (is intersecting with some other item), action will not be performed and `BotTimeOccupied` will be raised. If time has already passed, action will not be performed and `BotTimePassed` will be raised. """ if not user.get_is_in_whitelist(): raise BotNoAccess() if time_data <= datetime.now(): raise BotTimePassed() if not is_free_time(time_data, duration): raise BotTimeOccupied() models.BookingItem.create( start_datetime=time_data, end_datetime=time_data + duration, description=description, user=user )
def unbook(user: models.User, time_data: datetime, force: bool = False) -> None: """ Remove booking for time. Remove booking which intersects with time `time_data`. If the parameter `force` is set to `True`, removing of past bookings or other user's bookings is allowed (if user have right permissions). If user `user` do not have permissions to request this action, it will not be performed and `BotNoAccess` will be raised. If time has already passed and `force` parameter is set to `False`, action will not be performed and `BotTimePassed` will be raised. If such booking is not found, action will not be performed and `BotBookingNotFound` will be raised. """ if not user.get_is_in_whitelist(): raise BotNoAccess() if force: if not user.get_is_admin(): raise BotNoAccess() if not force: if time_data <= datetime.now(): raise BotTimePassed() booking_item: Optional[models.BookingItem] = get_booking(time_data) if booking_item is None: raise BotBookingNotFound() if not force: if booking_item.user != user: raise BotNoAccess() booking_item.delete_instance()
def process_button_unbook(_call: telebot.types.CallbackQuery, sender: models.User) -> Optional[Dict[str, Any]]: """ Button `unbook`. Start inputting data to remove booking. """ if not sender.get_is_in_whitelist(): raise BotNoAccess() sender.start_input_line_unbook() return { 'message': message_prompt_date, 'markup': get_calendar_keyboard(sender.input_calendar.year, sender.input_calendar.month), }
def add_user_to_whitelist(user: models.User, target_username: str) -> None: """ Command function. Add user with name `target_username` to whitelist. If such user could not be found, `BotUsernameNotFound` will be raised. If user `user` do not have permissions to request this action, it will not be performed and `BotNoAccess` will be raised. """ if user.get_is_admin(): raise BotNoAccess() try: models.User.get( models.User.username == target_username ).update(is_whitelisted=True) except models.User.DoesNotExist: raise BotUsernameNotFound()
def get_whitelist(user: models.User) -> List[Tuple[int, str]]: """ Command function. Return whitelist as list of user names (or `<?>` strings for users who are not present in user database). If user `user` do not have permissions to request this action, it will not be performed and `BotNoAccess` will be raised. """ if not user.get_is_admin(): raise BotNoAccess() whitelist_users = models.User.select().where(models.User.is_whitelisted) result = [] for whitelist_user in whitelist_users: if whitelist_user.username is not None: whitelist_username = ( '@' + whitelist_user.username) else: whitelist_username = '******' result.append((whitelist_user.user_id, whitelist_username,)) return result