示例#1
0
def delete(request: Request) -> None:
    """Remove a comment."""
    object_or_404(
        Article.by_slug(
            request.openapi_validated.parameters["path"]["slug"], db=request.db
        )
    )

    comment = object_or_404(
        Comment.by_id(request.openapi_validated.parameters["path"]["id"], db=request.db)
    )

    request.db.delete(comment)
    return None
def unfavorite(request: Request) -> SingleArticleResponse:
    """Unfavorite an article."""
    article = object_or_404(
        Article.by_slug(request.openapi_validated.parameters["path"]["slug"],
                        db=request.db))
    request.user.unfavorite(article)
    return {"article": article}
def delete(request: Request) -> None:
    """Delete an article."""
    article = object_or_404(
        Article.by_slug(request.openapi_validated.parameters["path"]["slug"],
                        db=request.db))
    request.db.delete(article)
    return None
def profile(request: Request) -> ProfileResponse:
    """Get a profile."""
    profile = object_or_404(
        Profile.by_username(
            request.openapi_validated.parameters["path"]["username"],
            db=request.db))
    return {"profile": profile}
示例#5
0
def comments(request: Request) -> MultipleCommentsResponse:
    """Get recent comments on the given article."""
    article = object_or_404(
        Article.by_slug(
            request.openapi_validated.parameters["path"]["slug"], db=request.db
        )
    )
    return {"comments": article.comments}
示例#6
0
def create(request: Request) -> SingleCommentResponse:
    """Get an article."""
    article = object_or_404(
        Article.by_slug(request.openapi_validated.parameters["path"]["slug"],
                        db=request.db))

    body = request.openapi_validated.body
    comment = Comment(body=body.comment.body,
                      article=article,
                      author=request.user)
    article.comments.append(comment)
    return {"comment": comment}
def update(request: Request) -> SingleArticleResponse:
    """Update an article."""
    body = request.openapi_validated.body
    article = object_or_404(
        Article.by_slug(request.openapi_validated.parameters["path"]["slug"],
                        db=request.db))

    if getattr(body.article, "title", None):
        article.title = body.article.title
    if getattr(body.article, "description", None):
        article.description = body.article.description
    if getattr(body.article, "body", None):
        article.body = body.article.body

    return {"article": article}
示例#8
0
def update(request: Request) -> SingleArticleResponse:
    """Update an article."""
    body = request.openapi_validated.body
    article = object_or_404(
        Article.by_slug(
            request.openapi_validated.parameters["path"]["slug"], db=request.db
        )
    )

    if body["article"].get("title"):
        article.title = body["article"]["title"]
    if body["article"].get("description"):
        article.description = body["article"]["description"]
    if body["article"].get("body"):
        article.body = body["article"]["body"]

    return {"article": article}
def test_object_or_404() -> None:
    """Test the help guard that stops view code execution."""
    with pytest.raises(HTTPNotFound):
        object_or_404(None)

    assert object_or_404("foo") == "foo"