def board_routine(user: User, func, id: int, *args, **kwargs): """ Basic board routine. Serves as a layer of abstraction to treat priority errors in board method resource routines. This fuction needs to be passed (and called) by session_oriented_request() (inherited from AuthEntity), with subroutine function (that has to be called from within this routine) and an ID (serves as Board ID). Function passed to this routine needs this signature: >>> def func(user: User, board: Board, *args, **kwargs) N.B. *args and **kwargs can be omitted. :param user: User Object (got from session_oriented_request()) :param func: Subroutine function (needs to be called from within this routine) :param id: Board ID :param args: Name arguments :param kwargs: Positional arguments :return: """ # Requesting board object board = BoardAPI.get_board(id) if board is None: # Board does not exists return responses.client_error(404, 'Board does not exist') elif not user.board_subscribed(id): # User not subscribed to this board return responses.client_error(401, 'User is not authorized to see this board') # Return the result got from closure function passed as argument return func(user, board, *args, **kwargs)
def thread_routine(user: User, func, id: int, *args, **kwargs): """ Basic thread routine. Serves as a layer of abstraction to thread priority errors in thread method resource routines. This function needs to be passed (and called) by session_oriented_request() (inherited from AuthEntity), with subroutine function (that has to be called from within this routine) and an ID (serves as Thread ID). Function passed to this routine needs this signature: >>> def func(user: User, thread: Thread, *args, **kwargs) N.B. *args and **kwargs can be omitted. :param user: User Object (got from session_oriented_request()) :param func: Subroutine function (needs to be called from within this routine) :param id: Thread ID :param args: Name arguments :param kwargs: Positional arguments :return: """ thread = ThreadAPI.get_thread(id) if thread is None: return responses.client_error(404, 'Thread does not exist') if not user.board_subscribed(thread.board): return responses.client_error(401, 'User is not authorized to see this thread') return func(user, thread, *args, **kwargs)