Ejemplo n.º 1
0
def delete(request: Request) -> None:
    """Delete an URL."""
    url = object_or_404(
        Url.by_slug(request.openapi_validated.parameters["path"]["slug"],
                    db=request.db))
    request.db.delete(url)
    return None
Ejemplo n.º 2
0
def update(request: Request) -> Url:
    """Update an URL."""
    body = request.openapi_validated.body
    url = object_or_404(
        Url.by_slug(request.openapi_validated.parameters["path"]["slug"],
                    db=request.db))

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

    return url
Ejemplo n.º 3
0
def redirect(request: Request) -> None:
    """Redirect to target."""
    slug = request.matchdict["slug"]
    url = object_or_404(Url.by_slug(slug, db=request.db))
    raise HTTPFound(url.href)
Ejemplo n.º 4
0
def url(request: Request) -> Url:
    """Get an URL."""
    url = object_or_404(
        Url.by_slug(request.openapi_validated.parameters["path"]["slug"],
                    db=request.db))
    return url
Ejemplo n.º 5
0
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"