Exemple #1
0
def _create_comment(
        user_id: int,
        file: SolutionFile,
        kind: str,
        line_number: int,
        comment_text: Optional[str] = None,  # set when kind == text
        comment_id: Optional[int] = None,  # set when kind == id
):
    user = User.get_or_none(User.id == user_id)
    if user is None:
        # should never happen, we checked session_id == solver_id
        return fail(404, 'No such user.')

    if (not kind) or (kind not in ('id', 'text')):
        return fail(400, 'Invalid kind.')

    if line_number <= 0:
        return fail(422, f'Invalid line number: {line_number}.')

    if kind == 'id':
        new_comment_id = comment_id
    elif kind == 'text':
        if not comment_text:
            return fail(422, 'Empty comments are not allowed.')
        new_comment_id = CommentText.create_comment(text=comment_text).id
    else:
        # should never happend, kind was checked before
        return fail(400, 'Invalid kind.')

    solutions.notify_comment_after_check(user, file.solution)

    comment_ = Comment.create(
        commenter=user,
        line_number=line_number,
        comment=new_comment_id,
        file=file,
    )

    return jsonify({
        'success': 'true',
        'text': comment_.comment.text,
        'author_name': user.fullname,
        'author_role': user.role.id,
        'is_auto': False,
        'id': comment_.id,
        'line_number': line_number,
    })
Exemple #2
0
def _create_comment(
        user: User,
        file: SolutionFile,
        kind: str,
        line_number: int,
        comment_text: Optional[str] = None,  # set when kind == text
        comment_id: Optional[int] = None,  # set when kind == id
):
    if user is None:
        # should never happen, we checked session_id == solver_id
        raise ResourceNotFound('No such user.', 404)

    if (not kind) or (kind not in ('id', 'text')):
        raise NotValidRequest('Invalid kind.', 400)

    if line_number <= 0:
        raise UnprocessableRequest(f'Invalid line number: {line_number}.', 422)

    if kind == 'id':
        new_comment_id = comment_id
    elif kind == 'text':
        if not comment_text:
            raise UnprocessableRequest('Empty comments are not allowed.', 422)
        new_comment_id = CommentText.create_comment(text=comment_text).id
    else:
        # should never happend, kind was checked before
        raise NotValidRequest('Invalid kind.', 400)

    solutions.notify_comment_after_check(user, file.solution)

    return Comment.create(
        commenter=user,
        line_number=line_number,
        comment=new_comment_id,
        file=file,
    )