Пример #1
0
def board(board_name, page=None):
    if not validation.check_board_name_validity(board_name):
        abort(404)

    board: BoardModel = board_service.find_board(board_name)

    if not board:
        abort(404)

    # Page 1 is argument-less
    if page == 1:
        return redirect(url_for('board', board_name=board_name))

    if page is None:
        page = 1

    if page <= 0 or page > board.config.pages:
        abort(404)

    # Index starts from 0
    index = page - 1

    board_page = posts_service.get_board_page(board, index)

    # TODO: don't use the board id
    show_mod_buttons = show_moderator_buttons(board.id)

    return render_template('board.html',
                           board=board,
                           board_page=board_page,
                           page_index=index,
                           show_moderator_buttons=show_mod_buttons,
                           **get_board_view_params(board.config, 'board',
                                                   board_name))
Пример #2
0
 def to_python(self, value):
     if not validation.check_board_name_validity(value):
         raise ValidationError()
     model = board_service.find_board(value)
     if not model:
         raise ValidationError()
     return model
Пример #3
0
def create(board: BoardModel) -> BoardModel:
    if not validation.check_board_name_validity(board.name):
        raise ArgumentError(MESSAGE_INVALID_NAME)

    with session() as s:
        existing = s.query(BoardOrmModel).filter_by(name=board.name).one_or_none()
        if existing:
            raise ArgumentError(MESSAGE_DUPLICATE_BOARD_NAME)

        orm_board = board.to_orm_model()

        board_config = BoardConfigModel.from_defaults()
        board_config_orm = board_config.to_orm_model()
        s.add(board_config_orm)
        s.flush()

        orm_board.config_id = board_config_orm.id

        s.add(orm_board)
        s.commit()

        board = board.from_orm_model(orm_board)

        cache.set(cache_key('board_and_config', board.name), board.to_cache())

        _set_all_board_names_cache(s)

        s.commit()

        return board
Пример #4
0
def _gather_manage_params() -> ManagePostDetails:
    form = request.form

    board_name = form.get('board', None)
    if not validation.check_board_name_validity(board_name):
        abort(400)

    thread_refno = form.get('thread', type=int)
    valid_id_range(thread_refno)

    post_id = form.get('post_id', type=int)
    if not post_id:
        post_id = None

    if post_id is not None:
        valid_id_range(post_id)

    password = form.get('password', None)
    if not password:
        password = None

    if password and not validation.check_password_validity(password):
        abort(400)

    ip4 = get_request_ip4()

    mod_id = None
    if get_authed():
        mod_id = request_moderator().id

    mode_string = form.get('mode')

    return ManagePostDetails(board_name, thread_refno, post_id, ip4, mod_id, mode_string, password)
Пример #5
0
def board(board_name, page=None):
    if not validation.check_board_name_validity(board_name):
        abort(404)

    board: BoardModel = board_service.find_board(board_name)

    if not board:
        abort(404)

    # Page 1 is argument-less
    if page == 1:
        return redirect(url_for('board', board_name=board_name))

    if page is None:
        page = 1

    if page <= 0 or page > board.config.pages:
        abort(404)

    # Index starts from 0
    index = page - 1

    board_page = posts_service.get_board_page(board, index)

    # TODO: don't use the board id
    show_mod_buttons = show_moderator_buttons(board.id)

    return render_template('board.html', board=board, board_page=board_page, page_index=index,
                           show_moderator_buttons=show_mod_buttons,
                           **get_board_view_params(board.config, 'board', board_name))
Пример #6
0
 def to_python(self, value):
     if not validation.check_board_name_validity(value):
         raise ValidationError()
     model = board_service.find_board(value)
     if not model:
         raise ValidationError()
     return model
Пример #7
0
    def __call__(self, form, field):
        if not validation.check_board_name_validity(field.data):
            raise ValidationError('Board name not valid.')

        board = board_service.find_board(field.data)
        if not board:
            raise ValidationError('Board does not exist')
        field.board = board
Пример #8
0
def _gather_post_params() -> Tuple[BoardModel, PostDetails]:
    form = request.form

    # Gather params
    thread_refno_raw = form.get('thread', None)
    thread_refno = None
    if thread_refno_raw is not None:
        try:
            thread_refno = int(thread_refno_raw)
            valid_id_range(thread_refno)
        except ValueError:
            abort(400)

    board_name = form.get('board', None)
    if not validation.check_board_name_validity(board_name):
        abort(400)

    board = board_service.find_board(board_name)
    if not board:
        abort(404)

    text = form.get('comment', None)
    name = form.get('name', None)
    subject = form.get('subject', None)
    password = form.get('password', None)

    # Convert empty strings to None
    if not text:
        text = None
    if not name:
        name = None
    if not subject:
        subject = None
    if not password:
        password = None

    file = request.files.get('file', None)
    has_file = file is not None and file.filename is not None and len(file.filename) > 0

    ip4 = get_request_ip4()

    with_mod = form.get('with_mod', default=False, type=bool)
    mod_id = None
    if with_mod:
        moderator = request_moderator() if get_authed() else None
        if moderator is not None:
            mod_id = moderator.id

    return board, PostDetails(form, board_name, thread_refno, text, name, subject, password, has_file,
                              ip4, mod_id, None)
Пример #9
0
def find_by_name(name: str) -> Optional[BoardModel]:
    if not validation.check_board_name_validity(name):
        raise ArgumentError(MESSAGE_INVALID_NAME)

    board_cache = cache.get(cache_key('board_and_config', name))
    if not board_cache:
        with session() as s:
            q = s.query(BoardOrmModel).filter_by(name=name)
            q = q.options(joinedload('config'))
            board_orm_model = q.one_or_none()
            if not board_orm_model:
                return None
            board = BoardModel.from_orm_model(board_orm_model, include_config=True)
            cache.set(cache_key('board_and_config', name), board.to_cache())
            return board

    return BoardModel.from_cache(board_cache)
Пример #10
0
def find_by_names(names: List[str]) -> List[BoardModel]:
    """unknown names are ignored!"""

    for name in names:
        if not validation.check_board_name_validity(name):
            raise ArgumentError(MESSAGE_INVALID_NAME)

    boards = []
    with session() as s:
        for name in names:
            board_cache = cache.get(cache_key('board_and_config', name))
            if board_cache:
                boards.append(BoardModel.from_cache(board_cache))
            else:
                board_orm_model = s.query(BoardOrmModel).filter_by(name=name).one_or_none()
                if board_orm_model:
                    board = BoardModel.from_orm_model(board_orm_model, include_config=True)
                    cache.set(cache_key('board_and_config', name), board.to_cache())
                    boards.append(board)

    return boards
Пример #11
0
 def __call__(self, form, field):
     if not validation.check_board_name_validity(field.data):
         raise ValidationError('Board name not valid.')